[英]Encrypt file in Perl and decrypt in c#
我正在嘗試使用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個左右的字節已正確解密,但其余的只是垃圾。
順便說一句,我可以使用Perl通過以下方式解密加密的文件:
$cipher->start('decrypting');
那么C#和Perl我在做什么錯呢?
編輯:我嘗試按照@munissor的建議並更改要使用的C#代碼
PaddingMode.Zeros
但我仍然得到同樣的例外。 請幫助...
如果查看CPAN文檔中的Crypt :: CBC,它會說“空”填充將塊填充為零。 因此,我認為您應該在C#端使用PaddingMode.Zeros
。
找到了解決方案!!!
在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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.