views:

82

answers:

1

I am working on an Android app and have a couple strings that I would like to encrypt before sending to a database. I'd like something that's secure, easy to implement, will generate the same thing every time it's passed the same data, and preferably will result in a string that stays a constant length no matter how large the string being passed to it is. Maybe I'm looking for a hash.

A: 

Hi! This snippet calculate md5 for a given string

public String md5(String s) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i=0; i<messageDigest.length; i++)
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

Source: http://www.androidsnippets.org/snippets/52/index.html

Hope this is useful for you

Antonio
Yes, thanks! I am at work and unable to try it right now (this is my research time), but I will try it when I get home and it seems like it'll be just what I need.
Jorsher
could you tell me how to decrypt your code,thanks
pengwang