128 位模式的 CBC 加密块大小
CBC Encryption block size for 128 bit mode
我需要使用 AES 和 CBC 以 128 位加密字符串
为此,我需要将块大小设置为什么?
var iv = "0000000000000000000000000000000000000000000000000000000000000000".ToByteArray();
using (Aes myAes = Aes.Create())
{
myAes.Mode = CipherMode.CBC;
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes_Aes(xml, myAes.Key, iv);
// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, iv);
//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", xml);
Console.WriteLine("Round Trip: {0}", roundtrip);
}
奇怪的是我的规格似乎不适合 IV
无论如何我都没有告诉对方 IV 是什么所以我想我将不得不使用一串 0,我认为它有 64 个字符长所以我使用了上面的代码
有人可以帮忙吗?
暂时搁置发送 key/IV 值或将它们初始化为静态值所带来的所有安全概念...
您的代码似乎来自 Microsoft AES documentation,那么为什么不坚持使用生成的 IV 值呢?
如果您绝对想自己设置 IV(不寒而栗),则需要将其设置为 128 位值或 16 字节。我不知道您的 String.ToByteArray()
代码究竟做了什么,但如果我冒险猜测,它可能会使用 UTF8 或 ASCII 等编码将字符串转换为字节。在任何一种情况下,您的 IV 数组的长度都将超过 16 个字节。只需使用 byte[] iv = new byte[16];
之类的东西,它会将所有插槽默认初始化为 0.
但是,既然你提到了,我强烈建议你仔细检查 key/IV 是如何生成或传达给另一方的。
IV 不是密钥。 IV代表初始化向量https://en.wikipedia.org/wiki/Initialization_vector. To generate an IV you can use: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aescryptoserviceprovider.generateiv?view=net-5.0
但是您的代码已经生成了 IV 和 KEY。您的代码生成一个 256 位密钥,而您需要一个 128 位密钥。您可以通过添加
来实现
myAes.KeySize = 128;
紧跟在 Aes.Create()
.
之后
所以应用于 example 你可能开始于:
string original = "Here is some data to encrypt!";
// Create a new instance of the Aes
// class. This generates a new key and initialization
// vector (IV).
using (Aes myAes = Aes.Create())
{
myAes.KeySize = 128; //After this line key size will go to 16 bytes.
// 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);
}
我需要使用 AES 和 CBC 以 128 位加密字符串
为此,我需要将块大小设置为什么?
var iv = "0000000000000000000000000000000000000000000000000000000000000000".ToByteArray();
using (Aes myAes = Aes.Create())
{
myAes.Mode = CipherMode.CBC;
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes_Aes(xml, myAes.Key, iv);
// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, iv);
//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", xml);
Console.WriteLine("Round Trip: {0}", roundtrip);
}
奇怪的是我的规格似乎不适合 IV
无论如何我都没有告诉对方 IV 是什么所以我想我将不得不使用一串 0,我认为它有 64 个字符长所以我使用了上面的代码
有人可以帮忙吗?
暂时搁置发送 key/IV 值或将它们初始化为静态值所带来的所有安全概念...
您的代码似乎来自 Microsoft AES documentation,那么为什么不坚持使用生成的 IV 值呢?
如果您绝对想自己设置 IV(不寒而栗),则需要将其设置为 128 位值或 16 字节。我不知道您的 String.ToByteArray()
代码究竟做了什么,但如果我冒险猜测,它可能会使用 UTF8 或 ASCII 等编码将字符串转换为字节。在任何一种情况下,您的 IV 数组的长度都将超过 16 个字节。只需使用 byte[] iv = new byte[16];
之类的东西,它会将所有插槽默认初始化为 0.
但是,既然你提到了,我强烈建议你仔细检查 key/IV 是如何生成或传达给另一方的。
IV 不是密钥。 IV代表初始化向量https://en.wikipedia.org/wiki/Initialization_vector. To generate an IV you can use: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aescryptoserviceprovider.generateiv?view=net-5.0
但是您的代码已经生成了 IV 和 KEY。您的代码生成一个 256 位密钥,而您需要一个 128 位密钥。您可以通过添加
来实现myAes.KeySize = 128;
紧跟在 Aes.Create()
.
所以应用于 example 你可能开始于:
string original = "Here is some data to encrypt!";
// Create a new instance of the Aes
// class. This generates a new key and initialization
// vector (IV).
using (Aes myAes = Aes.Create())
{
myAes.KeySize = 128; //After this line key size will go to 16 bytes.
// 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);
}