加密与加密在.NET中使用OpenSSL解密文件 [英] Encrypting & Decrypting files using OpenSSL in .NET

查看:126
本文介绍了加密与加密在.NET中使用OpenSSL解密文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#项目中使用OpenSSL加密库来加密/解密文件.这是我的代码:

I'm using the OpenSSL Crypto library in my C# project to encrypt/decrypt files. Here is my code :

byte[] key = System.Text.Encoding.ASCII.GetBytes("password");

byte[] iv = System.Text.Encoding.ASCII.GetBytes("1234");


OpenSSL.Crypto.CipherContext cc = new OpenSSL.Crypto.CipherContext(
    OpenSSL.Crypto.Cipher.AES_256_ECB);

FileStream fIn = new FileStream("C:\\file.txt", FileMode.Open, 
    FileAccess.Read);
FileStream fOut = new FileStream("C:\\encrypted.txt", FileMode.OpenOrCreate,
    FileAccess.Write);
fOut.SetLength(0);

byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fIn.Length;
int len;

DateTime start = DateTime.Now;
while (rdlen < totlen)
{
    // argument 1
    len = fIn.Read(bin, 0, 100);         
    // argument 2
    fOut.Write(cc.Crypt(bin,key,iv,true),0,100);                 
    rdlen = rdlen + len;
}

fOut.Flush();  
fOut.Close();
fIn.Close();

结果我得到了这个异常:

As a result I got this exception:

偏移量和长度超出范围 对于数组或计数大于 从索引到的元素数量 源集合的末尾.

Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.

当我将参数1和2的值从100更改为64时(bin始终始终为byte [100]) 它可以正常工作,对该文件进行了加密和解密,但是解密后的文件的大小大于原始文件的大小,并且在文本文件的末尾包含了1或2行.

When I changed the values of argument 1 and 2 from 100 to 64 (bin still always byte[100]) it worked, the file was encrypted and decrypted but the size of the decrypted file was bigger than the original one and contained 1 or 2 more lines at the end of the text file.

推荐答案

我不知道该库,但是这里的一个问题是您正在使用256位= 32字节的块大小来加密100字节的块.您的块应为32字节的倍数.文件末尾的多余字节可能只是将最后一个块四舍五入为32个字节.

I don't know the library but one problem here is that you're encrypting chunks of 100 bytes with a 256-bit = 32 byte block size. Your chunks should be multiples of 32 bytes. The extra bytes at the end of the file are likely just rounding the final block up to 32 bytes.

就像Philip的回答一样,崩溃的可能原因是Write中的硬编码100. Crypt函数将从要加密的最终块中恢复32、64或96字节之一,这些字节都不足100.(在有效的100字节情况下,您的数据可能会被填充为128字节加密,因此当您只写100时,您将丢失最后一个块的最后28个字节.)

As in Philip's answer the likely cause of the crash is the hard-coded 100 in the Write, though. The Crypt function will be returing one of 32, 64 or 96 bytes from the final block it's encrypting, which are all short of 100. (In the 100 byte case that works, chances are your data is being padded out to 128 bytes encrypted and so you're losing the last 28 bytes of the last block when you only write 100.)

  1. 您正在以ECB模式通过IV-您不需要这个
  2. 通过反复调用Crypt,您大概每100个字节进行一次密钥设置.这是低效率的;您只需在加密开始时执行一次.您应该寻找一种使用密钥初始化类的方法(在其他模式下为IV),然后将其提供给数据块以立即进行加密,而不是每次都使用密钥来调用Crypt.我不知道这在这个库中会是什么,但是它应该存在.就目前情况而言,您也不能使用CBC或任何类似的模式,因为您将每100个字节而不是一次写入IV,并且不要在相邻的100个字节块之间链接最后一个块.
  3. 如果要使用Crypt,为什么不立即将文件加载到内存中呢?我意识到这不会扩展到千兆字节的数据,但是在您的正常使用情况下可能会实现.或者至少选择更大的数据大小,例如256k.但是,如果您超过一个区块,仍然会遇到重复的密钥设置/CBC损坏的情况.

这篇关于加密与加密在.NET中使用OpenSSL解密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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