java RSA非对称加密-解密(简洁明了)

上篇文章:RSA非对称加密证书的生成(简洁明了)

Java非对称加密证书生成看我的上篇文章,下边主要是Java的解密部分,利用证书pkcs8_private_key.der(即:RSA非对称加密证书的生成(简洁明了)生成Java支持的PKCS8二进制类型的私钥)获取私钥再进行密文的解密。

下边是主要的代码:

package home;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.security.*;
import java.security.spec.EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;

public class RSAUtil {

    private static PrivateKey privateKey;

    static {
        try {
            InputStream in = (RSAUtil.class.getResourceAsStream("pkcs8_private_key.der"));
            //System.out.println("der:"+in);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] temp = new byte[1024];
            int count = 0;
            while ((count = in.read(temp)) != -1) {
                bout.write(temp, 0, count);
                temp = new byte[1024];
            }
            in.close();
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(bout.toByteArray());
            privateKey = keyFactory.generatePrivate(privateKeySpec);
            //System.out.println("privateKey:"+privateKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("RSA encoder Exception");
        }
    }

    public static byte[] decodeBase64(String input) throws Exception{  
        Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");  
        Method mainMethod= clazz.getMethod("decode", String.class);  
        mainMethod.setAccessible(true);  
         Object retObj=mainMethod.invoke(null, input);  
         return (byte[])retObj;  
    } 

    public static String decodeText(String sec) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchProviderException, UnsupportedEncodingException,Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        //byte[] base64 = Base64.decodeBase64(sec.getBytes());
        byte[] base64 = decodeBase64(sec);
        byte[] deBytes = cipher.doFinal(base64);
        return new String(deBytes, "UTF-8");

    }

    public static void main(String[] args) throws Exception {
            //之前的加密明文为:“我是铭文,请把我非对称加密”;
            //对应的密文为:“f71XCg6R6wHC/SU/W84kFC9whsRr/ivQK+6Z3d5X78NOSCAplxqtIH6Hb3hHLAQiQm347WdYQdJItFAM/99QMD8I9ImiI7rMBvxEij7mLJtaY+pg4mkyuSvQe4C8tXA6ZEE0AWcaLw62JScV4EIaMHzdw2TxjNcUMmf+M5rRix0=”
        System.out.println(RSAUtil.decodeText("f71XCg6R6wHC/SU/W84kFC9whsRr/ivQK+6Z3d5X78NOSCAplxqtIH6Hb3hHLAQiQm347WdYQdJItFAM/99QMD8I9ImiI7rMBvxEij7mLJtaY+pg4mkyuSvQe4C8tXA6ZEE0AWcaLw62JScV4EIaMHzdw2TxjNcUMmf+M5rRix0="));
    }
}

下图为结果图:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/lining1041204250/article/details/79304261
今日推荐