Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解密AES加密文件的选择性部分_C#_.net_Encryption_Cryptography_Aes - Fatal编程技术网

C# 解密AES加密文件的选择性部分

C# 解密AES加密文件的选择性部分,c#,.net,encryption,cryptography,aes,C#,.net,Encryption,Cryptography,Aes,我有几个大的加密文件使用的代码从 这是加密代码: BUF_LENGTH = 16 FileStream FsInput = new FileStream(InFileName, FileMode.Open, FileAccess.Read); FileStream FsOutput = new FileStream(OutFileName, FileMode.OpenOrCreate, FileAccess.Write); byte[] ReadBuffer = new byte[BUF_LEN

我有几个大的加密文件使用的代码从

这是加密代码:

BUF_LENGTH = 16
FileStream FsInput = new FileStream(InFileName, FileMode.Open, FileAccess.Read);
FileStream FsOutput = new FileStream(OutFileName, FileMode.OpenOrCreate, FileAccess.Write);
byte[] ReadBuffer = new byte[BUF_LENGTH];
long InFileLength = FsInput.Length;
long AllBytes = 0;
int PartBytes = 0;

if (InFileLength == 0)
    return false;

ICryptoTransform AESEncryptor = AESEncoder.CreateEncryptor(KeyBytes, null);
CrStream = new CryptoStream(FsOutput, AESEncryptor, CryptoStreamMode.Write);

while (AllBytes < InFileLength)
{
    PartBytes = FsInput.Read(ReadBuffer, 0, BUF_LENGTH);
    CrStream.Write(ReadBuffer, 0, PartBytes);
    AllBytes = AllBytes + PartBytes;
}
CrStream.Close();
FsInput.Close();
FsOutput.Flush();
FsOutput.Close();
我编写了这段代码,但它只在将
pos
变量保持为0时起作用。如果我将其设置为其他值,如512,则它不起作用。
有人知道我应该去哪里吗?

谢谢

因为。对于CTR模式,您还需要将计数器设置为pos。我认为汉斯·沃尔夫在他的代码中考虑了这一部分。您能在这里验证一下吗:?我想您可以使用他的
递增计数器
功能,这就是您所需要的。谢谢!成功了!把它作为答案贴出来。我会投票的,因为。对于CTR模式,您还需要将计数器设置为pos。我认为汉斯·沃尔夫在他的代码中考虑了这一部分。您能在这里验证一下吗:?我想您可以使用他的
递增计数器
功能,这就是您所需要的。谢谢!成功了!把它作为答案贴出来。我会投票的。
private byte[] read_block(String filepath, long pos, int length)
{
    byte[] data = readfile(filepath, pos, length); // returns the specific block.
    byte[] plaindata = new byte[data.Length];

    byte[] kb = PrepareKeyIvBytes();
    byte[] nba = new byte[IvBytes.Length];
    IvBytes.CopyTo(nba, 0);
    AESDecoder = new Aes128CounterMode(nba);
    ICryptoTransform AESDecryptor = AESDecoder.CreateDecryptor(IvBytes, null);
    MemoryStream ms = new MemoryStream();
    CrStream = new CryptoStream(ms, AESDecryptor, CryptoStreamMode.Write);

    CrStream.Write(data, 0, length);
    CrStream.Close();
    ms.Flush();

    ms.ToArray().CopyTo(plaindata, 0);
    return plaindata;
}