为我的 AES 方法创建字节数组需要多少个字符?
我在这里使用 AES 方法: http:// msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanagement.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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
纯文本的大小并不重要。 只需确保在解密字符串FromBytes_AES(byte[] cipherText, byte[] Key, byte[] IV) 方法中使用完全相同的 IV 和密钥以及加密字节即可。 这将返回给您输入的纯文本。
例如:
在这里您应该看到 plain-text 和 plain-text-again 是相同的。 现在继续将 plain_text 更改为您想要的任何内容,看看效果是否正常。
RijndaelManaged 的默认值为:
块大小:128
密钥大小:256
模式:CipherMode.CBC
填充:PaddingMode.PKCS7
有效的 IV 大小为:
128、192、256 位(这是 BlockSize,请确保将其设置为您正在使用的大小 IV)
有效的密钥大小为:
128、192、256 位(这是 KeySize,请确保将其设置为您正在使用的密钥大小)
这意味着 byte[] iv 可以是 16、24 或 32 字节(在我上面的示例中)它是 16 个字节),并且 byte[] 键也可以是 16、24 或 32 个字节(在我上面的示例中是 16 个字节)。
希望有帮助。
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:
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.
为此你需要填充。 实际上,您链接的页面有一个关于填充的示例(用 C++ 编写)。
使用填充,您可以加密非标准块大小。
You need padding for that. Actually, the page you linked have an example on padding (in C++).
With paddings, you can encrypt non standard block sizes.
不要将字符串转换为其 Unicode 字节表示形式。 很难检查正确的长度,并且无法提供足够的随机化。
您可以执行以下操作:使用密钥派生函数。 您需要一个固定长度的字节数组作为函数的输入。 这就是 Rfc2898 最擅长。
因此,创建一个新的 Rfc2898 对象:
有关我如何使用它的示例,请查看 我的项目使用 Rijndael。 我有一个密码步骤,我使用上述方法获取字符串并获取密钥和 iv 字节数组。
Do not convert a string into its Unicode byte representation. It will be too hard to check for the right length, and not provide enough randomization.
You could do the following: use a key derivation function. You want a fixed length byte array for the input of the function. This is what Rfc2898 is best at.
So, create a new Rfc2898 object:
For an example of how I've used this, check out my project using Rijndael. I have a password step where I take a string and get the key and iv byte arrays using the above method.