首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在没有IV的情况下使用AesCryptoServiceProvider进行解密

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

Stack Overflow用户
提问于 2012-03-14 18:33:19
回答 1查看 3.3K关注 0票数 3

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

BlackBerry代码似乎没有使用IV,这意味着我没有什么可以传递给AesCryptoServiceProvider的。

我有没有可能在没有静脉输液器的情况下解密AES,如果可能,如何解密?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-15 02:48:18

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

相反,要使用某种安全的操作模式,您实际上需要在基本的AESEncrypt /解密引擎周围使用包装器。您希望使用CBCEncryptionEngine来完成此操作。以下是来自here的一些示例代码。请注意,IV在创建时是随机的,因此您不需要设置它或担心重用。只需在此处用AES替换DES即可。

代码语言:javascript
复制
// 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9700215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档