in reply to Java to perl conversion

To clarify a bit more of the situation

The encryption cipher from JAVA is out of my hands and generated by an API
I somehow need to match the same cipher in perl

JAVA CODE to generate the Cipher
String key = "just a bunch of text"; byte[] bytes = new byte[256]; byte[] kbytes = key.getBytes(); for (int i = 0; i < bytes.length; i++) { if (i < kbytes.length) { bytes[i] = kbytes[i]; } else { bytes[i] = (byte) i; } } byte[] md5digest = md5DigestAsBytes(bytes); try { theGeneratedKey = Twofish_Algorithm.makeKey(md5digest); blockSize = Twofish_Algorithm.blockSize(); catch (java.security.InvalidKeyException ike) { throw new Exception(Exception.E_SECURITY, "Invalid security key ' +" + new String(md5digest) + "'", ike); }
the part that i need a bit help with:
String key = "just a bunch of text"; byte[] bytes = new byte[256]; byte[] kbytes = key.getBytes(); for (int i = 0; i < bytes.length; i++) { if (i < kbytes.length) { bytes[i] = kbytes[i]; } else { bytes[i] = (byte) i; } } byte[] md5digest = md5DigestAsBytes(bytes);
The rest is quite obvious
I am lacking the experience with java to replicate the code and test it against a perl version
Question should read:
I need to match the exact same cipher in Perl
to be able to utilize Crypt::Twofish and decrypt the data. How can I do this in Perl

This is my first post here.

Replies are listed 'Best First'.
Re^2: Java to perl conversion (Updated)
by BrowserUk (Patriarch) on May 31, 2013 at 10:37 UTC
    the part that i need a bit help with:

    Looking again, I'm wrong! The string/array isn't null padded, but rather padded with bytes equivalent to their position.

    This does exactly that.

    But you will need to understand that Java's byte arrays and Perl's scalars (when they contain strings) are effectively equivalent.

    So, use:

    my $string = "just a bunch of text"; $string .= pack 'C*', length( $string ) .. 255; my $md5 = md5( $string );

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.