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
しかし、それでも同じ例外が発生します。助けてください...