Note that there are some explanatory texts on larger screens.

plurals
  1. POEncrypt file in Perl and decrypt in c#
    primarykey
    data
    text
    <p>I'm trying to encrypt a text file using Perl and then decrypt it using a different application written in C#. Here's my Perl code:</p> <pre><code>use strict; use Crypt::CBC; my $ifh; my $ofh; my $line; my $cipher = Crypt::CBC-&gt;new( { 'key' =&gt; 'length16length16', 'cipher' =&gt; 'Rijndael', 'iv' =&gt; 'length16length16', 'literal_key' =&gt; 1, 'header' =&gt; 'none', 'padding' =&gt; 'null', 'keysize' =&gt; 128 / 8 } ); open($ifh,'&lt;', $infile) or die "Can't open $infile for encryption input: $!\n"; open($ofh, '&gt;', $outfile) or die "Can't open $outfile for encryption output: $!\n"; $cipher-&gt;start('encrypting'); for $line (&lt;$ifh&gt;) { print $ofh $cipher-&gt;crypt($line); } print $ofh $cipher-&gt;finish; close($ifh) or die "Error closing input file: $!\n"; close($ofh) or die "Error closing output file: $!\n"; </code></pre> <p>And my C# code for decryption:</p> <pre><code> RijndaelManaged myRijndael = new System.Security.Cryptography.RijndaelManaged(); myRijndael.Key = System.Text.Encoding.UTF8.GetBytes("length16length16"); myRijndael.IV = System.Text.Encoding.UTF8-&gt;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(); </code></pre> <p>I keep getting </p> <blockquote> <p>System.Security.Cryptography.CryptographicException: Length of the data to decrypt is invalid</p> </blockquote> <p>When I try to read the data a few bytes at a time, I notice that the first 100 or so bytes are decrypted properly, but the rest is just garbage.</p> <p>BTW, I can decrypt the encrypted file using Perl with:</p> <pre><code>$cipher-&gt;start('decrypting'); </code></pre> <p>So what am I doing wrong with C# and Perl?</p> <p>EDIT: I tried following @munissor advice and change the C# code to use </p> <blockquote> <p>PaddingMode.Zeros</p> </blockquote> <p>but I still get the same exception. Help please...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload