Use AesCryptoServiceProvider to decrypt without IV

I wrote a BlackBerry app that uses AES encryption. I am trying to decode this with the AesCryptoServiceProvider in C #.

The BlackBerry code doesn't seem to use an IV, which means I have nothing to pass to the AesCryptoServiceProvider.

Is it possible to decrypt AES without IV, if yes, how?

+3


source to share


1 answer


Reading the jQuery BlackBerry crypto documentation, it seems like you shouldn't be using AESEncryptionEngine directly. If you use it directly, you end up with (I assume) ECB mode, which results in the following encryption of the penguin image. do not do this.

Bad encryption Rather it seems that in order to use some protected mode of operation, you need to actually use a wrapper around the main AESEncrypt / Decrypt mechanism. You want to use CBCEncryptionEngine for this . Here's some sample code here . Note that the IV is randomized on creation, so you don't need to install it or worry about reusing it. Just replace DES with AES here.



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

      

+1


source







All Articles