Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

generating an encoded text

by shekarkcb (Beadle)
on Dec 21, 2011 at 10:09 UTC ( [id://944575]=perlquestion: print w/replies, xml ) Need Help??

shekarkcb has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Greetings. Just wonder, if Perl has any functions/packages to encode/decode strings. I have string,

  • Encode the string to Hex (something similar to toHexString() ). -> For this i tried xxd -r -p

  • convert fromHexString -> not sure what to use...

  • Create a SHA1 hash for a string - for this i tried openssl sha1 -binary

  • convert a string to ToBase64String -> i have tried  openssl base64

  • Convert a string to AES-128-ECB encode , with some key -> for this i tried  openssl -enc aec-128-ecb -nosalt -k KEY

Your suggetions/pointers are greatly helpful.

Thanks.

Replies are listed 'Best First'.
Re: generating an encoded text
by moritz (Cardinal) on Dec 21, 2011 at 12:10 UTC
      Thanks for the reply monks. As i am not that good in Perl, it would be great if somebody post some sample code.

      Thanks.

        There is sample code at the top of the documentation of each module that I linked to -- did you look at it? and if yes, which parts did you not understand?

Re: generating an encoded text
by Anonymous Monk on Dec 21, 2011 at 10:13 UTC

    Greetings. Just wonder, if Perl has any functions/packages to ...

    Yes, see CPAN, MetaCPAN

Re: generating an encoded text
by Khen1950fx (Canon) on Dec 21, 2011 at 21:45 UTC
    Here's a little script that I put together to help you get going. Instead of SHA1, I use SHA-256---for me, it's better and stronger than SHA1; also, for AES-128-ECB, I used Crypt::CBC with a Rijndael cipher in MODE_ECB, rijndael_compat. Give it a try.
    #!/usr/bin/perl -l use strict; use warnings; use CPAN; use Crypt::CBC; use Crypt::Rijndael; use Data::Translate; use Data::Dumper::Concise; use MIME::Base64 qw(encode_base64); use Digest::SHA qw(sha256_hex sha256_base64); $| = 1; CPAN::Shell->install(qw( Crypt::CBC Crypt::Rijndael Data::Translate Data::Dumper::Concise Digest::SHA MIME::Base64)); print "=" x 72; print "Converting ascii to hex: "; my $data = new Data::Translate; my @aa = qw(hello_world); my ( $s, @ah ) = $data->a2h(@aa); binmode STDOUT, ":encoding(utf8)"; print "\t", join( '', @ah ); print "=" x 72; print "Converting hex to ascii:"; my $data2 = new Data::Translate; my @hh = qw(68 65 6c 6c 6f 5f 77 6f 72 6c 64); my ( $s2, @ha2 ) = $data2->h2a(@hh); binmode STDOUT, ":encoding(utf8)"; print "\t", join( '', @ha2 ); print "=" x 72; print "Calculating sha256_hex digest for hex: "; my $data3 = '68 65 6c 6c 6f 5f 77 6f 72 6c 64'; print "\t", sha256_hex($data3); print "=" x 72; print "Calculating sha256_hexdigest for hello_world: "; my $data4 = qw(hello_world); print "\t", sha256_hex($data4); print "=" x 72; print "Calculating sha256_base64 for hello_world: "; my $data5 = qw(hello_world); local ($/) = undef; print "\t", encode_base64($data5); print "=" x 72; my $bs = Crypt::Rijndael->blocksize; my $ks = Crypt::Rijndael->keysize; my $cipher = Crypt::CBC->new( -key => 'a' x $ks, -iv => 'f' x $bs, -literal_key => 1, -cipher => 'Rijndael', -header => 'none', -padding => 'rijndael_compat', ); my $in = <<"END"; hello_world END my $j = Crypt::Rijndael->new( 'a' x $ks, Crypt::Rijndael->MODE_ECB ); $j->set_iv( 'f' x $bs ); $cipher->start('encrypting'); if ($in) { my $string = 'a' x 32; binmode STDOUT, ":encoding(UTF-8)"; my $encrypt = $cipher->encrypt($string); my $decrypt = $cipher->decrypt($encrypt); print "Testing MODE_ECB: "; print "\t", Dumper("$decrypt"); } $cipher->finish;

    References:

    Crypt::CBC
    Crypt::Rijndael
    Data::Translate
    Data::Dumper::Concise
    MIME::Base64
    Digest::SHA

      Thanks 'Khen1950fx' for the reply. Will try to implement the same in my case.
      
      Thanks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-03-28 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found