использовать AesCryptoServiceProvider для расшифровки без IV - PullRequest
3 голосов
/ 14 марта 2012

Я написал приложение BlackBerry, которое использует шифрование AES.Я пытаюсь расшифровать это с помощью AesCryptoServiceProvider в C #.

Код BlackBerry, кажется, не использует IV, что означает, что мне нечего передавать в AesCryptoServiceProvider.

Возможно ли это длямне расшифровать AES без IV, если да, то как?

1 Ответ

1 голос
/ 14 марта 2012

Читая документацию по java-шифрованию, вы не должны напрямую использовать AESEncryptionEngine.Если вы используете его прямо, вы получите (я предполагаю) режим ECB, который приводит к следующему шифрованию изображения пингвина.не делайте этого.

Bad Encryption Скорее, кажется, что для использования какого-либо безопасного режима работы вам нужно использовать обертку вокруг базового движка AESEncrypt / Decrypt.Вы хотите использовать CBCEncryptionEngine для этого.Вот пример кода из здесь .Обратите внимание, что 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;
}    
...