简体   繁体   English

使用AesCryptoServiceProvider解密而不使用IV

[英]use AesCryptoServiceProvider to Decrypt without IV

I have written a BlackBerry app that uses AES encryption. 我编写了一个使用AES加密的BlackBerry应用程序。 I am trying to decrypt this using the AesCryptoServiceProvider in C#. 我试图使用C#中的AesCryptoServiceProvider解密它。

The BlackBerry code doesn't seem to use an IV which means I have nothing to pass to the AesCryptoServiceProvider. BlackBerry代码似乎没有使用IV,这意味着我没有任何东西可以传递给AesCryptoServiceProvider。

Is it possible for me to decrypt AES without an IV, if so, how? 我是否有可能在没有IV的情况下解密AES,如果是这样,怎么样?

Reading the blackberry java crypto documentation, it appears you are not supposed to directly use AESEncryptionEngine. 阅读blackberry java加密文档,看来你不应该直接使用AESEncryptionEngine。 If you use it straight, you end up with ( I assume) ECB mode which results in the following encryption of an image of a penguin. 如果你直接使用它,你最终得到(我假设)ECB模式,这导致企鹅图像的以下加密。 don't do it. 不要这样做。

加密不好 Rather it appears that to use some secure mode of operation, you need to actually use a wrapper around the basic AESEncrypt / Decrypt Engine. 相反,似乎要使用某种安全操作模式,您需要实际使用基本AESEncrypt / Decrypt Engine的包装器。 You want to use the CBCEncryptionEngine to do this. 您想使用CBCEncryptionEngine来执行此操作。 Here is some sample code from here . 下面是一些示例代码在这里 Note that the IV is randomized on creation, so you don't need to set it or worry about reuse. 请注意,IV在创建时是随机的,因此您无需设置它或担心重用。 Just replace DES with AES here. 这里只需用AES替换DES。

// 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;
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM