0

So I found part of an encryption that I'm not advanced enough in to convert to java. This is the javascript encryption:

function rstr2binl(input) {
    var output = Array(input.length >> 2);
    for(var i = 0; i < output.length; i++)
        output[i] = 0;

    for(var i = 0; i < input.length * 8; i += 8)
        output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (i%32);

    return output;
}

I had trouble trying to figure out kind of data the output can be. Here was my attempt:

private static String rstr2binl(String input)
{
    tnt[] output = {input.length() >> 2};

    for(int i = 0; i < output.length; i++)
        output[i] = 0;

    for(int i = 0; i < input.length() * 8; i += 8)
        output[i>>5] |= (input.charAt(i / 8) & 0xFF) << (i%32);

    return output;
}

Would anybody be able to show me how to convert this javascript to java?

1 Answer 1

1

Given that you declare the variable output as int[] the return type of the method should be int[] not String.

However, a quick search for this method indicates that it is part of a JavaScript library to generate MD5 Message Digests.

http://pajhome.org.uk/crypt/md5/md5.html

In which case I would suggest that the best way to implement it in Java is to not try to write your own cryptography functions and just use the standard Java libraries. The java.security.MessageDigest class can return an MD5 hash.

http://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.