mal*_*ere 0 java cryptography rsa
我一直无法使用RSA公钥加密.以下是重现问题的示例JUnit代码:
public class CryptoTests {
private static KeyPair keys;
@BeforeClass
public static void init() throws NoSuchAlgorithmException{
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = CryptoUtils.getSecureRandom();
keyGen.initialize(2176, random);
keys = keyGen.generateKeyPair();
}
@Test
public void testRepeatabilityPlainRSAPublic() throws EdrmCryptoException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException{
byte[] plaintext = new byte [10];
Random r = new Random();
r.nextBytes(plaintext);
Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE, keys.getPublic());
byte[] encrypted1 = rsa.doFinal(plaintext);
rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE, keys.getPublic());
byte[] encrypted2 = rsa.doFinal(plaintext);
rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE, keys.getPublic());
byte[] encrypted3 = rsa.doFinal(plaintext);
assertArrayEquals(encrypted1, encrypted2);
assertArrayEquals(encrypted1, encrypted3);
}
}
Run Code Online (Sandbox Code Playgroud)
结果?断言失败了.
为什么会出现这种行为?据我记得我的加密类,任何密钥都可以用于加密.然而,这不是这里发生的事情.我用私钥测试了同样的东西,我得到了一个可重复的输出.
如果出于某种原因,禁止使用公钥进行RSA加密,那么为什么我没有得到例外?
我该怎么做才能获得可重复的结果?
PS我的JDK是在Ubuntu 10.10盒子上运行的1.6.0_22.
| 归档时间: |
|
| 查看次数: |
1880 次 |
| 最近记录: |