java rsa nopadding,在Java中使用模数和指数进行RSA加密

I'm currently doing RSA encryption on Java and I have to use private and public modulus for the encryption. I currently Have the following:

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);

}

In the main method I have the following:

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));

In the console I get the following: ���.7���p;%kV�9y���xa�ɼ{ and not "12" If I make the String data = "12345";, I then get: javax.crypto.BadPaddingException: Message is larger than modulus

Firstly why is the encryption and decryption not working? Why am I not getting back "12". Secondly why can I not have data be greater than 2 characters?

Note I'm using the following website to get the modulus and exponent values.

解决方案

There is an error creating the private key. You are providing the public exponent instead of private and (as commented @dave_thomsom_085) private exponent instead of modulus

Change createPrivateKey() with

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);

}

Also you should not use raw RSA without padding for security reasons. Use RSA/ECB/PKCS1Padding or the new OAEP padding RSA/ECB/OAEPWithSHA1AndMGF1Padding

According to this answer you can encrypt with PKCS1Padding data up to 11 bytes less than the key size. and using OAEP it must be less than the size of the key modulus – 41

Then, using your 1024 bits key:

PKCS1: (1024 bits / 8) - 11 = 117 bytes

OAEP: (1024 bits / 8) - 42 = 86 bytes

Also is recommended to use CRT instead of private exponent directly

RSAPrivateCrtKeySpec privateKeySpec =

new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExpP, primeExpQ, crtCoefficient);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值