Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

convert hexadecimal value to encrypted string

by linuxfan (Beadle)
on Dec 11, 2004 at 02:10 UTC ( [id://414048]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I have encrypted text that is stored as a hex string in a variable. How do I get back the original encrypted string using pack/unpack?

I tried

my $str = "639D879F224CA2A1BA73CC4884DBD362"; pack 'H*',$str;
However, the above does not give me the encrypted string. What is the right format that I should supply to pack so I can get back the encrypted string?

Thanks a lot!

Replies are listed 'Best First'.
Re: convert hexadecimal value to encrypted string
by Mr. Muskrat (Canon) on Dec 11, 2004 at 02:27 UTC

    It's not really solvable with pack or unpack if the string is truly encrypted.

Re: convert hexadecimal value to encrypted string
by diotalevi (Canon) on Dec 11, 2004 at 02:30 UTC
    You'll have to use your encryption software to decrypt the output. How did you encrypt the value in the first place?
      I have an encryption server that encrypts this data and writes this to a table in the database. I can extract data from the database and then send it in a request to the server so that it can give back me the plain text(this is what I am trying to do right now).

      The data is stored as a hex string in the database table. I want to convert it back to a "regular string" and am unable to find the right conversion string using pack/unpack. I hope this makes my question clearer.

      Thanks!

        You'll need to send the data back to the encryption server and have it decrypt it.
Re: convert hexadecimal value to encrypted string
by nedals (Deacon) on Dec 11, 2004 at 04:29 UTC
    It does not use pack/unpack, but does this return the encrypted string you need?
    use strict; $_ = "639D879F224CA2A1BA73CC4884DBD362"; my @hexdata = /(\w{2})/g; my $str = join('', map { chr(hex $_) } @hexdata); print "$str\n";
    Maybe, seeing this, someone may be able to give you a pack/unpack solution.
Re: convert hexadecimal value to encrypted string
by Prior Nacre V (Hermit) on Dec 11, 2004 at 02:44 UTC

    Your problem may be just nybble order. Try:

    pack 'h*', $str;

    Regards,

    PN5

      Thanks for your reply. I tried this but it doesn't work for me :(
Re: convert hexadecimal value to encrypted string
by ikegami (Patriarch) on Dec 11, 2004 at 07:35 UTC
    What do you expect the output to be for the string you gave?
Re: convert hexadecimal value to encrypted string
by exussum0 (Vicar) on Dec 11, 2004 at 13:47 UTC
    How did you encrypt it? Whatever you used for encrypting may have a way of decrypting. If this is just a hash value, then you CAN'T get back the encrypted string, but only verify against it by re-encrypting your original string back to the gobbledy gook.

    ----
    Give me strength for today.. I will not talk it away..
    Just for a moment.. It will burn through the clouds.. and shine down on me.

Re: convert hexadecimal value to encrypted string
by melora (Scribe) on Dec 11, 2004 at 21:56 UTC
    Here's a hex-to-ASCII thing that I use
    $text =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    That would give you the original encrypted string in ASCII form from the encrypted string in hex form. I'm assuming that each ASCII char is stored as two (ASCII) characters of hex.
    I don't know if that does what you need... I hope it helps.
Re: convert hexadecimal value to encrypted string
by Anonymous Monk on Dec 11, 2004 at 18:27 UTC
    This may sound like a dumb question, but did you assign the result of pack to a string? Or just call pack in void context like you did in your example code?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 02:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found