我有一个问题。在我的应用程序中,我使用 AES 加密和解密。当我加密数据并将其发送到服务器端时,它会显示一些垃圾字符添加到加密数据中。但是怎么做?
实际上,这个加密数据是从我这边完美解密的,但是服务器接收到的时候有垃圾字符。
这是我的加密代码:
public static byte[] encrypt( byte[] keyData, byte[] data )
throws CryptoException, IOException
{
// Create the AES key to use for encrypting the data.
// This will create an AES key using as much of the keyData
// as possible.
AESKey key = new AESKey( keyData );
// Now, we want to encrypt the data.
// First, create the encryptor engine that we use for the actual
// encrypting of the data.
AESEncryptorEngine engine = new AESEncryptorEngine( key );
// Since we cannot guarantee that the data will be of an equal block
// length we want to use a padding engine (PKCS5 in this case).
PKCS5FormatterEngine fengine = new PKCS5FormatterEngine( engine );
// Create a BlockEncryptor to hide the engine details away.
ByteArrayOutputStream output = new ByteArrayOutputStream();
BlockEncryptor encryptor = new BlockEncryptor( fengine, output );
encryptor.write( data );
encryptor.close();
output.close();
return output.toByteArray();
}
请注意,我无权访问服务器上的解密逻辑。