对外分享
RSA 公钥加密,私钥解密
RSA 私钥加密,公钥解密
本文档使用 MrDoc 发布
-
+
首页
RSA 公钥加密,私钥解密
``` public class RsaTest { public static void main(String[] args) throws Exception { // 生成公钥和私钥 KeyPair keyPair = generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // 要加密的信息 String message = "This is a very long message that needs to be encrypted in chunks. It should be longer than a single RSA encryption block to demonstrate chunking."; // 加密 byte[] encryptedMessage = encrypt(message.getBytes(), publicKey); System.out.println("密文: " + Base64.getEncoder().encodeToString(encryptedMessage2)); // 解密 byte[] decryptedMessage = decrypt(encryptedMessage, privateKey); // 输出结果 System.out.println("Original message: " + message); System.out.println("Decrypted message: " + new String(decryptedMessage)); // 以下开始测试序列化和反序列化 PublicKey publicKey2 = getPublicKey(convertPublic(publicKey)); PrivateKey privateKey2 = getPrivateKey(convertPrivate(privateKey)); // 加密 byte[] encryptedMessage2 = encrypt(message.getBytes(), publicKey2); // 解密 byte[] decryptedMessage2 = decrypt(encryptedMessage2, privateKey2); System.out.println("Original message2: " + message); System.out.println("Decrypted message2: " + new String(decryptedMessage2)); } private static KeyPair generateKeyPair() throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); return keyGen.generateKeyPair(); } private static byte[] encrypt(byte[] message, Key publicKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); int maxLength = 109; List<byte[]> chunks = new ArrayList<>(); for (int i = 0; i < message.length; i += maxLength) { byte[] chunk = new byte[Math.min(maxLength, message.length - i)]; System.arraycopy(message, i, chunk, 0, chunk.length); chunks.add(cipher.doFinal(chunk)); } return combineChunks(chunks); } private static byte[] decrypt(byte[] encryptedMessage, Key privateKey) throws Exception { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, privateKey); int chunkSize = 128; // 对于1024位的密钥 List<byte[]> chunks = new ArrayList<>(); for (int i = 0; i < encryptedMessage.length; i += chunkSize) { byte[] chunk = new byte[chunkSize]; System.arraycopy(encryptedMessage, i, chunk, 0, chunkSize); chunks.add(cipher.doFinal(chunk)); } return combineChunks(chunks); } private static byte[] combineChunks(List<byte[]> chunks) { int totalLength = chunks.stream().mapToInt(chunk -> chunk.length).sum(); byte[] result = new byte[totalLength]; int currentPosition = 0; for (byte[] chunk : chunks) { System.arraycopy(chunk, 0, result, currentPosition, chunk.length); currentPosition += chunk.length; } return result; } public static String convertPublic(PublicKey publicKey) { return Base64.getEncoder() .withoutPadding() .encodeToString(publicKey.getEncoded()); } public static String convertPrivate(PrivateKey privateKey) { return Base64.getEncoder() .withoutPadding() .encodeToString(privateKey.getEncoded()); } @SneakyThrows public static PublicKey getPublicKey(String sshPublicKey) { sshPublicKey = sshPublicKey.replace("\n", ""); byte[] publicKeyBytes = Base64.getDecoder().decode(sshPublicKey); // 创建X509EncodedKeySpec对象 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); return KeyFactory.getInstance("RSA").generatePublic(keySpec); } @SneakyThrows public static PrivateKey getPrivateKey(String sshPrivateKey) { sshPrivateKey = sshPrivateKey.replace("\n", ""); byte[] byteKey = Base64.getDecoder().decode(sshPrivateKey); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(byteKey); return KeyFactory.getInstance("RSA").generatePrivate(pkcs8EncodedKeySpec); } } ```
寒烟濡雨
2023年8月31日 21:55
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码