TestAesAndRsa
package com.zhuyun.test;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.SecretKey;
import org.junit.Test;
import com.zhuyun.aes.AESUtil;
import com.zhuyun.rsa.RSAUtil;
public class TestAesAndRsa {
//测试RSA与AES的结合。
// 客户端用公钥加密AES秘钥,AES秘钥加密实际内容;
// 服务端用私钥解密AES秘钥,AES秘钥解密实际内容
@Test
public void testAesAndRsa() throws Exception {
//===============生成公钥和私钥,公钥传给客户端,私钥服务端保留==================
//生成RSA公钥和私钥,并Base64编码,生成一次以后,就写死在配置文件或代码中,下次不再重新生成
KeyPair keyPair = RSAUtil.getKeyPair();
String publicKeyStr = RSAUtil.getPublicKey(keyPair);
String privateKeyStr = RSAUtil.getPrivateKey(keyPair);
System.out.println("RSA公钥Base64编码:" + publicKeyStr);
System.out.println("RSA私钥Base64编码:" + publicKeyStr);
//=================客户端=================
//hello, i am infi, good night! 需要加密的实际内容
String message = "hello, i am infi, good night!";
//将Base64编码后的公钥转换成PublicKey对象
PublicKey publicKey = RSAUtil.string2PublicKey(publicKeyStr);
//生成AES秘钥,并Base64编码
String aesKeyStr = AESUtil.genKeyAES();
System.out.println("AES秘钥Base64编码:" + aesKeyStr);
//用公钥加密AES秘钥
byte[] publicEncrypt = RSAUtil.publicEncrypt(aesKeyStr.getBytes(), publicKey);
//公钥加密AES秘钥后的内容Base64编码
String publicEncryptStr = RSAUtil.byte2Base64(publicEncrypt);
System.out.println("公钥加密AES秘钥并Base64编码的结果:" + publicEncryptStr);
//将Base64编码后的AES秘钥转换成SecretKey对象
SecretKey aesKey = AESUtil.loadKeyAES(aesKeyStr);
//用AES秘钥加密实际的内容
byte[] encryptAES = AESUtil.encryptAES(message.getBytes(), aesKey);
//AES秘钥加密后的内容Base64编码
String encryptAESStr = AESUtil.byte2Base64(encryptAES);
System.out.println("AES秘钥加密实际的内容并Base64编码的结果:" + encryptAESStr);
//############## 网络上传输的内容有Base64编码后的公钥加密AES秘钥的结果 和 Base64编码后的AES秘钥加密实际内容的结果 #################
//############## 即publicEncryptStr和encryptAESStr ###################
//===================服务端================
//将Base64编码后的私钥转换成PrivateKey对象
PrivateKey privateKey = RSAUtil.string2PrivateKey(privateKeyStr);
//公钥加密AES秘钥后的内容(Base64编码),进行Base64解码
byte[] publicEncrypt2 = RSAUtil.base642Byte(publicEncryptStr);
//用私钥解密,得到aesKey
byte[] aesKeyStrBytes = RSAUtil.privateDecrypt(publicEncrypt2, privateKey);
//解密后的aesKey
String aesKeyStr2 = new String(aesKeyStrBytes);
System.out.println("解密后的aesKey(Base64编码): " + aesKeyStr2);
//将Base64编码后的AES秘钥转换成SecretKey对象
SecretKey aesKey2 = AESUtil.loadKeyAES(aesKeyStr2);
//AES秘钥加密后的内容(Base64编码),进行Base64解码
byte[] encryptAES2 = AESUtil.base642Byte(encryptAESStr);
//用AES秘钥解密实际的内容
byte[] decryptAES = AESUtil.decryptAES(encryptAES2, aesKey2);
//解密后的实际内容
System.out.println("解密后的实际内容: " + new String(decryptAES));
}
}
关于RSAUtil 和AESUtil这两个类,详见我的前面两篇博客《java实现RSA的简单加密解密》和 《java实现AES的简单加密解密》
运行结果如下:
RSA公钥Base64编码:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3edVb1wjqKNucCPW7/0Ct4lFBVdEVZyD
tT+cbzncvnHokEzNj2bRUSk0sR4YBUscPyR5H8c5p2MgRcuLLMWzXBpUS4TO+NHfLzzj30WPcqIL
MaO6nIMCqH6zn3Bz0my56Gx7hHUI+D7ija/JtroLQiI9Z69pljtxjEfX7Oa7EgDWc0uhPh+grhHD
LCsdibn5Czpc+GkkRYOV9iPIaia3+h2Eqyj9WOOxd6czsgGEjO8+g28Kl7CLKzwE1RUYEZmu+Tch
U2417ejpAjyhR/2rdE1l9US75Tz0/6RFDCTTo13ZLsN/PSVfyZtZ0CLjsCAjimDq2GR1qVU0K7XE
SBrDuwIDAQAB
RSA私钥Base64编码:MIIEvwIBAD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tVdWg1KVh1j/H9RLe9IAMMCgYEArKT44v5qS7/PmsksWMi0g5EzAnNiczfEnQ5A
JEZlP8aO/Pkqf9EEByx3n+zqa1qjGfE1z4BeMmQmlOmUs5ki/PZ8rWfYH19eJWhox4w972auPwLs
KTFAxd+YQp8s8BHkYTJToPueKxPDsfvN4lPzXE5IOQo2r8WphJZNZMvWojECgYAOl1SYKEDYY7+/
WlqG8z0wYIkGul36dUTO+dfzJwpyU0HPd2QNZjHVOdr9ysS9chCCQ7MsleSmjGpRzwVNoGGI5ymN
4FbhEdh8RjVn9bRqscOTLE6T9/is0N+4RU6uj2/VGM8uDIj1IGXIAEny2Rbmbhrl7P4SlJvQQNd0
21j5WwKBgQCbO0pumh5n/6dBP66FMsp2+v7pDFUO14yipDY0KiVRrsb7UdkdOxcQPrmf9flOfBaT
iAI8hu6PlEknb6nEw4+kYJN0rf4ENHaYhilDoJruLoPAtE9JkP0rEKNr7YfC/ZmmEFQitmHt7aoK
ThTTxQArD8YZ/5YLENp5ECxgWifPYA==
AES秘钥Base64编码:3aNZH0W9NAeFAJx66rf/Pw==
公钥加密AES秘钥并Base64编码的结果:A17kkwTbz7p7UP6Z56cau3f0OoF+yZCngbJTQDUUsGwdj89alHIKg05tT7t4Nh8hq7l/1KkhNw6Y
RWiQ3LmoU55H82xzZWpj19YzqDCNyceIJKO7YafYI40uqiBrzSX9ruwrNMlv9AQi8RYKcXzX/89E
BJuSHSOttfgoXaEOmqgNDNAeipqMxHMCrqi4D6ST32WpttKlYm9NsxgqDuUfLrXQRoPusUfPk1ZI
N4mVvHX39n+WuOfMLjo2BGXddyDj8p6I3dD2v7OiLC5wiB6LYlB6yftX2xLdy077FpkJW6yyki2Y
O+mktov3ZSRflgBn5mjnGagJeqw5PXj4F3uo0w==
AES秘钥加密实际的内容并Base64编码的结果:z307ajQDSY6zC9WyPMOZiUvPyRZWUbJv2k44Ee3/kiQ=
解密后的aesKey(Base64编码): 3aNZH0W9NAeFAJx66rf/Pw==
解密后的实际内容: hello, i am infi, good night!