最近公司有个新需求,用到了RSA加密算法,于是百度+视频简单了解了下,先上代码
package com.jm.decode;
import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;
import javax.crypto.Cipher;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class StringSplit {
/*
* RSA加密测试
* */
public static String encode(String str) throws Exception{
String[] charStr = str.split("#");
String charStr0 = charStr[0]+"#";
String charStr1 = charStr[1]+"#"+charStr[2]+"#"+charStr[3]+"#"+charStr[4];
String charStr2 = "#"+charStr[5]+"#"+charStr[6]+"#"+charStr[7]+"#"+charStr[8]+"#"+charStr[9]+"#"+charStr[10]+"#"+charStr[11]+"#"+charStr[12];
String jmstr = encodes(charStr1);
return charStr0 + jmstr + charStr2;
}
public static String decode(String str) throws Exception{
String[] charStr = str.split("#");
String charStr0 = charStr[0]+"#";
String charStr1 = charStr[1];
try {
byte[] b = charStr1.getBytes();
charStr1 = new String(b, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String charStr2 = "#"+charStr[2]+"#"+charStr[3]+"#"+charStr[4]+"#"+charStr[5]+"#"+charStr[6]+"#"+charStr[7]+"#"+charStr[8]+"#"+charStr[9];
return charStr0 + decodes(charStr1) + charStr2;
}
public static String encodes(String inStr) throws Exception{
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++){
a[i] = (char) (a[i] ^ (char)(')' ^ (char)(','^'w')));
}
String s = new String(a);
//生成RSA公钥和私钥,并Base64编码
KeyPair keyPair = StringSplit.getKeyPair();
String publicKeyStr = StringSplit.getPublicKey(keyPair);
//将Base64编码后的公钥转换成PublicKey对象
PublicKey publicKey = StringSplit.string2PublicKey(publicKeyStr);
//用公钥加密
byte[] publicEncrypt = StringSplit.publicEncrypt(s.getBytes(), publicKey);
//返回加密后的数据
return StringSplit.byte2Base64(publicEncrypt);
}
public static String decodes(String inStr) throws Exception{
//生成RSA公钥和私钥,并Base64编码
KeyPair keyPair = StringSplit.getKeyPair();
String privateKeyStr = StringSplit.getPrivateKey(keyPair);
//将Base64编码后的私钥转换成PrivateKey对象
PrivateKey privateKey = StringSplit.string2PrivateKey(privateKeyStr);
//加密后的内容Base64解码
byte[] base642Byte = StringSplit.base642Byte(inStr);
//用私钥解密
byte[] privateDecrypt = StringSplit.privateDecrypt(base642Byte, privateKey);
inStr = new String(privateDecrypt);
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++){
a[i] = (char) (a[i] ^ (char)(')' ^ (char)(','^'w')));
}
String s = new String(a);
return s;
}
//生成秘钥对
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);
}
public static String test() throws Exception{
//生成RSA公钥和私钥,并Base64编码
KeyPair keyPair = StringSplit.getKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
byte[] bytes = privateKey.getEncoded();
return byte2Base64(bytes);
}
public static void main(String[] args) throws Exception{
String str2 = "待加密的数据#测试测试测试";
byte[] byteArray7 = str2.getBytes();
System.out.println(byteArray7.length);
//===============生成公钥和私钥,公钥传给客户端,私钥服务端保留==================
//生成RSA公钥和私钥,并Base64编码
KeyPair keyPair = StringSplit.getKeyPair();
String publicKeyStr = StringSplit.getPublicKey(keyPair);
String privateKeyStr = StringSplit.getPrivateKey(keyPair);
// String privateKeyStr =StringSplit.test();
System.out.println("RSA公钥Base64编码:" + publicKeyStr);
byte[] byteArray = publicKeyStr.getBytes();
System.out.println(byteArray.length);
System.out.println("RSA私钥Base64编码:" + privateKeyStr);
byte[] byteArray1 = privateKeyStr.getBytes();
System.out.println(byteArray1.length);
//=================客户端=================
//hello, i am infi, good night!加密
// String message = "hello, i am infi, good night!";
//将Base64编码后的公钥转换成PublicKey对象
PublicKey publicKey = StringSplit.string2PublicKey(publicKeyStr);
//用公钥加密
byte[] publicEncrypt = StringSplit.publicEncrypt(str2.getBytes(), publicKey);
//加密后的内容Base64编码
String byte2Base64 = StringSplit.byte2Base64(publicEncrypt);
System.out.println("公钥加密并Base64编码的结果:" + byte2Base64);
byte[] byteArray2 = byte2Base64.getBytes();
System.out.println(byteArray2.length);
//############## 网络上传输的内容有Base64编码后的公钥 和 Base64编码后的公钥加密的内容 #################
//===================服务端================
//将Base64编码后的私钥转换成PrivateKey对象
PrivateKey privateKey = StringSplit.string2PrivateKey(privateKeyStr);
//加密后的内容Base64解码
byte[] base642Byte = StringSplit.base642Byte(byte2Base64);
//用私钥解密
byte[] privateDecrypt = StringSplit.privateDecrypt(base642Byte, privateKey);
//解密后的明文
System.out.println("解密后的明文: " + new String(privateDecrypt));
// System.out.println(encodes(str));
// System.out.print(decodes(encodes(str)));
}}
以下几点需要注意(如有错误请指教):
1.密文长度<公钥长度
2.keyPairGenerator.initialize(2048); 通过此方法设置模长度,模长度越大加密强度越大(通常1024即基本不可能被破解);视频说模长度需为64的倍数,经测试不是64倍数也可以。
3.公钥长度+私钥长度 约等于 模长度