【发布时间】:2014-05-13 06:55:35
【问题描述】:
我的目标是得到一个非常简单的分组密码,即 AES 的核心。它必须接受一个键和一个块并返回一个块。解密器应该获取密钥和块并返回原始块。
我一直在使用以下密钥(十六进制)进行测试
AA000000000000000000000000000000
以及下面的纯文本
AA000000000000000000000000000000
(同样是十六进制,是的,与密钥相同)
结果应该是
814827A94525FF24B90F20BEC065866D
确实如此。 (您可以在此处验证这应该是 http://www.cryptogrium.com/aes-encryption-online-ecb.html 的结果,并将其作为输入并选择 AES-128)。
但解密器总是返回
00000000000000000000000000000000
(只有零)。
我在以下实现中做错了什么?此实现仅用于教育目的。这就是我使用 ECB 模式的原因,也是我期望始终使用相同加密的原因。
namespace CryptographyCSharp
{
using System;
using System.Security.Cryptography;
public class MyAes
{
public static string EncryptStringToBytes_Aes(string msg_hex, string key_hex)
{
if (msg_hex == null)
throw new ArgumentNullException("msg_hex");
if (key_hex == null)
throw new ArgumentNullException("key_hex");
byte[] output = new byte[16];
msg_hex = msg_hex.PadRight(32, '0');
key_hex = key_hex.PadRight(32, '0');
using (var aes = Aes.Create("AES"))
{
aes.BlockSize = 128;
aes.KeySize = 128;
aes.Mode = CipherMode.ECB;
if (!aes.ValidKeySize(128))
{
throw new Exception();
}
ICryptoTransform encryptor = aes.CreateEncryptor(key_hex.hex2bytes(), null);
encryptor.TransformBlock(msg_hex.hex2bytes(), 0, 16, output, 0);
encryptor.Dispose();
}
return output.tohex();
}
public static string DecryptStringFromBytes_Aes(string hex_ct, string key_hex)
{
if (hex_ct == null)
throw new ArgumentNullException("cipherText");
if (key_hex == null)
throw new ArgumentNullException("Key");
hex_ct = hex_ct.PadRight(32, '0');
key_hex = key_hex.PadRight(32, '0');
string plaintext = null;
using (Aes aes = Aes.Create("AES"))
{
aes.BlockSize = 128;
aes.KeySize = 128;
aes.Mode = CipherMode.ECB;
if (!aes.ValidKeySize(128))
{
throw new Exception();
}
ICryptoTransform decryptor = aes.CreateDecryptor(key_hex.hex2bytes(), null);
var output = new byte[16];
decryptor.TransformBlock(hex_ct.hex2bytes(), 0, 16, output, 0);
plaintext = output.tohex();
}
return plaintext;
}
}
}
我使用了一些扩展方法,将十六进制转换为字节,反之亦然(即 string.hex2bytes 和 bytes[].tohex)。如果您需要,我可以提供。
【问题讨论】:
-
为什么要编写 PadRight 方法进行解密?如果他们真的在做某事,他们只能弄乱密文。
-
我现在需要它。稍后我将使用 PKCS7,但现在我需要它们。
标签: c# encryption cryptography aes