谁解释一下 AES 加密算法的问题啊?
怎么 和我们 平时用的加密总是不一样呢?
问题一:怎么有Key和IV 两样东西?
问题二:怎么Key和IV是自动生成的吗,那意义何在呢?
问题三:我们平时日常应用的话,不就一个 password就好了吗。。。
谁介绍一下?
复制粘贴也可以,关键要清晰,清楚。。。
[最优解释]
IV是分组加密的中的内容。。不是自动生成的。
AES是分组加密的一种。
分组加密时,整个组加密成一组数据。由于分组长度一般很小,所以大数据会分成很多小组。每个小组分别使用Key加密,加密出另一个小组数据。 然后小组数据的拼接用IV(初始向量:由这个名字你就猜得到向量可能会不断变化)进行混乱化。。
我就是这么理解的。
[其他解释]
Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null
[其他解释]
可是MSDN上的例子,Key和IV都是自动生成的啊
其实,我就是很简单的需求,
加密的时候输入password和源字符串,得到加密的字符串,
解密的时候输入password和加密字符串,得到原字符串。
下面是MSDN上的例子,但是我觉得都很困惑。。。好像对我来说没有什么用处。。。
using System;
using System.IO;
using System.Security.Cryptography;
namespace Aes_Example
{
class AesExample
{
public static void Main()
{
try
{
string original = "Here is some data to encrypt!";
// Create a new instance of the AesCryptoServiceProvider
// class. This generates a new key and initialization
// vector (IV).
using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider())
{
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
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
[其他解释]
IV.Length <= 0)
throw new ArgumentNullException("Key");
byte[] encrypted;
// Create an AesCryptoServiceProvider object
// with the specified key and IV.
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
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.
using (MemoryStream 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);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null