java中长消息的RSA加密和解密
我想使用java标准库,据我所知,它的功能输入是有限的。所以我为此目的实施了两种方法。它们是这样的:
private byte[] RSAenc(String in) throws Exception {
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.ENCRYPT_MODE, privKey);
int l = in.length();
byte[] part;
byte[] result = new byte[(int)(64*java.lang.Math.ceil(l/20.0))];
int i = 0;
while(i*20+20<l) {
part = c.doFinal(in.substring(i*20,i*20+19).getBytes("UTF-8"));
System.arraycopy(part, 0, result, i*64, part.length);
i = i+1;
}
part = c.doFinal(in.substring(i*20,l-1).getBytes("UTF-8"));
System.arraycopy(part, 0, result, i*64, part.length);
return result;
}
private String RSAdec(byte [] in) throws Exception {
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.DECRYPT_MODE, privKey);
String result = "";
byte[] part = new byte[64];
int l = in.length;
int i = 0;
while(i+64<=l) {
System.arraycopy(in, i, part, 0, part.length);
result = result + new String(c.doFinal(part), "UTF-8");
i= i+64;
}
return result;
}
它们以这种方式工作:为了加密,我将字符串分解为最多 20 个大小的子字符串,然后使用密码对其进行加密。为了解密,我将字节数组分解为 64 字节块,并对它们应用密码解密。 是否已经有一个函数可以做到这一点?或者至少有一个更简洁的解决方案吗?我的方法安全吗?在所有 JRE 发行版上,加密结果长度 (result.length
) 是否始终为 64?
谢谢,
I want to use java standard library, and as much as I know, its functions inputs are limited. So I implemented two methods to this purpose. Here they are:
private byte[] RSAenc(String in) throws Exception {
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.ENCRYPT_MODE, privKey);
int l = in.length();
byte[] part;
byte[] result = new byte[(int)(64*java.lang.Math.ceil(l/20.0))];
int i = 0;
while(i*20+20<l) {
part = c.doFinal(in.substring(i*20,i*20+19).getBytes("UTF-8"));
System.arraycopy(part, 0, result, i*64, part.length);
i = i+1;
}
part = c.doFinal(in.substring(i*20,l-1).getBytes("UTF-8"));
System.arraycopy(part, 0, result, i*64, part.length);
return result;
}
private String RSAdec(byte [] in) throws Exception {
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.DECRYPT_MODE, privKey);
String result = "";
byte[] part = new byte[64];
int l = in.length;
int i = 0;
while(i+64<=l) {
System.arraycopy(in, i, part, 0, part.length);
result = result + new String(c.doFinal(part), "UTF-8");
i= i+64;
}
return result;
}
They work in this manner: for encryption I break the string to at most, 20 size substrings, and then use the Cipher to encrypt them. For decryption, I break byte array to 64 byte blocks, and apply Cipher decryption to them.
Is there already a function which do this? Or at least is there a neater solution? How safe is my approach? Is encryption result length (result.length
) always 64 on all distributions of JRE?
Thanx,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RSA 适用于密钥加密,不适用于批量数据加密。
大多数协议不是使用 RSA 加密消息,而是为对称密码(如 AES)生成密钥,并用该密钥加密消息。然后,使用 RSA 加密该对称密钥,以便只有消息接收者才能恢复它。 RSA 加密的对称密钥与 AES 加密的消息一起发送给接收者。
RSA is suited to key encipherment, not bulk data encryption.
Instead of encrypting messages with RSA, most protocols generate a key for a symmetric cipher, like AES, and encrypt the message with that. Then, RSA is used to encrypt that symmetric key so that only the message recipient can recover it. The RSA-encrypted symmetric key is sent with the AES-encrypted message to the recipient.