Android的 - 如何加密字符串?字符串、Android

2023-09-11 20:30:48 作者:深海映星辰

我工作的一个Android应用程序,并有一对夫妇的字符串,我想发送到数据库之前进行加密。我想要的东西是安全的,易于实施,每一个它通过了相同的数据时会产生同样的事情,preferably将导致在保持一个固定长度的字符串不管是大的字符串传递给它是。也许我在找一个哈希值。

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.

推荐答案

这个片段计算MD5任何给定的字符串

This snippet calculate md5 for any 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 "";
}

来源:http://www.androidsnippets.com/snippets/52/index.html

希望这对您有用。