我使用的是AES方法在这里: HTTP ://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx

我想有一个字符串值,我将转换为字节数组,并将它传递到AES加密方法。字符串应该多少个字符是产生该方法需要正确的字节数组的大小?

static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");

        // Declare the stream used to encrypt to an in memory
        // array of bytes.
        MemoryStream msEncrypt = null;

        // Declare the RijndaelManaged object
        // used to encrypt the data.
        RijndaelManaged aesAlg = null;

        try
        {
            // Create a RijndaelManaged object
            // with the specified key and IV.
            aesAlg = new RijndaelManaged();
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption.
            msEncrypt = new MemoryStream();
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {

                    //Write all data to the stream.
                    swEncrypt.Write(plainText);
                }
            }

        }
        finally
        {

            // Clear the RijndaelManaged object.
            if (aesAlg != null)
                aesAlg.Clear();
        }

        // Return the encrypted bytes from the memory stream.
        return msEncrypt.ToArray();

    }
有帮助吗?

解决方案

明文的大小没有关系。只要确保你使用完全相同的IV和密钥与在decryptStringFromBytes_AES加密字节(字节[]密文,字节[]键,字节[] IV)方法一起。这将返回到你所输入的纯文本。

例如:


string plain_text = "Cool this works";
byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                                           0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
byte[] key = new byte[] { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                                           0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
byte[] encrytped_text = encryptStringToBytes_AES(plain_text, key, iv);
string plain_text_again = decryptStringFromBytes_AES(encrypted_text, key, iv);

在这里,你应该看到,纯文本和纯文本,再次是相同的。现在继续改变plain_text到任何你想要的,看看这工作得很好。

有RijndaelManaged的缺省值是:结果 块大小:128次点击 密钥大小:256点击 模式:CipherMode.CBC结果 填充:PaddingMode.PKCS7结果

在有效IV大小为:结果 128,192,256位(这是块大小,请务必将其设置为您正在使用尺寸IV)点击 有效密钥长度:点击 128,192,256位(这是密钥大小,确保其设置为要使用的大小键),点击

这意味着,字节[] IV可以是16,24,或32个字节(在我的上述示例中的16个字节)和字节[]键也可以是16,24,或32个字节(在我的上述例子中其16个字节)。

希望有所帮助。

其他提示

您需要为填充。实际上,你链接的页面有一个例如在填充(在C ++)。

通过填补处理,可以加密非标准块大小。

不要字符串转换成它的Unicode字节表示。这将是太硬,以检查正确的长度,并不能提供足够的随机化。

您可以执行以下操作:使用密钥导出函数。你想对函数的输入一个固定长度的字节数组。这是 Rfc2898 是最好在

因此,创建一个新的对象Rfc2898:

using PBKDF2 = System.Security.Cryptography.Rfc2898DeriveBytes;

class Example {
    byte[] mySalt = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };

    void Initialize( string password ) {
        PBKDF2 kdf = new PBKDF2( password, mySalt );
        // Then you have your algorithm
        // When you need a key: use:
        byte[] key = kdf.GetBytes( 16 ); // for a 128-bit key (16*8=128)

        // You can specify how many bytes you need. Same for IV.
        byte[] iv = kdf.GetBytes( 16 ); // 128 bits again.

        // And then call your constructor, etc.
        // ...
    }
}

有关如何我用这个例子,采用Rijndael算法退房我的项目。我有密码步骤,其中我有一个字符串并且使用上述方法得到的密钥和IV字节数组。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top