2013-12-15 42 views
0

我的RSA解密这部分代码:BadPaddingException:执行会话密钥的RSA加密时,数据必须以零

// Turn the encoded key into a real RSA private key. 
// Private keys are encoded in PKCS#8. 
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); 
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 
PrivateKey privateKey = keyFactory.generatePrivate(keySpec); 

// Create a cipher using that key to initialize it 
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 

// Read in the encrypted bytes of the session key 
DataInputStream dis = new DataInputStream(new FileInputStream(fileInput)); 
byte[] encryptedKeyBytes = new byte[dis.readInt()]; 
dis.readFully(encryptedKeyBytes); 

// Decrypt the session key bytes. 
rsaCipher.init(Cipher.DECRYPT_MODE, privateKey); 
byte[] rijndaelKeyBytes = rsaCipher.doFinal(encryptedKeyBytes); 

// Transform the key bytes into an actual key. 
SecretKey rijndaelKey = new SecretKeySpec(rijndaelKeyBytes, "Rijndael"); 

当我选择私钥文件出现错误,我通过会话进行加密键做的主要文件的非对称加密:

javax.crypto.BadPaddingException:数据必须以零

H启动我能解决这个错误吗?

+0

请在发布之前使用拼写检查器并重新阅读您的问题。确保使用使用良好的标签。 [标签:密码]很难使用,请使用[标签:加密]或[标签:加密]代替 - 如果你不知道你的问题可能不会被注意到。 –

回答

0

你的问题表明,有一些误解:

当我选择私钥文件,我通过会话密钥进行加密,这样做的主要文件的非对称加密出现错误

因此,上述代码使用RSA私钥解密会话密钥。然后会话密钥可以用来解密数据。因此,您需要使用新的随机会话密钥加密数据,然后使用RSA公钥加密会话密钥

您应该一般不会加密任何其他实体的RSA私钥。 RSA私钥应该保密。您只能对它们进行加密,以便在密钥存储中进行备份或保护

+0

从我的答案user3077162中缺少任何东西? –