一、RSA算法
1977年,三位数学家Rivest、Shamir 和 Adleman 设计了一种算法,可以实现非对称加密。这种算法用他们三个人的名字命名,叫做RSA算法。从那时直到现在,RSA算法一直是最广为使用的"非对称加密算法"。毫不夸张地说,只要有计算机网络的地方,就有RSA算法。
这种算法非常可靠,密钥越长,它就越难破解。根据已经披露的文献,目前被破解的最长RSA密钥是768个二进制位。也就是说,长度超过768位的密钥,还无法破解(至少没人公开宣布)。因此可以认为,1024位的RSA密钥基本安全,2048位的密钥极其安全。
RSA加密较耗性能,仅能对较短的字符串进行加密,不宜对长字符串进行加密,可参照https的实现方式,浏览器生成对称密钥把内容加密,用RSA公钥加密对称密钥,把两者都发送到服务器,服务器使用RSA私钥解密对称密钥,再用对称密钥解密内容,由于每次请求对称密钥都可以重新生成,RSA又很难破解,这就保证了整体的安全性
二、java后台实现
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class RSAHelper {
private static PrivateKey PRIVATE_KEY;
static{
try {
PRIVATE_KEY=getPrivateKey(PropertiesUtil.getProperty("rsa.private.key", "")); //获取properties中配好的私钥
} catch (Exception e) {
PRIVATE_KEY=null;
}
}
/**
* 生成RSA密钥对(默认密钥长度为1024)
*
* @return
*/
public static KeyPair generateRSAKeyPair() {
return generateRSAKeyPair(1024);
}
/**
* 生成RSA密钥对
*
* @param keyLength 密钥长度
*
* @return
*/
public static KeyPair generateRSAKeyPair(int keyLength) {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(keyLength);
return kpg.genKeyPair();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
// 用公钥加密
public static byte[] encryptData(byte[] data, PublicKey pubKey) {
try {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return cipher.doFinal(data);
} catch (Exception e) {
return null;
}
}
/**
*
* 用公钥加密
* @param str 待加密字符串
* @param enCode 字符串编码格式
* @param pubKey 公钥
* @return 加密后字符串
*/
public static String encryptData(String str,String enCode, PublicKey pubKey) {
try {
byte[] data=str.getBytes(enCode);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(data));
} catch (Exception e) {
return null;
}
}
// 用私钥解密
public static byte[] decryptData(byte[] encryptedData, PrivateKey priKey) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return cipher.doFinal(encryptedData);
} catch (Exception e) {
return null;
}
}
/**
* 用私钥解密
* @author suo
* @param encryptedStr 待解密字符串
* @param enCode 字符串编码格式
* @param priKey 私钥
* @return 解密后字符串
*/
public static String decryptData(String encryptedStr,String enCode, PrivateKey priKey) {
try {
byte[] encryptedData=Base64.getDecoder().decode(encryptedStr);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(encryptedData),enCode);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 使用既有的私钥进行解密
* @param encryptedStr
* @param enCode
* @return
*/
public static String decryptData(String encryptedStr,String enCode) {
return decryptData(encryptedStr, enCode, PRIVATE_KEY);
}
/**
* 根据指定私钥对数据进行签名(默认签名算法为"SHA1withRSA")
*
* @param data 要签名的数据
* @param priKey 私钥
* @return
*/
public static byte[] signData(byte[] data, PrivateKey priKey) {
return signData(data, priKey, "SHA1withRSA");
}
/**
* 根据指定私钥和算法对数据进行签名
*
* @param data 要签名的数据
* @param priKey 私钥
* @param algorithm 签名算法
* @return
*/
public static byte[] signData(byte[] data, PrivateKey priKey,
String algorithm) {
try {
Signature signature = Signature.getInstance(algorithm);
signature.initSign(priKey);
signature.update(data);
return signature.sign();
} catch (Exception ex) {
return null;
}
}
/**
* 用指定的公钥进行签名验证(默认签名算法为"SHA1withRSA")
*
* @param data 数据
* @param sign 签名结果
* @param pubKey 公钥
* @return
*/
public static boolean verifySign(byte[] data, byte[] sign, PublicKey pubKey) {
return verifySign(data, sign, pubKey, "SHA1withRSA");
}
/**
*
* @param data 数据
* @param sign 签名结果
* @param pubKey 公钥
* @param algorithm 签名算法
* @return
*/
public static boolean verifySign(byte[] data, byte[] sign,
PublicKey pubKey, String algorithm) {
try {
Signature signature = Signature.getInstance(algorithm);
signature.initVerify(pubKey);
signature.update(data);
return signature.verify(sign);
} catch (Exception ex) {
return false;
}
}
/**
* 从base64字符串获取公钥
* @param key
* @return
* @throws Exception
*/
public static PublicKey getPublicKey(String key) throws Exception {
byte[] keyBytes;
keyBytes = Base64.getDecoder().decode(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* 从base64字符串获取私钥
* @param key
* @return
* @throws Exception
*/
public static PrivateKey getPrivateKey(String key) throws Exception{
byte[] keyBytes;
keyBytes = Base64.getDecoder().decode(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
/**
* 把密钥转成base64字符串
* @param key
* @return
* @throws Exception
*/
public static String getKeyString(Key key) throws Exception {
byte[] keyBytes = key.getEncoded();
String s = Base64.getEncoder().encodeToString(keyBytes);
return s;
}
}
三、前端js代码
前端使用了jsencrypt.js,代码如下
引入jsencrypt.js库
<script type="text/javascript" src="bin/jsencrypt.js"></script>
js库使用
var encrypt = new JSEncrypt();
encrypt.setPublicKey($('#pubkey').val()); //设置公钥
var encrypted = encrypt.encrypt($('#input').val()); //加密
//console.log(encrypted);
// Decrypt with the private key...
var decrypt = new JSEncrypt();
decrypt.setPrivateKey($('#privkey').val()); //设置私钥
var uncrypted = decrypt.decrypt(encrypted); //解密
var publicKey='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDUV0bsCCz6hFnag6uvbARN9Yz59PDyC/PWq9U5\nbu6AYOgpz5CVHlAmGdhdp3ooFd9zJIWW8kCHNicP8ZGWFvlFSRVRf952dNDG0wvL0z6DXpe3ylHO\nYVlKVEUkMGYE3PvPxAeLMiSAJYiwGK3o338qXmn4xP+zfnA2y/xQHl9MDwIDAQAB';
encrypt.setPublicKey(publicKey);
console.log(encrypt.encrypt('测试'));
注意:密钥长度越长,所能加密的字符串也越长,1024-bit keys can only encrypt 117 bytes without some sort of padding, but a 2048-bit one can handle up to 245 bytes, and a 4096-bit key up to 501 bytes,当你发现加密失败,可能是因为密钥长度不够,当然密钥越长加解密所需时间也越长,具体用多长可自行尝试决定