in reply to Rewriting a C# Encryption Function

my $data= pack('cccccccc',49,0,50,0,51,0,52,0); print sha1_hex($data); print sha1_base64($data);

prints out 139f69c93c042496a8e958ec5930662c6cccafbf, which is the hex form of your result and E59pyTwEJJao6VjsWTBmLGzMr78

So your c# encryption routine converts your string rather pointlessly IMHO to unicode and then hashes it. The pack method above has obvious limits, as soon as a char isn't in the same place on page 0 of the unicode charset your hash will be different again.

Do you need the new hash conform to the old or was it just the correctness of the code that was important?

UPDATE: http://perldoc.perl.org/Encode.html might provide methods to get at the underlying bytes of the unicode

Replies are listed 'Best First'.
Re^2: Rewriting a C# Encryption Function
by bkiahg (Pilgrim) on Aug 22, 2008 at 21:45 UTC
    Getting closer to this. I think the problem I'm facing right now is in the Encoding to Unicode:
    use strict; use MIME::Base64; use Digest::SHA1 qw/sha1 sha1_hex sha1_base64/; use Encode qw/encode decode/; my $data = encode("UTF-16", 1234); $data = sha1($data); print encode_base64($data) . "\n";

      I note that the bytes which the C# is taking the SHA1 of are little-endian UTF-16:

      // s = '1234' .... byte[] bytes = Encoding.Unicode.GetBytes(s); // bytes = 49, 0, 50, 0, 51, 0, 52, 0
      so you're probably best off:
      my $data = encode("UTF-16LE", .....);
      On my x86 (little-endian) machine
      my $data = encode("UTF-16", '1234');
      gives $data bytes:
      0xFE 0xFF 0x00 0x31 0x00 0x32 0x00 0x33 0x00 0x34
      which is big-endian complete with BOM. Whereas:
      my $data = encode("UTF-16LE", '1234');
      gives $data bytes:
      0x31 0x00 0x32 0x00 0x33 0x00 0x34 0x00
      which is little-endian sans BOM; which appears to be what's required.

        You are the man!

        Thank you guys for helping me solve this, below is the code that does exactly what I was looking for.
        use strict; use MIME::Base64; use Digest::SHA1 qw(sha1 sha1_hex sha1_base64); use Encode qw/encode decode/; my $data = encode("UTF-16LE", '1234'); $data = sha1($data); print encode_base64($data) . "\n";
Re^2: Rewriting a C# Encryption Function
by bkiahg (Pilgrim) on Aug 22, 2008 at 21:20 UTC
    The hash has to match with the old. IE if the data submitted is 1234 then the encrypted data/base64 needs to equal E59pyTwEJJao6VjsWTBmLGzMr78. How it gets there isn't as important.

    The perl function doesn't have to convert the data to unicode and then hash it, if it is simpler it skip certain parts than I'm more than happy to do that. Its the end result that has to match.

      Sadly you can skip unicode madness only if you know that the strings you have to convert are all from a very limited set of chars (a-z,A-Z,0-9 and a few special chars). Then using just the pack method would be enough.

      But the link I provided seems to have the solution on a platter:

      my $octets= encode("utf16", '1234'); $octets= substr($octets,2); for my $i (0..(length($octets)/2-1)) { my $n= substr($octets,$i*2,1); substr( $octets,$i*2,1)= substr($octets,$i*2+1,1); substr($octets,$i*2+1,1)=$n; } print sha1_base64($octets);

      prints out 'E59py...'. The really ugly for loop swaps the bytes of one 16 bit value since the encoding was swapped on my machine. If you don't get the same result, drop the for loop and check again.

        First off, you shouldn't forget to mention use Encode; -- believe it or not, some folks might still not be aware that "encode" is not a perl built-in function, and/or might not know what module it comes from. (Update: but on closer inspection, I see the OP does know about Encode, so I apologize for the nit-pick.)

        Second, you really should not use "utf16" in this case -- better to specify what byte order you really want for the job, because that way, perl won't automatically add the BOM at the start of the string, and you won't have to byte-swap later:

        $ perl -MEncode -e '$o=encode("utf16","1234"); print $o' | xxd -g1 0000000: fe ff 00 31 00 32 00 33 00 34 ...1.2.3.4 $ perl -MEncode -e '$o=encode("utf16le","1234"); print $o' | xxd -g1 0000000: 31 00 32 00 33 00 34 00 1.2.3.4. $ perl -MEncode -e '$o=encode("utf16be","1234"); print $o' | xxd -g1 0000000: 00 31 00 32 00 33 00 34 .1.2.3.4