![](/img/trans.png)
[英]Encryption in Android using RSA algorithm with given modulus and exponent
[英]RSA Encryption using modulus and exponent in Java
我目前正在使用Java進行RSA加密,我必須使用私有和公共模數進行加密。 我目前有以下內容:
private void createPublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
String publicModulus = "d2c34017ef94f8ab6696dae66e3c0d1ad186bbd9ce4461b68d7cd017c15bda174045bfef36fbf048" +
"73cfd6d09e3806af3949f99c3e09d6d3c37f6398d8c63f9a3e39b78a187809822e8bcf912f4c44a8" +
"92fe6a65a477ddea9582738317317286a2610ba30b6b090c3b8c61ffb64207229b3f01afe928a960" +
"c5a44c24b26f5f91";
BigInteger keyInt = new BigInteger(publicModulus, 16);
BigInteger exponentInt = new BigInteger("10001", 16);
RSAPublicKeySpec keySpeck = new RSAPublicKeySpec(keyInt, exponentInt);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(keySpeck);
}
private void createPrivateKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
String privateModulus = "6c97ab6369cf00dd174bacd7c37e6f661d04e5af10670d4d88d30148ec188e63227b8dac0c517cf9" +
"67aa73cd23684c9165dc269f091bfab33b6c5c7db95b54130e348255c30aaaac1c7f09ef701e0d6f" +
"6dc142d2e4ed78466cc104e28d50be7adf3863afc021dbdd8b5f0b968b7cd965242c7d8d4b32ee84" +
"0fac3cad134344c1";
BigInteger privateModulusInt = new BigInteger(privateModulus, 16);
BigInteger exponentInt = new BigInteger("10001", 16);
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(privateModulusInt, exponentInt);
KeyFactory factory = KeyFactory.getInstance("RSA");
privateKey = factory.generatePrivate(privateKeySpec);
}
在主要方法中,我有以下內容:
createPrivateKey();
createPublicKey();
String data = "12";
Cipher cipher1 = Cipher.getInstance("RSA/ECB/NoPadding");
cipher1.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedData = cipher1.doFinal(data.getBytes());
Cipher cipher2 = Cipher.getInstance("RSA/ECB/NoPadding");
cipher2.init(Cipher.DECRYPT_MODE,privateKey);
byte[] decryptedData = cipher2.doFinal(encryptedData);
System.out.println(new String(decryptedData));
在控制台中,我得到以下結果: .7 p;%kV 9y xa ɼ{而不是"12"
如果我使String data = "12345";
,然后我得到: javax.crypto.BadPaddingException: Message is larger than modulus
首先為什么加密和解密不起作用? 為什么我沒有回到"12"
。 其次,為什么我的數據不能大於2個字符?
注意我使用以下網站獲取模數和指數值。
創建私鑰時出錯。 您提供公共指數而不是私有和(如評論@ dave_thomsom_085)私有指數而不是模數
使用更改createPrivateKey()
private static PrivateKey createPrivateKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
String publicModulus = "d2c34017ef94f8ab6696dae66e3c0d1ad186bbd9ce4461b68d7cd017c15bda174045bfef36fbf048" +
"73cfd6d09e3806af3949f99c3e09d6d3c37f6398d8c63f9a3e39b78a187809822e8bcf912f4c44a8" +
"92fe6a65a477ddea9582738317317286a2610ba30b6b090c3b8c61ffb64207229b3f01afe928a960" +
"c5a44c24b26f5f91";
String privateExponent = "6c97ab6369cf00dd174bacd7c37e6f661d04e5af10670d4d88d30148ec188e63227b8dac0c517cf9" +
"67aa73cd23684c9165dc269f091bfab33b6c5c7db95b54130e348255c30aaaac1c7f09ef701e0d6f" +
"6dc142d2e4ed78466cc104e28d50be7adf3863afc021dbdd8b5f0b968b7cd965242c7d8d4b32ee84" +
"0fac3cad134344c1";
BigInteger privateExponenInt = new BigInteger(privateExponent, 16);
BigInteger keyInt = new BigInteger(publicModulus, 16);
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(keyInt, privateExponenInt);
KeyFactory factory = KeyFactory.getInstance("RSA");
return factory.generatePrivate(privateKeySpec);
}
此外,出於安全原因,您不應使用原始RSA而不使用填充。 使用RSA/ECB/PKCS1Padding
或新的OAEP填充RSA/ECB/OAEPWithSHA1AndMGF1Padding
根據這個答案,您可以使用PKCS1Padding
數據加密,最多比密鑰大小少11個字節。 並且使用OAEP它必須小於鍵模數的大小 - 41
然后,使用您的1024位密鑰:
(1024 bits / 8) - 11 = 117 bytes
(1024 bits / 8) - 42 = 86 bytes
還建議直接使用CRT而不是私有指數
RSAPrivateCrtKeySpec privateKeySpec =
new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExpP, primeExpQ, crtCoefficient);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.