Hello Wise Monks,

I once again come in search of your infinite knowledge. I am completely lost. This is a little beyond my expertise in encryption. I am trying to rewrite a small c# encryption function in perl and am having a heck of a time with it.

Here is the c# code that I am trying to emulate:
private static string Encrypt(string s) { HashAlgorithm provider = null; try { provider = new SHA1Managed(); byte[] bytes = Encoding.Unicode.GetBytes(s); provider.Initialize(); bytes = provider.ComputeHash(bytes); return Convert.ToBase64String(bytes); } finally { if (provider != null) provider.Clear(); } }
I've tried hundreds of variations on the below code trying to get the same results:
use strict; use MIME::Base64; use Digest::SHA1 qw/sha1 sha1_hex sha1_base64/; use Encode qw/encode decode/; print 'Hello World' . "\n"; print encode_base64(sha1('1234')) . "\n"; print decode("UCS-2BE", decode_base64('E59pyTwEJJao6VjsWTBmLGzMr78=')) +;
To say the least I'm a bit lost and would love a point in the right direction or a friendly bit of advice.
Here is a trace of the data as it comes through the c# code:
// s = '1234' private static string Encrypt(string s) { HashAlgorithm provider = null; try { provider = new SHA1Managed(); byte[] bytes = Encoding.Unicode.GetBytes(s); // bytes = 49, 0, 50, 0, 51, 0, 52, 0 provider.Initialize(); bytes = provider.ComputeHash(bytes); // bytes = 19, 519, 105, 201, 60, 4, 36, 150, 168, 233, + 88, 236, 89, 48, 102, 44, 108, 204, 175, 191 return Convert.ToBase64String(bytes); // bytes = E59pyTwEJJao6VjsWTBmLGzMr78= } finally { if (provider != null) provider.Clear(); } }

Thank you in advance and if I forgot to add any crucial piece of knowledge that would be helpful, let me know and I'll happily provide anything that would help this bit of a puzzle I have. Really am stumped this time...

UPDATE: Realized in my hacking at this to try and make it work my example didn't make a lot of sense. Updated to remove sillyness.

In reply to Rewriting a C# Encryption Function by bkiahg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.