多少个字符来创建为我的AES方法的字节数组? [英] How many characters to create a byte array for my AES method?

查看:144
本文介绍了多少个字符来创建为我的AES方法的字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



 静态的byte [] encryptStringToBytes_AES(串明文的byte []键,字节[] IV)
{
//检查参数。
如果(明文== NULL || plainText.Length< = 0)
抛出新的ArgumentNullException(明文);
如果(主要== NULL || Key.Length< = 0)
抛出新的ArgumentNullException(钥匙);
如果(IV == NULL || IV.Length< = 0)
抛出新的ArgumentNullException(钥匙);

//声明用于加密到内存中
//数组中的字节流。
MemoryStream的msEncrypt = NULL;

//声明RijndaelManaged的对象
//用于对数据进行加密。
RijndaelManaged的aesAlg = NULL;


{
//创建一个对象RijndaelManaged的
//使用指定的密钥和IV。
aesAlg =新RijndaelManaged的();
aesAlg.Key =密钥;
aesAlg.IV = IV;

//创建一个decrytor执行流转换。
ICryptoTransform的加密= aesAlg.CreateEncryptor(aesAlg.Key,aesAlg.IV);

//创建用于加密的数据流。
msEncrypt =新的MemoryStream();
使用(CryptoStream的csEncrypt =新的CryptoStream(msEncrypt,加密,CryptoStreamMode.Write))使用(StreamWriter的swEncrypt =新的StreamWriter(csEncrypt))

{
{

//写的所有数据流。
swEncrypt.Write(明文);
}
}

}
终于
{

//清除RijndaelManaged的对象。如果
(!aesAlg = NULL)
aesAlg.Clear();
}

//从内存流返回加密的字节。
返回msEncrypt.ToArray();

}


解决方案

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



例如:

  
串plain_text =酷这个工程;
的byte [] IV =新的字节[] {0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07,
0x08的,为0x09,0x0A的,0x0B中,的0x0C,0X0D,为0x0E,为0x0F} ;
的byte []键=新的字节[] {0x00时,为0x11,0x22,0x33,0x44进行,将0x55,0x66,0x77,
均为0x88,0x99,和0xAA,为0xBB,含有0xCC,0xDD,0xEE,0xFF的} ;
字节[] = encrytped_text encryptStringToBytes_AES(plain_text,钥匙,IV);
串plain_text_again = decryptStringFromBytes_AES(encrypted_text,钥匙,IV);



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



有关RijndaelManaged的默认值是:结果
块大小:128 < BR>
密钥长度:256结果
模式:CipherMode.CBC结果
填充:PaddingMode.PKCS7结果



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



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



希望有所帮助。


I am using the AES methods here: http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged.aspx

I want to have a string value that I will convert to byte array and pass it to the AES encrypt method. How many characters should the string be to produce the correct byte array size that the method expects?

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();

    }

解决方案

The size of the plain text does not matter. Just make sure you use the exact same IV and Key along with the encrypted bytes in the decryptStringFromBytes_AES(byte[] cipherText, byte[] Key, byte[] IV) method. That will return back to you the entered plain text.

For example:


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);

Here you should see that plain-text and plain-text-again are the same. Now go ahead and change plain_text to anything you want and see that this works fine.

The default values for RijndaelManaged are:
BlockSize: 128
KeySize: 256
Mode: CipherMode.CBC
Padding: PaddingMode.PKCS7

The valid IV sizes are:
128, 192, 256 bits (This is the BlockSize, make sure to set it to size IV you are using)
The valid Key sizes are:
128, 192, 256 bits (This is the KeySize, make sure to set it to the size key you are using)

This means that the byte[] iv can be 16, 24, or 32 bytes (in my above example its 16 bytes) and the byte[] key can also be 16, 24, or 32 bytes (in my above example its 16 bytes).

Hope that helps.

这篇关于多少个字符来创建为我的AES方法的字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆