Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6807019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:46:53+00:00 2026-05-26T19:46:53+00:00

I’m trying to encrypt a text file using Perl and then decrypt it using

  • 0

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:

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";

And my C# code for decryption:

    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();

I keep getting

System.Security.Cryptography.CryptographicException: Length of the
data to decrypt is invalid

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.

BTW, I can decrypt the encrypted file using Perl with:

$cipher->start('decrypting');

So what am I doing wrong with C# and Perl?

EDIT: I tried following @munissor advice and change the C# code to use

PaddingMode.Zeros

but I still get the same exception.
Help please…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:46:53+00:00Added an answer on May 26, 2026 at 7:46 pm

    Found the solution!!!

    In Perl, I had to add after the opening of the output file:

    binmode $ofh;

    The padding suggestion was helpful, but in the end I omitted the padding directive and used the default which is PKCS7 in Perl and in C#.

    My final Perl code looks like this:

    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";
    

    and the C# part:

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want use html5's new tag to play a wav file (currently only supported
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a reasonable size flat file database of text documents mostly saved in
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.