我用Java设置了公钥和私钥加密,并分发了这两个用户的公钥(通信是在两个用户之间进行的)。现在我希望用户交换一个对称密钥。我该做的是:
我的用户A生成密钥的代码:
1. KeyGenerator keyGenerator = KeyGenerator.getInstance(ENCMETHOD);
2. SecureRandom secureRandom = new SecureRandom();
3. int keyBitSize = 128;
4. keyGenerator.init(keyBitSize, secureRandom);
5. secretKey = keyGenerator.generateKey();
6. encodedKey = Base64.getEncoder().encodeToString(secretKey.getEncoded());
// encrypt with public key of B and then my private key
7. String encryptedMessage = encodedKey;
8. encryptedMessage = ac.encryptText
(
ac.encryptText(encryptedMessage, otherUserPublickey),
privateKey
);第8行引发以下错误:
javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:344)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at driver.AsymmetricCryptography.encryptText(AsymmetricCryptography.java:73) // please refer to the code section below for this method
at driver.ClientOne.main(ClientOne.java:158) // this is line 8 in the above code方法AsymmetricCryptography.encryptText(String message,PrivateKey键):
public String encryptText(String msg, PrivateKey key)
throws
UnsupportedEncodingException, IllegalBlockSizeException,
BadPaddingException, InvalidKeyException {
this.cipher.init(Cipher.ENCRYPT_MODE, key);
return Base64.encodeBase64String(cipher.doFinal(msg.getBytes("UTF-8")));
}
// this.cipher = Cipher.getInstance("RSA");任何帮助都是非常感谢的。谢谢。
发布于 2018-10-21 21:49:35
看起来你已经超过了你可以用RSA加密的数据量(参见这里的https://security.stackexchange.com/questions/44702/whats-the-limit-on-the-size-of-the-data-that-public-key-cryptos-can-handle),它本质上就是模数大小,可能是由Base 64编码造成的。
https://stackoverflow.com/questions/52919233
复制相似问题