Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: String differs from printing to STDOUT and printing to file

by cboesch (Initiate)
on Oct 23, 2019 at 10:53 UTC ( [id://11107884]=note: print w/replies, xml ) Need Help??


in reply to Re: String differs from printing to STDOUT and printing to file
in thread String differs from printing to STDOUT and printing to file

Yes, piping the output to a file results in the same as printing to the file.
So it looks like the terminal does that.
Here is the code for the whole script:
#!/usr/bin/env perl use strict; use Crypt::Rijndael; use MIME::Base64; my $string = 'QeTEv2804'; sub encrypt { my ($plaintext) = @_; my $password = "uNsY3WSs0hTd"; my $trail = 16 - (length($plaintext) % 16 ); $plaintext .= "\0" x $trail; my $cipher = Crypt::Rijndael->new(pack("A32",$password),Crypt::Rijnd +ael::MODE_CBC()); $cipher->set_iv(pack("A16",$password)); my $crypted = $cipher->encrypt($plaintext); my $encoded = encode_base64($crypted); return $encoded; } sub decrypt { my ($ciphertext) = @_; my $password = "uNsY3WSs0hTd"; my $cipher_nonbase64 = decode_base64($ciphertext); my $cipher = Crypt::Rijndael->new(pack("A32",$password),Crypt::Rijnd +ael::MODE_CBC()); $cipher->set_iv(pack("A16",$password)); my $plaintext = $cipher->decrypt($cipher_nonbase64); return $plaintext; } my $enc = &encrypt($string); my $dec = &decrypt("$enc"); print "Decrypted --> $dec <--\n";
Pipe the script to file and you should see it.

Replies are listed 'Best First'.
Re^3: String differs from printing to STDOUT and printing to file
by haukex (Archbishop) on Oct 23, 2019 at 10:59 UTC

    Thanks for posting the code, it confirms GrandFather's suggestion; using my advice from here to use Devel::Peek to inspect $dec, it shows:

    SV = PV(0x571d5fd57c40) at 0x560d5ed7d9a0 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x571d5fe15180 "QeTEv2804\0\0\0\0\0\0\0"\0 CUR = 16 LEN = 18 COW_REFCNT = 0

    As per the Crypt::Rijndael docs, the blocksize for Rijndael is 16 bytes.

Re^3: String differs from printing to STDOUT and printing to file
by rjt (Curate) on Oct 23, 2019 at 11:05 UTC

    encrypt() seems to NUL pad the string to the required block size:

      $plaintext .= "\0" x $trail;

    That's where your NULs are coming from. If you know that trailing \0s are not ever valid, you can strip them right before your return in decrypt():

    $plaintext =~ s/\0+$//;

    On the other hand, if trailing NUL are a legitimate possibility, you will have to come up with a different plan, such as encoding the real length within your plaintext in encrypt() so you can trim it in the decrypt() sub.

      Ok that's it.
      Thanks a lot!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11107884]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2025-05-24 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.