bouncycastle for java 中的 RSA - 如何获得密文随机化?
我实际上正在为我的应用程序 RSA 加密使用 bouncycastle 库。 我的问题是:当我使用相同的密钥对一个明文加密两次时,它将产生两个不同的密文,因此在 bouncycastles 实现中必须存在某种随机化(RSA 本身不是随机的,因此 enc(a, k)总是一样的)。
谁能告诉我这是怎么做到的?我发现了一些关于加密致盲的信息,但对我来说,我必须使用一些致盲引擎来实现这一点。
这是我的源代码:
private byte[] encRSA(byte[] in, java.security.PublicKey publicKey) {
try {
Cipher rsaCipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", "BC");
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
rsaCipher.update(in);
return rsaCipher.doFinal();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
有人可以帮助我吗?
谢谢!!!
I´m acutally using bouncycastle library for my applications RSA crypto.
My question is: When I encrypt one plaintext two times using the same key, It will lead to two different ciphertexts, so there has to be some kind of randomization in bouncycastles implementation (RSA itself is not randomized, so enc(a, k) is always the same).
Can anyone please tell me, how this is done? I found out something about crypto blinding, but it seemed for me, that I´d have to use some blinding-engine for that.
Here my Sourcecode:
private byte[] encRSA(byte[] in, java.security.PublicKey publicKey) {
try {
Cipher rsaCipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", "BC");
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
rsaCipher.update(in);
return rsaCipher.doFinal();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Can anyone please help me?
Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RSA 输出不是随机的,但 PKCS1Padding 是随机的,导致每次都有不同的输出。有关详细信息,请参阅 RFC 3218。
实际上需要随机填充来对抗攻击,攻击者可以通过加密消息并与他截获的加密输出进行比较来尝试猜测消息。
RSA output is not random, but the PKCS1Padding is, leading to a different output each time. See RFC 3218 for more information.
The random padding is actually needed to counter attacks where an attacker could try to guess a message by encrypting one and comparing to the encrypted output he intercepted.