RSA+Base64加解密Java工具类RSAUtil

Posted jmy520

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RSA+Base64加解密Java工具类RSAUtil相关的知识,希望对你有一定的参考价值。

RSA+Base64加解密Java工具类RSAUtil(公钥和私钥请自行生成)

import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Properties;
import javax.crypto.Cipher;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class RSAUtil {
    /**
     * RSA+BASE64加密(先RSA加密,然后Base64加密)
     * @param secretContent:secretContent要加密的内容
     * @return
     */
    public static String encrypt(String secretContent) {
        String byte2Base64 = "";
        try {
            //===============生成公钥和私钥,公钥传给客户端,私钥服务端保留==================
            //生成RSA公钥和私钥,并Base64编码
            String filePath = "key.properties";
            Properties prop = new Properties();
            String PRIVATEKEY = null;
            String PUBLICKEY = null;
            prop.load(RSAUtil.class.getClassLoader().getResourceAsStream(filePath));
            PRIVATEKEY = prop.getProperty("PRIVATEKEY");
            PUBLICKEY = prop.getProperty("PUBLICKEY");
            //=================客户端================
            //将Base64编码后的公钥转换成PublicKey对象
            PublicKey publicKey = string2PublicKey(PUBLICKEY);
            //用公钥加密
            byte[] publicEncrypt = publicEncrypt(secretContent.getBytes(), publicKey);
            //加密后的内容Base64编码
            byte2Base64 = byte2Base64(publicEncrypt);
            System.out.println(byte2Base64);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byte2Base64;
    }

    /**
     * BASE64+RAS解密(先Base64解密,然后RSA解密)
     * @param decodeContent :解密字符串
     */
    public static String decode(String decodeContent) {
    	 byte[] privateDecrypt = new byte[] {};
        try {
            //===============生成公钥和私钥,公钥传给客户端,私钥服务端保留==================
            //生成RSA公钥和私钥,并Base64编码
            String filePath = "key.properties";
            Properties prop = new Properties();
            String PRIVATEKEY = null;
            String PUBLICKEY = null;
            prop.load(RSAUtil.class.getClassLoader().getResourceAsStream(filePath));
            PRIVATEKEY = prop.getProperty("PRIVATEKEY");
            PUBLICKEY = prop.getProperty("PUBLICKEY");
            //===================服务端================
            //将Base64编码后的私钥转换成PrivateKey对象
            PrivateKey privateKey = string2PrivateKey(PRIVATEKEY);
            //加密后的内容Base64解码
            byte[] base642Byte = base642Byte(decodeContent);
            //用私钥解密
           privateDecrypt = privateDecrypt(base642Byte, privateKey);
           
        }catch(Exception e) {
        	e.printStackTrace();
        }
        return  new String(privateDecrypt);
    }
    //生成秘钥对
    public static KeyPair getKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        return keyPair;
    }
    
    //获取公钥(Base64编码)
    public static String getPublicKey(KeyPair keyPair){
        PublicKey publicKey = keyPair.getPublic();
        byte[] bytes = publicKey.getEncoded();
        return byte2Base64(bytes);
    }
    
    //获取私钥(Base64编码)
    public static String getPrivateKey(KeyPair keyPair){
        PrivateKey privateKey = keyPair.getPrivate();
        byte[] bytes = privateKey.getEncoded();
        return byte2Base64(bytes);
    }
    
    //将Base64编码后的公钥转换成PublicKey对象
    public static PublicKey string2PublicKey(String pubStr) throws Exception{
        byte[] keyBytes = base642Byte(pubStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return publicKey;
    }
    
    //将Base64编码后的私钥转换成PrivateKey对象
    public static PrivateKey string2PrivateKey(String priStr) throws Exception{
        byte[] keyBytes = base642Byte(priStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }
    
    //公钥加密
    public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] bytes = cipher.doFinal(content);
        return bytes;
    }
    
    //私钥解密
    public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] bytes = cipher.doFinal(content);
        return bytes;
    }
    
    //字节数组转Base64编码
    public static String byte2Base64(byte[] bytes){
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(bytes);
    }
    
    //Base64编码转字节数组
    public static byte[] base642Byte(String base64Key) throws IOException{
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(base64Key);
    }
}

以上是关于RSA+Base64加解密Java工具类RSAUtil的主要内容,如果未能解决你的问题,请参考以下文章

JAVA怎么样实现Base64加密解密?

RSA加解密工具类

RSA 加解密(Java 实现)

java常用加解密工具类

python rsa加解密

RSA加密和解密工具类