// cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
public byte[] sign_and_encrypt( SecretKey key ) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
cipher.init(Cipher.WRAP_MODE, priv_key);
byte [] signed = cipher.wrap(key);
cipher.init(Cipher.ENCRYPT_MODE, their_pub_key);
System.out.println("Signed length: " + signed.length);
return cipher.doFinal(signed);
}
我在 java 中对会话密钥进行签名和加密时遇到问题。带符号的 byte[] 长度为 256 字节,但doFinal
底部的 只能占用大小为 245 或更小的块。
有没有办法设置添加的填充量?或者另一种在不加密两个签名切片的情况下使其工作的方法?
我得到的例外是
javax.crypto.IllegalBlockSizeException: Data must not be longer than 245 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 Crypto.JEncrypRSA.sign_and_encrypt(JEncrypRSA.java:76)
at Chat.Client.get_session_key(Client.java:115)
at Chat.Client.main(Client.java:131)