什么是RSA加密算法?
RSA加密算法是一种非对称加密算法,其玩法打破了以往所有加密算法的规则.在RSA出现之前,所有的加密方法都是同一种模式:加密解密的规则使用同一种方式.这种长达几个世纪的加密方案有一个致命的缺陷.在传递加密信息时,必须让对方拿到解密的规则才能正常解密.由于加密解密的规则一致,所以保存和传递"密钥",就成了最头疼的问题。
- 使用公钥加密的数据,利用私钥进行解密
- 使用私钥加密的数据,利用公钥进行解密
RSA加密使用了"一对"密钥.分别是公钥和私钥,这个公钥和私钥其实就是一组数字!其二进制位长度可以是1024位或者2048位.长度越长其加密强度越大,目前为止公之于众的能破解的最大长度为768位密钥,只要高于768位,相对就比较安全.所以目前为止,这种加密算法一直被广泛使用.
如何使用?
生成RSA非对称密钥对 SK&PK
/**
/**
* 生成RSA密钥对
*
* @return Keys.Rsa
* @throws NoSuchAlgorithmException
*/
public static Keys.Rsa genKey() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
String publicKeyString = Tools.encodeToString(publicKey.getEncoded());
String privateKeyString = Tools.encodeToString((privateKey.getEncoded()));
Keys.Rsa rsa = new Keys().new Rsa(publicKeyString, privateKeyString);
return rsa;
}
RSA 公钥加密
/**
/**
* RSA 公钥加密
*
* @param str
* @param publicKey
* @return
* @throws Exception
*/
public static String encrypt(String str, String publicKey) throws Exception {
//base64编码的公钥
byte[] keyBytes = Tools.decode(publicKey);
KeySpec keySpec = new X509EncodedKeySpec(keyBytes);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(KEY_ALGORITHM).generatePublic(keySpec);
//RSA加密
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String outStr = Tools.encodeToString(cipher.doFinal(str.getBytes(Charsets.UTF_8)));
return outStr;
}
RSA 私钥解密
/**
/**
* RSA 私钥解密
*
* @param str
* @param privateKey
* @return
* @throws Exception
*/
public static String decrypt(String str, String privateKey) throws Exception {
//base64编码的私钥
byte[] keyBytes = Tools.decode(privateKey);
KeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(keySpec);
//RSA解密
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, priKey);
//64位解码加密后的字符串
byte[] inputByte = Tools.decode(str.getBytes(Charsets.UTF_8));
return new String(cipher.doFinal(inputByte));
}
Test测试
import com.alibaba.fastjson.JSON;
import com.pnoker.common.bean.encryp.Keys;
import com.pnoker.common.utils.encryp.AesTools;
import com.pnoker.common.utils.encryp.RsaTools;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* <p>Copyright(c) 2018. Pnoker All Rights Reserved.
* <p>Author : Pnoker
* <p>Email : pnokers@gmail.com
* <p>Description: AES\RSA 加密算法测试
*/
@Slf4j
public class TestEncrypt {
@Test
public void aesTest() throws Exception {
Keys.Aes aes = AesTools.genKey();
log.info(" aes key : {}", JSON.toJSONString(aes));
String str = "nihao@ks.pnoker.cn,nihao@ks.pnoker.cn";
log.info("str : {}", str);
String ens = AesTools.encrypt(str, aes.getPrivateKey());
log.info("ens : {}", ens);
String des = AesTools.decrypt(ens, aes.getPrivateKey());
log.info("des : {}", des);
}
@Test
public void rsaTest() throws Exception {
Keys.Rsa rsa = RsaTools.genKey();
log.info(" rsa key : {}", JSON.toJSONString(rsa));
String str = "nihao@ks.pnoker.cn,nihao@ks.pnoker.cn";
log.info("str : {}", str);
String ens = RsaTools.encrypt(str, rsa.getPublicKey());
log.info("ens : {}", ens);
String des = RsaTools.decrypt(ens, rsa.getPrivateKey());
log.info("des : {}", des);
}
}
Base64工具类
import com.google.common.base.Charsets;
import java.util.Base64;
/**
* <p>Copyright(c) 2018. Pnoker All Rights Reserved.
* <p>Author : Pnoker
* <p>Email : pnokers@gmail.com
* <p>Description: 工具类
*/
public class Tools {
/**
* 将字符串进行Base64编码
*
* @param str
* @return 返回字节流
*/
public static byte[] encode(String str) {
byte[] bytes = Base64.getEncoder().encode(str.getBytes(Charsets.UTF_8));
return bytes;
}
/**
* 将字节流进行Base64编码
*
* @param bytes
* @return 返回字符串
*/
public static String encodeToString(byte[] bytes) {
String str = Base64.getEncoder().encodeToString(bytes);
return str;
}
/**
* 必须配合encode使用,用于encode编码之后解码
*
* @param str 字符串
* @return 返回字节流
*/
public static byte[] decode(String str) {
byte[] bytes = Base64.getDecoder().decode(str);
return bytes;
}
/**
* 必须配合encode使用,用于encode编码之后解码
*
* @param input 字节流
* @return 返回字节流
*/
public static byte[] decode(byte[] input) {
byte[] bytes = Base64.getDecoder().decode(input);
return bytes;
}
}
实体封装类
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* <p>Copyright(c) 2018. Pnoker All Rights Reserved.
* <p>Author : Pnoker
* <p>Email : pnokers@gmail.com
* <p>Description: AES & RSA 算法密钥实体类
*/
public class Keys {
/**
* RSA 密钥对
*/
@Data
@AllArgsConstructor
public class Rsa {
private String publicKey;
private String privateKey;
}
/**
* Aes 密钥
*/
@Data
@AllArgsConstructor
public class Aes {
private String privateKey;
}
}
完整例子推荐
IOT应用实例下载 AES/RSA算法
项目推荐
DC3是基于Spring Cloud的开源可分布式物联网(IOT)平台,用于快速开发、部署物联设备接入项目,是一整套物联系统解决方案。