Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by GrandFather (Saint)
on Oct 23, 2019 at 10:41 UTC ( #11107883=note: print w/replies, xml ) Need Help??


in reply to String differs from printing to STDOUT and printing to file

The ^@s are most likely null characters in the file. You don't show us how $var gets its value so the nulls could be in the variable. My guess it that the printf to STDOUT isn't showing the nulls in your terminal window even though they are there, but whatever you are using to see the contents of the file is showing the nulls.

As a test you could pipe the output from your print version of the script to a file then inspect the file in the same way as you did for the script generated file. I expect you will find both files are the same and the problem is in how you generate the contents of $var.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^2: String differs from printing to STDOUT and printing to file
by cboesch (Initiate) on Oct 23, 2019 at 10:53 UTC
    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.

      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.

      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://11107883]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2023-09-30 13:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?