在Perl中加密文件并在c#中解密

时间:2011-11-14 11:19:32

标签: c# .net perl encryption rijndael

我正在尝试使用Perl加密文本文件,然后使用C#编写的其他应用程序对其进行解密。 这是我的Perl代码:

use strict;
use Crypt::CBC;

my $ifh;
my $ofh;
my $line;

my $cipher = Crypt::CBC->new(
    {
        'key'         => 'length16length16',
        'cipher'      => 'Rijndael',
        'iv'          => 'length16length16',
        'literal_key' => 1,
        'header'      => 'none',
    'padding'     => 'null',
        'keysize'     => 128 / 8
    }
);

open($ifh,'<', $infile)
            or die "Can't open $infile for encryption input: $!\n";
open($ofh, '>', $outfile)
        or die "Can't open $outfile for encryption output: $!\n";

$cipher->start('encrypting');

for $line (<$ifh>) {
    print $ofh $cipher->crypt($line);
  }

print $ofh $cipher->finish;

close($ifh)
    or die "Error closing input file: $!\n";

close($ofh)
    or die "Error closing output file: $!\n";

我的C#解密代码:

    RijndaelManaged myRijndael = new System.Security.Cryptography.RijndaelManaged();
    myRijndael.Key = System.Text.Encoding.UTF8.GetBytes("length16length16");
    myRijndael.IV = System.Text.Encoding.UTF8->GetBytes("length16length16");
    myRijndael.Mode = CipherMode.CBC;
    myRijndael.Padding = PaddingMode.None;

    // Create a decryptor to perform the stream transform.
    ICryptoTransform decryptor = myRijndael.CreateDecryptor(myRijndael.Key, myRijndael.IV);

    //Create the streams used for decryption.
    FileStream file = File.OpenRead(strInFile);
    CryptoStream csDecrypt = new CryptoStream(file, decryptor, CryptoStreamMode.Read);
    StreamReader srDecrypt = new StreamReader(csDecrypt);

    // Read the decrypted bytes from the decrypting stream
    string decryptedText = srDecrypt.ReadToEnd();

我一直在

  

System.Security.Cryptography.CryptographicException:长度   要解密的数据无效

当我尝试一次读取几个字节的数据时,我注意到前100个字节正确解密,但其余的只是垃圾。

BTW,我可以使用Perl解密加密文件:

$cipher->start('decrypting');

那么我对C#和Perl做错了什么?

编辑:我尝试关注@munissor建议并更改C#代码以使用

  

PaddingMode.Zeros

但我仍然得到同样的例外。 请帮忙......

2 个答案:

答案 0 :(得分:3)

如果你检查CPAN documentation for Crypt::CBC它表示“null”填充用零填充块。所以我认为你应该在C#端使用PaddingMode.Zeros

答案 1 :(得分:1)

找到解决方案!!!

在Perl中,我必须在打开输出文件后添加:

  

binmode $ ofh;

填充建议很有帮助,但最后我省略了padding指令,并在Perl和C#中使用了默认的PKCS7。

我的最终Perl代码如下所示:

use strict;
use Crypt::CBC;

my $ifh;
my $ofh;

my $cipher = Crypt::CBC->new(
    {
      'key'         => 'length16length16',
      'cipher'      => 'Rijndael',
      'iv'          => 'length16length16',
      'literal_key' => 1,
      'header'      => 'none',
      'keysize'     => 128 / 8
    }
);

#open input and output file
open($ifh,'<', $infile)
        or die "Can't open $infile for encryption input: $!\n";
open($ofh, '>', $outfile)
    or die "Can't open $outfile for encryption output: $!\n";
binmode &ofh;

$cipher->start('encrypting');

#write encrypted data to output file
while (read($ifh,my $buffer,1024)) 
{
    print $ofh $cipher->crypt($buffer);
} 

print $ofh $cipher->finish;

#close file handles
close($ifh)
    or die "Error closing input file: $!\n";
close($ofh)
    or die "Error closing output file: $!\n";

和C#部分:

RijndaelManaged myRijndael = new System.Security.Cryptography.RijndaelManaged();
myRijndael.Key = System.Text.Encoding.UTF8.GetBytes("length16length16");
myRijndael.IV = System.Text.Encoding.UTF8->GetBytes("length16length16");
myRijndael.Mode = CipherMode.CBC;

// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = myRijndael.CreateDecryptor(myRijndael.Key, myRijndael.IV);

//Create the streams used for decryption.
FileStream file = File.OpenRead(strInFile);
CryptoStream csDecrypt = new CryptoStream(file, decryptor, CryptoStreamMode.Read);
StreamReader srDecrypt = new StreamReader(csDecrypt);

// Read the decrypted bytes from the decrypting stream
string decryptedText = srDecrypt.ReadToEnd();