背景:需要对字符串进行RSA加解密,网上的太多废话。
目的:为了解决你的需求,好抄。
上代码:
package ai.saas.framework.common.util;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* RSA 加解密
*/
public class RSA {
/**
* RSA 公钥和私钥
* 通过RSA密钥处理器获得,而非自定义字符串 照抄即可
*/
public final static String pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxF4tm6qJ00KhfZVkubzgSznFuVAqw5vP3mMULfj33HcP9XjUdtxsmwtDQABgSLdihwCtliGBblsfkobeQxJEtyALECd1u3sU8QJarSGCZZBgD+Y2rNQeTKcmP02qIN8FS3y81jgwL321Scr4rrfRbHA0hax9Ap9f5IUBpLWX+5wIDAQAB";
public final static String priKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALEXi2bqonTQqF9lWS5vOBLOcW5UCrDm8/eYxQt+Pfcdw/1eNR23GybC0NAAGBIt2KHAK2WIYFuWx+Sht5DEkS3IAsQJ3W7exTxAlqtIYJlkGAP5jas1B5MpyY/Taog3wVLfLzWODAvfbVJyviut9FscDSFrH0Cn1/khQGktZf7nAgMBAAECgYAoIfHRjPLsDi1aERm40DOg72/9VMZneK9pORHOnPll+zTjQmo/JbPdgODZgmvyErm5TleHVvSfTPhX894pzKIpoViT/dK/1W8eaJwD5r1l6pVptgm8yjHz5wcTdfB3Pu4OFgkciKKbMkinhx7GdBPcT5kyHUsJ4rLb4xvXy7RC4QJBANjJxkNcnsuWq6pzZnO+ktdRqtSoNTnTswqa0NYdXUnvkBilzFqzfzu7SZkN61picjYxE0bGdsnQGSIKfxemuD0CQQDRH6ormeGD7Z6WXyzKIW2gOrm/jcfSi61/czyGOziXRL8wULE4UyymYw8g3vhdmKjsqMxS3bt/E/cPavQXDmHzAkABdJlYKm8GfuR4KH4vmL/zoiC7fMWetcUl9RQh2+CcdndJMxPEKCBajk9YL7e8y5xosLuoZfy2Lid9CwT/a6chAkBdDCHDVKyq7sdpUN+08Dq+2GJdgNAwNkX0N2nur9r2M3h+Huk7joaHYfwRPCR2QL6ozlJbbJ1IZbHAAm6o85VRAkBQYUGoP40qE8eKUsZfTLyrOGy6FUiX8p+kJRv9RoYVa/aQDxisdo1+lwe9rhnpWkyjE3jRm3XDKScEhl6MAMnN";
public static void main(String[] args) throws Exception {
String str = "ABCDEFG"; // 需要操作加解密的字符串
String encryptionBase64Str = pubKeyEncryption(str); // 加密
System.out.println("公钥加密,私钥解密 --加密: " + encryptionBase64Str); // RSA加密再base64编码后的字符串
String decryptionStr = priKeyDecryption(encryptionBase64Str); // 解密
System.out.println("公钥加密,私钥解密 --解密: " + decryptionStr);
}
/**
* 公钥加密
*
* @param str 需要加密操作的字符串
* @return 结果再Base64加密后的字符串
* @throws Exception
*/
public static String pubKeyEncryption(String str) throws Exception {
// String 转 公钥
PublicKey rsaPublicKey = getPublicKey(pubKey);
//公钥加密
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(str.getBytes());
return Base64.encodeBase64String(result);
}
/**
* 用私钥 解密
*
* @param encryptionBase64Str RSA加密后又base64编码后的字符串
* @return 解密结果
* @throws Exception
*/
public static String priKeyDecryption(String encryptionBase64Str) throws Exception {
// base64 解码
byte[] base64 = Base64.decodeBase64(encryptionBase64Str);
// String 转 私钥
PrivateKey rsaPrivateKey = getPrivateKey(priKey);
//私钥解密
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(base64);
return new String(result);
}
/**
* String转公钥PublicKey
*
* @param key
* @return
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* String转私钥PrivateKey
*
* @param key
* @return
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = (new BASE64Decoder()).decodeBuffer(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
}