C# 如何在.Net中加密AES/ECB/128消息?
从这个站点获取了向量 在javascript(sjcl)中也有相同的结果C# 如何在.Net中加密AES/ECB/128消息?,c#,encryption,aes,C#,Encryption,Aes,从这个站点获取了向量 在javascript(sjcl)中也有相同的结果 var key = [0x2b7e1516,0x28aed2a6,0xabf71588,0x09cf4f3c]; var test = [0x6bc1bee2,0x2e409f96,0xe93d7e11,0x7393172a]; aes = new sjcl.cipher.aes(key); r = aes.encrypt(test); console.log(r); 但是我在C里够不着# [TestMethod] p
var key = [0x2b7e1516,0x28aed2a6,0xabf71588,0x09cf4f3c];
var test = [0x6bc1bee2,0x2e409f96,0xe93d7e11,0x7393172a];
aes = new sjcl.cipher.aes(key);
r = aes.encrypt(test);
console.log(r);
但是我在C里够不着#
[TestMethod]
public void EncryptIntsToInts()
{
Int32[]键={unchecked((Int32)0x2b7e1516)、0x28aed2a6、unchecked((Int32)0xabf71588)、0x09cf4f3c};
Int32[]test={0x6bc1bee2,0x2e409f96,未选中((Int32)0xe93d7e11),0x7393172a};
Int32[]答案={0x3ad77bb4,0x0d7a3660,未选中((Int32)0xa89ecaf3),0x2466ef97};
var r=AES.EncryptIntsToInts(test,key.ToByteArray());
Assert.IsTrue(r.SequenceEqual(答案));
}
静态字节[]零IV=新字节[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
公共静态Int32[]EncryptIntsToInts(Int32[]输入,字节[]键)
{
//检查参数。
如果(input==null | | input.Length您使用32位整数来定义键。当您将它们转换为字节时,您使用本机的尾数,通常是小尾数。因此,您的键是16157e2b a6…
而不是2b7e1516 28…
首先,我不会用int
s来表示一个键,但是如果你真的想,可以编写一个big-endian转换函数
我还强烈建议您不要使用ECB模式。您可以将CBC与HMAC一起使用(采用先加密后mac的结构),或者使用第三方库来实现GCM。正确的代码(只需添加一个尝试使用/):
为什么是ECB?这是一个非常弱的模式。外部程序的要求在旁边注:这是基于德克萨斯仪器AES 128 C实现的AES 128位ECB最简单的C实现:区别在于填充模式。零
不是。无
?或者我错过了其他东西吗?@Rup看起来现在他没有使用坏代码n键转换了。@CodesInChaos啊,是的,很好。(我真的是在提醒SkyN自己解释这个问题,所以可能值得投赞成票。)
[TestMethod]
public void EncryptIntsToInts()
{
Int32[] key = { unchecked((Int32)0x2b7e1516), 0x28aed2a6, unchecked((Int32)0xabf71588), 0x09cf4f3c };
Int32[] test = { 0x6bc1bee2,0x2e409f96,unchecked((Int32)0xe93d7e11),0x7393172a };
Int32[] answer = { 0x3ad77bb4, 0x0d7a3660, unchecked((Int32)0xa89ecaf3), 0x2466ef97 };
var r = AES.EncryptIntsToInts(test, key.ToByteArray());
Assert.IsTrue(r.SequenceEqual(answer));
}
static byte[] zeroIV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
public static Int32[] EncryptIntsToInts(Int32[] input, byte[] key)
{
// Check arguments.
if (input == null || input.Length <= 0)
throw new ArgumentNullException("input");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
// Declare the RijndaelManaged object
// used to encrypt the data.
RijndaelManaged aesAlg = null;
byte[] bResult;
try
{
aesAlg = new RijndaelManaged
{
Key = key,
Mode = CipherMode.ECB,
Padding = PaddingMode.None,
KeySize = 128,
BlockSize = 128,
IV = zeroIV
};
ICryptoTransform encryptor = aesAlg.CreateEncryptor();
byte[] bInput = new byte[input.Length * sizeof(int)];
Buffer.BlockCopy(input, 0, bInput, 0, bInput.Length);
bResult = encryptor.TransformFinalBlock(bInput, 0, input.Length);
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
}
int[] iResult = new int[bResult.Length / sizeof(int)];
Buffer.BlockCopy(bResult, 0, iResult, 0, bResult.Length);
return iResult;
}
[TestMethod]
public void EncryptIntsToInts()
{
byte[] key = "2b7e151628aed2a6abf7158809cf4f3c".HEX2Bytes();
byte[] test = "6bc1bee22e409f96e93d7e117393172a".HEX2Bytes();
byte[] answer = "3ad77bb40d7a3660a89ecaf32466ef97".HEX2Bytes();
RijndaelManaged aesAlg = new RijndaelManaged
{
Key = key,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7,
KeySize = 128,
BlockSize = 128,
IV = zeroIV
};
ICryptoTransform encryptor = aesAlg.CreateEncryptor();
var r = encryptor.TransformFinalBlock(test, 0, test.Length);
Assert.IsTrue(r.SequenceEqual(answer));
}
public static byte[] HEX2Bytes(this string hex)
{
if (hex.Length%2 != 0)
{
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture,
"The binary key cannot have an odd number of digits: {0}", hex));
}
byte[] HexAsBytes = new byte[hex.Length/2];
for (int index = 0; index < HexAsBytes.Length; index++)
{
string byteValue = hex.Substring(index*2, 2);
HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
return HexAsBytes;
}
static byte[] zeroIV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
[TestMethod]
public void EncryptIntsToInts()
{
byte[] key = "2b7e151628aed2a6abf7158809cf4f3c".HEX2Bytes();
byte[] test = "6bc1bee22e409f96e93d7e117393172a".HEX2Bytes();
byte[] answer = "3ad77bb40d7a3660a89ecaf32466ef97".HEX2Bytes();
var r = AES.Encrypt(test, key);
Assert.IsTrue(answer.SequenceEqual(r));
}
public static byte[] Encrypt(byte[] input, byte[] key)
{
var aesAlg = new AesManaged
{
KeySize = 128,
Key = key,
BlockSize = 128,
Mode = CipherMode.ECB,
Padding = PaddingMode.Zeros,
IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
return encryptor.TransformFinalBlock(input, 0, input.Length);
}