java 不使用 NoPadding 的 RSA 示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2580473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RSA example that do not use NoPadding
提问by Tom Brito
Where can I find a RSA encrypt example that does notuse "NoPadding"?
我在哪里可以找到不使用“NoPadding”的 RSA 加密示例?
--update
- 更新
Better: how to make this SSCCE run correctly without throw the "too much data for RSA block"exception?
更好:如何使这个 SSCCE 正确运行而不抛出“RSA 块的数据过多”异常?
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
/**
* Basic RSA example.
*/
public class TestRSA {
public static void main(String[] args) throws Exception {
byte[] input = new byte[100];
Cipher cipher = Cipher.getInstance("RSA/None/NoPadding", "BC");
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
// create the keys
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger("d46f473a2d746537de2056ae3092c451",
16), new BigInteger("11", 16));
RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(new BigInteger(
"d46f473a2d746537de2056ae3092c451", 16), new BigInteger("57791d5430d593164082036ad8b29fb1",
16));
RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privKeySpec);
// encryption step
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherText = cipher.doFinal(input);
// decryption step
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] plainText = cipher.doFinal(cipherText);
}
}
--update: about loop
--更新:关于循环
Using:
使用:
byte[] cipherText = new byte[input.length];
for (int i = 0; i < input.length; i++) {
byte[] singleByteArray = new byte[] { input[i] };
cipherText[i] = cipher.doFinal(singleByteArray)[0];
}
does not work fine. For a unknown reason the cipherText became full of zeros - even if the input is an array of 0x03.
不能正常工作。由于未知原因, cipherText 变成了全零 - 即使输入是 0x03 的数组。
回答by President James K. Polk
The Sun Providers Documentationfor the SunJCE provider tells you what padding specifications are allowed in the Cipher.getInstance() argument. Try Cipher.getInstance("RSA/ECB/PKCS1PADDING");
SunJCE 提供程序的Sun Providers 文档告诉您 Cipher.getInstance() 参数中允许的填充规范。尝试Cipher.getInstance("RSA/ECB/PKCS1PADDING");
EDIT:
It is not a padding issue, it is more that you have a misunderstanding of how RSA is used in cryptography. You can either 1) make the modulus bigger than the data, 2) use a Hybrid cryptosystem, or 3) least desirable is to manually break up the input into chunks that are each smaller than the modulus. If you are going to use PKCS1 padding (which is generally recommended), then the input must be not larger than n-11 bytes in length, where n is the number of bytes needed to store the RSA modulus.
编辑:
这不是填充问题,更多的是您对 RSA 在密码学中的使用方式有误解。您可以 1) 使模数大于数据,2) 使用混合密码系统,或 3) 最不希望的是手动将输入分成每个都小于模数的块。如果您打算使用 PKCS1 填充(通常建议使用),则输入的长度不得超过 n-11 个字节,其中 n 是存储 RSA 模数所需的字节数。
回答by Bozho
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherData = cipher.doFinal(content);
Update:Are you sure you need bouncycastle for this? And why not just pass RSAas argument to Cipher.getInstance(..)?
更新:你确定你需要 bouncycastle 吗?为什么不RSA作为参数传递给Cipher.getInstance(..)?
Update 2:Why don't you try any of these RSA encryption examples?
更新 2:您为什么不尝试任何这些RSA 加密示例?

