Зашифруйте файл в Perl и расшифруйте в c # - PullRequest
2 голосов
/ 14 ноября 2011

Я пытаюсь зашифровать текстовый файл с помощью 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

, но я все еще получаю то же исключение.Помогите пожалуйста ...

Ответы [ 2 ]

3 голосов
/ 14 ноября 2011

Если вы проверяете документацию CPAN для Crypt :: CBC , это говорит о том, что «нулевое» заполнение заполняет блоки нулями.Поэтому я думаю, что вы должны использовать PaddingMode.Zeros на стороне C #.

1 голос
/ 15 ноября 2011

Нашел решение !!!

В Perl мне пришлось добавить после открытия выходного файла:

binmode $ ofh;

Предложение заполнения было полезным, но в конце я опустил директиву padding и использовал значение по умолчанию - PKCS7 в Perl и в C #.

Мой окончательный код 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();
...