AES加密/解密-动态C到C#
我正在兔子微处理器上使用编程语言。我相信DynamicC是基于ANSI-C89的,只是做了一些非常细微的改动。他们有一个AES加密库,我试图在将数据发送到服务器之前使用它对数据进行加密(几乎没有成功) 以下是我正在使用的基于C的AES函数的文档: AESinitStream 语法:void AESinitStream(aestreamstate*state, 常量字符*键, 常量字符*初始向量) 描述:设置流状态结构以开始加密或 解密流。特定的流状态只能是 用于一个方向 参数1:状态-要初始化的AEStreamState结构 PARAMETER2:key—使用空指针的16字节密码密钥将阻止 无法重新计算现有密钥 PARAMETER3:init_vector-表示初始状态的16字节数组 一个反馈寄存器。河流的两端都必须 从相同的初始化向量和键开始 AES数据流 语法:void AES加密流(AESTREAMSTATE*状态, 字符*数据, 整数计数) 描述:加密字节数组 参数1:状态-状态结构 参数2:数据-将进行加密的字节数组 参数3:计数-数据数组的大小 这是我的C代码:AES加密/解密-动态C到C#,c#,c,encryption,aes,dynamic-c,C#,C,Encryption,Aes,Dynamic C,我正在兔子微处理器上使用编程语言。我相信DynamicC是基于ANSI-C89的,只是做了一些非常细微的改动。他们有一个AES加密库,我试图在将数据发送到服务器之前使用它对数据进行加密(几乎没有成功) 以下是我正在使用的基于C的AES函数的文档: AESinitStream 语法:void AESinitStream(aestreamstate*state, 常量字符*键, 常量字符*初始向量) 描述:设置流状态结构以开始加密或 解密流。特定的流状态只能是 用于一个方向 参数1:状态-要初始化的
const char key[] = {'\x41', '\x41', '\x37', '\x44',
'\x44', '\x34', '\x30', '\x33',
'\x30', '\x35', '\x39', '\x4e',
'\x36', '\x37', '\x30', '\x38'};
AESstreamState encrypt_state; //built in Dynamic C type
char init_vector[16];
int i;
int bufLength;
sprintf(Buf, "%s", "testabc");
bufLength = strlen(Buf);
for (i = 0; i < 16; i++)
init_vector[i] = rand() % 255;
printf("Key: ");
for (i = 0; i < sizeof(key); i++)
printf("%d ", key[i]);
printf("\n");
AESinitStream(&encrypt_state, key, init_vector); //built in Dynamic C function
AESencryptStream(&encrypt_state, Buf, bufLength); //built in Dynamic C function
printf("Data: ");
for (i = 0; i < strlen(Buf); i++)
printf("%d ", Buf[i]);
printf("\n");
//set first byte to something that lets the server know it's encrypted
//set 2nd through 16th byte to the IV (initialization vector) so every message will be different even if they have the same contents
for (i = bufLength-1; i >= 0; i--)
Buf[i+17] = Buf[i];
Buf[0] = 237; //φ
printf("IV: ");
for (i = 1; i < 17; i++)
{
printf("%d ", init_vector[i-1]);
Buf[i] = init_vector[i-1];
}
printf("\n");
private string DecryptAES(byte[] cipherText, byte[] IV)
{
byte[] key = {
0x41, 0x41, 0x37, 0x44,
0x44, 0x34, 0x30, 0x33,
0x30, 0x35, 0x39, 0x4e,
0x36, 0x37, 0x30, 0x38
};
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
{
throw new ArgumentNullException("cipherText");
}
else if (IV == null || IV.Length <= 0 || IV.Length != 16)
{
throw new ArgumentNullException("IV");
}
Console.Write("Key: ");
for (int i = 0; i < key.Length; i++)
Console.Write("{0} ", key[i]);
Console.WriteLine();
Console.Write("Data: ");
for (int i = 0; i < cipherText.Length; i++)
Console.Write("{0} ", cipherText[i]);
Console.WriteLine();
Console.Write("IV: ");
for (int i = 0; i < IV.Length; i++)
Console.Write("{0} ", IV[i]);
Console.WriteLine();
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Mode = CipherMode.CFB;
rijAlg.FeedbackSize = 8;
rijAlg.BlockSize = 128;
rijAlg.Padding = PaddingMode.None;
rijAlg.Key = key;
rijAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
string plaintext = null;
plaintext = srDecrypt.ReadToEnd();
Console.WriteLine("Decrypted: " + plaintext);
return plaintext;
}
}
}
}
}
请看在线作为参考
Hex values:
plaintext: 74657374616263
key: 41413744443430333035394e36373038
iv: 000102030405060708090A0B0C0D0E0F
我没有测试以下代码,请在你的电脑上测试
DC(动态C)
您的C AES库使用的是哪种链接模式?此外,您的密文长度没有意义,因为它应该是块长度(16字节)更新的倍数:看起来您也在滥用加密功能。。因为我没有文档,所以不能再多说了。但是对于我来说,你用可怜的
Buf
做什么没有多大意义。我使用的是CFB,它不需要是块长度的倍数(或者至少我认为)。今天晚些时候我将发布文档中的一些示例代码:)密文在AES中总是块大小的倍数。@EugeneSh。加密数据并不总是块大小的倍数,有几种模式不产生块大小的倍数,包括CTR模式。
const char key[16] = {
'\x06', '\xa9', '\x21', '\x40', '\x36', '\xb8', '\xa1', '\x5b',
'\x51', '\x2e', '\x03', '\xd5', '\x34', '\x12', '\x00', '\x06'
};
char bblock[8192];
AESstreamState ass;
memset(bblock, 'A', sizeof(bblock));
AESinitStream(&ass, key, key);
AESencryptStream(&ass, bblock, sizeof(bblock));
AESinitStream(&ass, key, key);
AESdecryptStream(&ass, bblock, sizeof(bblock));
Hex values:
plaintext: 74657374616263
key: 41413744443430333035394e36373038
iv: 000102030405060708090A0B0C0D0E0F
#use AES_CRYPT.LIB
#define AES_CFB_BLOCK_SIZE 16
#define PLAINTEXT_SIZE 7
//41413744443430333035394e36373038
const char key[AES_CFB_BLOCK_SIZE] = {
'\x41', '\x41', '\x37', '\x44', '\x44', '\x34', '\x30', '\x33',
'\x30', '\x35', '\x39', '\x4e', '\x36', '\x37', '\x30', '\x38'
};
//000102030405060708090A0B0C0D0E0F
const char iv[AES_CFB_BLOCK_SIZE] = {
'\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
'\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F'
};
//testabc = 74657374616263
const char plntxt[PLAINTEXT_SIZE] =
{
't', 'e', 's', 't', 'a', 'b', 'c'
};
int main(void) {
auto int i;
auto char text[256];
auto AESstreamState encrypt_state, decrypt_state;
printf("Test case 1 - encrypt aes cfb\n");
AESinitStream(&encrypt_state, key, iv);
memcpy(text, plntxt, sizeof(plntxt));
AESencryptStream(&encrypt_state, text, AES_CFB_BLOCK_SIZE);
printf("Encrypted text:\n");
for (i = 0; i < sizeof(plntxt); i++) {
printf("%02x.", (int) text[i]);
if (0 == ((i+1) % PLAINTEXT_SIZE)) printf("\n");
}
printf("\n");
printf("Test case 2 - decrypt aes cfb \n");
AESinitStream(&decrypt_state, key, iv);
//memcpy(text, cyptxt, sizeof(cyptxt));
AESdecryptStream(&decrypt_state, text, AES_CFB_BLOCK_SIZE);
printf("Decrypted text:\n");
for (i = 0; i < sizeof(cyptxt); i++) {
printf("%02x.", (int) text[i]);
if (0 == ((i+1) % PLAINTEXT_SIZE)) printf("\n");
}
printf("\n");
return 0;
}
using System;
using System.IO;
using System.Security.Cryptography;
namespace TestAES_CFB
{
class Program
{
static byte[] AES_CFB_Encrypt(string plainText, byte[] Key, byte[] IV)
{
byte[] encrypted;
using (Aes aes = Aes.Create())
{
aes.KeySize = 128; // 16 bytes
aes.BlockSize = 128; // 16 bytes
aes.Key = Key;
aes.IV = IV;
aes.Padding = PaddingMode.Zeros;
aes.Mode = CipherMode.CFB;
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
return encrypted;
}
static string AES_CFB_Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
string plaintext = null;
using (Aes aes = Aes.Create())
{
aes.KeySize = 128; // 16 bytes
aes.BlockSize = 128; // 16 bytes
aes.Key = Key;
aes.IV = IV;
aes.Padding = PaddingMode.Zeros;
aes.Mode = CipherMode.CFB;
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
static void Main(string[] args)
{
const int AES_CFB_BLOCK_SIZE = 16;
const int PLAINTEXT_SIZE = 7;
//41413744443430333035394e36373038
byte[] key = new byte[AES_CFB_BLOCK_SIZE] {0x41, 0x41, 0x37, 0x44, 0x44, 0x34, 0x30, 0x33, 0x30, 0x35, 0x39, 0x4e, 0x36, 0x37, 0x30, 0x38};
//000102030405060708090A0B0C0D0E0F
byte[] iv = new byte[AES_CFB_BLOCK_SIZE] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
//testabc = 74657374616263
string plntxt = "testabc";
using (Aes myAes = Aes.Create())
{
byte[] encrypted = AES_CFB_Encrypt(plntxt, key, iv);
string decrypted = AES_CFB_Decrypt(encrypted, key, iv);
Console.WriteLine("Encrypted: {0}", BitConverter.ToString(encrypted));
Console.WriteLine("Decrypted: {0}", decrypted);
Console.ReadLine();
}
}
}
}