Я пытаюсь зашифровать текстовый файл с помощью 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 advice и измените код C # на
PaddingMode.Zeros
, но я все еще получаю то же исключение.Помогите пожалуйста ...