CFB中的C#AES加密,其中明文长度等于加密长度 [英] C# AES Encryption in CFB Where Plaintext Length Equals Encrypted Length

查看:206
本文介绍了CFB中的C#AES加密,其中明文长度等于加密长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的数据格式,它的一部分加密在CFB模式下似乎是AES。明文数据长度和加密的数据长度是相同的。



在C#中,我所采取的每一个角度似乎都希望加密长度是块大小...所以我得到一个异常尝试解密数据。



在研究解决方案中,我使用了Crypto ++,并写了一个快速的C ++应用程序,成功解密数据,所以我很确定我使用正确的算法,关键和IV。这样做很好,但是如果可以的话,我想保留C#中的所有内容。任何建议?



下面的工作C ++代码:

  // define键
unsigned char key [16];
// populate key
// ...


// define iv
unsigned char iv [16];
// populate iv
// ...

std :: ifstream inFile;

//打开文件
inFile.open(file.aes,ios :: binary);

//获取文件大小
inFile.seekg(0,ios :: end);
int fileSize =(int)inFile.tellg();
inFile.seekg(offset,ios :: beg);

//读/关闭文件
char * inBytes = new char [fileSize];
inFile.read(inBytes,fileSize);
inFile.close();

//配置解密
CFB_Mode< AES> ::解密cfbDecryption(key,16,iv);

//填充输出字节
char * outBytes = new char [fileSize];
cfbDecryption.ProcessData((byte *)outBytes,(byte *)inBytes,fileSize);

//打开/写/关闭输出文件
std :: ofstream outFile;
outFile.open(out.dec);
outFile.write(outBytes,fileSize);
outFile.close();

删除[] inBytes;


解决方案

我重新尝试使用cryptlib,它解决了我的问题...代码如下:

 使用cryptlib; 

byte [] key = new byte [16] {...这里的关键字节...};

byte [] iv =新字节[16] {... iv bytes here ...};

byte [] enc; // ciphertext bytes(我从一个filestream填充它们)

crypt.Init();
int cryptContext = crypt.CreateContext(crypt.UNUSED,crypt.ALGO_AES);
crypt.SetAttribute(cryptContext,crypt.CTXINFO_MODE,crypt.MODE_CFB);
crypt.SetAttributeString(cryptContext,crypt.CTXINFO_KEY,key,0,16);
crypt.SetAttributeString(cryptContext,crypt.CTXINFO_IV,iv,0,16);
crypt.Decrypt(cryptContext,enc); //用明文字节替换的密文字节
crypt.DestroyContext(cryptContext);


I have an existing data format that has portions of it encrypted in what appears to be AES in CFB mode. The plaintext data length and the encrypted data length are the same.

In C#, pretty much every angle I've taken seems to expect the encrypted length to be a multiple of the block size... so I get an exception trying to decrypt the data.

In researching solutions, I've used Crypto++ and wrote a quick C++ app that successfully decrypts the data, so I'm pretty sure I'm using the right algorithm, key and IV. This works fine, but I'd like to keep everything inside C# if at all possible. Any suggestions?

Working C++ code below:

//define key
unsigned char key[16];
//populate key
//...


//define iv
unsigned char iv[16];
//populate iv
//...

std::ifstream inFile;

//open file
inFile.open("file.aes",ios::binary );

//get file size
inFile.seekg(0,ios::end);
int fileSize = (int) inFile.tellg();
inFile.seekg(offset, ios::beg);

//read/close file
char* inBytes = new char[fileSize];
inFile.read(inBytes,fileSize);
inFile.close();

//configure decryption
CFB_Mode<AES>::Decryption cfbDecryption(key, 16, iv);

//populate output bytes
char* outBytes = new char[fileSize];
cfbDecryption.ProcessData((byte*) outBytes,(byte*) inBytes,fileSize);

//open/write/close output file
std::ofstream outFile;
outFile.open("out.dec");
outFile.write(outBytes,fileSize);
outFile.close();

delete[] inBytes;

解决方案

I revisited trying to use cryptlib and it solved my problem... code is below:

using cryptlib;

byte[] key = new byte[16] {...key bytes here...};

byte[] iv =  new byte[16] {...iv bytes here...};

byte[] enc;  //ciphertext bytes (i populated them from a filestream)

crypt.Init();
int cryptContext = crypt.CreateContext(crypt.UNUSED, crypt.ALGO_AES);
crypt.SetAttribute(cryptContext, crypt.CTXINFO_MODE, crypt.MODE_CFB);
crypt.SetAttributeString(cryptContext, crypt.CTXINFO_KEY, key, 0, 16);
crypt.SetAttributeString(cryptContext, crypt.CTXINFO_IV, iv, 0, 16);
crypt.Decrypt(cryptContext, enc);   //ciphertext bytes replaced with plaintext bytes
crypt.DestroyContext(cryptContext);

这篇关于CFB中的C#AES加密,其中明文长度等于加密长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆