我在Java中创建一个小型加密程序,我希望从服务器向客户端发送一个byte [],以便在客户端读取它以进行解密。
服务器(生成加密密钥 - 使用somePublicKey加密)
c.init(Cipher.ENCRYPT_MODE, somePublicKey);
byte[] encryptedBytes = c.doFinal(generatedKey.getEncoded());
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(encryptedBytes);
out.close();
客户端(读取字节[]以便可以进行解密)
ObjectInputStream inside = new ObjectInputStream(s.getInputStream());
byte[] bytesToDecrypt = (byte[])inside.readObject();
cd.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cd.doFinal(bytesToDecrypt);
SecretKey decrypted = new SecretKeySpec(decryptedBytes, 0, decryptedBytes.length, "AES");
当我尝试读取客户端的字节时,我得到javax.crypto.BadPaddingException:解密错误。任何人都可以建议我做错了吗?