使用AesCryptoServiceProvider在没有IV的情况下进行解密

3

我编写了一个使用AES加密的BlackBerry应用程序。我正在尝试使用C#中的AesCryptoServiceProvider解密它。

BlackBerry代码似乎没有使用IV,这意味着我没有任何东西可以传递给AesCryptoServiceProvider。

如果没有IV,我是否可以解密AES?如果可以,怎么做?


请不要在标题前加上"C#.NET"等内容,这是标签的作用。 - John Saunders
1
如果您的消息全部是垃圾(多个块),那么您的密钥或密码文本(编码)是不正确的。如果是IV,只有第一个块会出现乱码。如果是填充,则解密应该失败(或原始解密将显示错误的填充字节)。 - Maarten Bodewes
1
还需要注意的是,强烈不建议使用ECB模式下的块密码,比如AES。ECB模式存在许多攻击手段,会对安全性和数据完整性造成威胁。请谨慎操作。 - Luke
谢谢。这是为了我的大学项目,时间已经不多了,所以不幸的是我必须坚持使用ECB。我一定会在报告中提到研究漏洞并加以说明。感谢您的提醒 :) - jim
1
@conor:ECB模式不需要IV,没有IV也应该正确解密。 - rossum
显示剩余4条评论
1个回答

1

阅读黑莓Java加密文档,似乎不应直接使用AESEncryptionEngine。如果直接使用它,您最终会得到(我假设)ECB模式,这将导致企鹅图像的以下加密。不要这样做。

Bad Encryption 相反,似乎要使用一些安全操作模式,您需要实际使用基本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;
}    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接