/rsa-aes-java

Using RSA and AES encrypted communication between client and server written in Java.

Primary LanguageJava

Usage

Client

//客户端代码
String text = "hello server!";

//随机生成16位aes密钥
byte[] aesKey = SecureRandomUtil.getRandom(16).getBytes();

//使用aes密钥对数据进行加密
String encryptText = AESCipher.encrypt(aesKey, text);

//使用客户端私钥对aes密钥签名
String signature = RSACipher.sign(Config.CLIENT_PRIVATE_KEY, aesKey);

//使用服务端公钥加密aes密钥
byte[] encryptKey = RSACipher.encrypt(Config.SERVER_PUBLIC_KEY, aesKey);

Server

//服务端代码
//模拟接收到客户端的加密的aes密钥和签名和加密的密文
byte[] encryptKey = "bJT8chyoHaFNG+Cvrc11sPSYzO0WeiiO/UJTHp44On7sfTOxmfard7z0GHPqp3HDT1qFkILUUPQqF7NZSys2VwgYHFxWgisif/CPHWAJ2t7zWSTunOChd6OGqH+n4pbzIsJjYJLFgNYPCNA6IB3Qlcm7DYDjDCVbvttVjHxSyRU=".getBytes();
String signature = "Cbcrf/WrmXMovWyueuDwfQtCRIY9PUDBZOf9npnuCj3tBlzZe2OJdZzlvXC2b5C4EC90eRR/NUTGFu5/HZ0n3+p/PtQBWMjEuuIDCC7+xraCG7MlcQPgpHOxyuZJRlRz8nNND8j4C6t/GJ6p5U80nTz3QD4QXHh2m3CKeNmt/B4=";
String encryptText = "9VpY0ZHvCSbfapOn/obRvg==";

//使用服务端私钥对加密后的aes密钥解密
byte[] aesKey = RSACipher.decrypt(Config.SERVER_PRIVATE_KEY, encryptKey);

//使用客户端公钥验签
Boolean result = RSACipher.checkSign(Config.CLIENT_PUBLIC_KEY, aesKey1, signature);

//使用aes私钥解密密文
String decryptText = AESCipher.decrypt(aesKey1, encryptText);

Test secret key

Client public key(PKCS#8)

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDv/LnAoDkyewwjwqwgi9VSg51F
+tUJ8cGwL6Rqdf5ZXrRCHI1KLjOxdFbzB81YjS76cOzezQRz2vuYDo7OvLfYSjFI
fmukUxN+EliK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