我编写了一个使用AES加密的BlackBerry应用程序。我正在尝试使用C#中的AesCryptoServiceProvider解密它。
BlackBerry代码似乎没有使用IV,这意味着我没有任何东西可以传递给AesCryptoServiceProvider。
如果没有IV,我是否可以解密AES?如果可以,怎么做?
我编写了一个使用AES加密的BlackBerry应用程序。我正在尝试使用C#中的AesCryptoServiceProvider解密它。
BlackBerry代码似乎没有使用IV,这意味着我没有任何东西可以传递给AesCryptoServiceProvider。
如果没有IV,我是否可以解密AES?如果可以,怎么做?
阅读黑莓Java加密文档,似乎不应直接使用AESEncryptionEngine。如果直接使用它,您最终会得到(我假设)ECB模式,这将导致企鹅图像的以下加密。不要这样做。
相反,似乎要使用一些安全操作模式,您需要实际使用基本AESEncrypt / Decrypt Engine周围的包装器。您需要使用CBCEncryptionEngine来完成此操作。这是here中的一些示例代码。请注意,在创建时IV是随机的,因此您无需设置它或担心重用。只需在此处将DES替换为AES即可。
// sampleDESCBCEncryption
private static int sampleDESCBCEncryption(
byte[] secretKey, byte[] initVector, byte[] plainText, byte[] cipherText, int
dataLength )
throws CryptoException, IOException
{
// Create a new DES key based on the 8 bytes in the secretKey array
DESKey key = new DESKey( secretKey );
// Create a new initialization vector using the 8 bytes in initVector
InitializationVector iv = new InitializationVector( initVector );
// Create a new byte array output stream for use in encryption
NoCopyByteArrayOutputStream out = new NoCopyByteArrayOutputStream();
// Create a new instance of a BlockEncryptor passing in an instance of a CBC encryptor engine
// (containing an instance of a DES encryptor engine), the initialization vector, and the
// output stream
BlockEncryptor cryptoStream = new BlockEncryptor(
new CBCEncryptorEngine( new DESEncryptorEngine( key ), iv ), out );
// Write dataLength bytes from plainText to the CFB encryptor stream
cryptoStream.write( plainText, 0, dataLength );
cryptoStream.close();
// Now copy the encrypted bytes from out into cipherText and return the length
int finalLength = out.size();
System.arraycopy( out.getByteArray(), 0, cipherText, 0, finalLength );
return finalLength;
}