如何在Android中散列字符串?

Jor*_*her 49 java hash android cryptography

我正在开发一个Android应用程序,并且在发送到数据库之前有一些我想要加密的字符串.我想要一些安全,易于实现的东西,每次传递相同的数据时都会产生相同的东西,并且最好会产生一个字符串,无论传递给它的字符串有多大,它都会保持不变.也许我正在寻找哈希.

Ant*_*nio 87

此代码段为任何给定字符串计算md5

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 "";
}
Run Code Online (Sandbox Code Playgroud)

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

希望这对你有用

  • 此代码无法正常运行.生成的字符串中缺少一些"0"字符.我不知道为什么,但事实就是如此. (29认同)
  • 当此代码失败时有一个特殊情况.当两位十六进制数中的第一个为零时.这段代码更好:http://stackoverflow.com/a/6565597/221135 (6认同)
  • MD5,afaik,不被认为是可逆的.通常,您可以使用它来填充某些内容,通常是密码或类似内容,然后验证密码,您将运行相同的加密并将结果与​​存储的结果进行比较. (2认同)
  • 同意@SertalpBilal,正确答案在这里http://stackoverflow.com/a/4846511/1092591 (2认同)

Cra*_*g B 64

来自(http://www.androidsnippets.org/snippets/52/index.html)的上述功能存在缺陷.如果messageDigest中的一个数字不是两个字符的十六进制值(即0x09),则它无法正常工作,因为它没有填0.如果你搜索周围你会发现它的功能和抱怨不工作 这是一个更好的在本页的评论部分找到,我稍作修改:

public static String md5(String s) 
{
    MessageDigest digest;
    try
    {
        digest = MessageDigest.getInstance("MD5");
        digest.update(s.getBytes(Charset.forName("US-ASCII")),0,s.length());
        byte[] magnitude = digest.digest();
        BigInteger bi = new BigInteger(1, magnitude);
        String hash = String.format("%0" + (magnitude.length << 1) + "x", bi);
        return hash;
    }
    catch (NoSuchAlgorithmException e)
    {
        e.printStackTrace();
    }
    return "";
}
Run Code Online (Sandbox Code Playgroud)

  • 我编辑它来处理被丢弃的前导零...现在它正常工作:) (7认同)
  • 这个也有问题.尝试编码q4m'x68n6_YDB4ty8VC4&} wqBtn ^ 68W,它给出c70bb931f03b75af1591f261eb77d0b,而正确的那个应该是0c70bb931f03b75af1591f261eb77d0b前0个disappers. (6认同)
  • 没有亲爱的它导致问题它将丢弃0我曾尝试过.net和java这个功能丢弃0 (2认同)

小智 21

不工作的方法:

public static 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 "";
}
Run Code Online (Sandbox Code Playgroud)

结果 - 1865e62e7129927f6e4cd9bff104f0(长度30)

工作方式:

public static final String md5(final String toEncrypt) {
    try {
        final MessageDigest digest = MessageDigest.getInstance("md5");
        digest.update(toEncrypt.getBytes());
        final byte[] bytes = digest.digest();
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(String.format("%02X", bytes[i]));
        }
        return sb.toString().toLowerCase();
    } catch (Exception exc) {
        return ""; // Impossibru!
    }
}
Run Code Online (Sandbox Code Playgroud)

结果 - 1865e62e7129927f6e4c0d9bff1004f0(长度32)


Don*_*nut 7

private static char[] hextable = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

public static String byteArrayToHex(byte[] array) {
    String s = "";
    for (int i = 0; i < array.length; ++i) {
        int di = (array[i] + 256) & 0xFF; // Make it unsigned
        s = s + hextable[(di >> 4) & 0xF] + hextable[di & 0xF];
    }
    return s;
}

public static String digest(String s, String algorithm) {
    MessageDigest m = null;
    try {
        m = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return s;
    }

    m.update(s.getBytes(), 0, s.length());
    return byteArrayToHex(m.digest());
}

public static String md5(String s) {
    return digest(s, "MD5");
}
Run Code Online (Sandbox Code Playgroud)

  • 是的100 %%工作我有上面的功能所有导致问题与0 disacrding但这3功能解决方案给我我确切的解决方案我匹配.net md5 encription (2认同)

小智 5

上面的答案几乎100%正确.它会因unicode而失败.

    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("MD5");
        byte utf8_bytes[] = tag_xml.getBytes();
        digest.update(utf8_bytes,0,utf8_bytes.length);
        hash = new BigInteger(1, digest.digest()).toString(16);
    } 
    catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

需要字节数组的长度而不是字符串.


Cli*_*ize 5

使用@Donut解决方案,您必须使用UTF-8编码字符(例如:é)getBytes("UTF-8").这是我对摘要方法的更正:

private static char[] hextable = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};


public static String byteArrayToHex(byte[] array) {
    String s = "";
    for (int i = 0; i < array.length; ++i) {
        int di = (array[i] + 256) & 0xFF; // Make it unsigned
        s = s + hextable[(di >> 4) & 0xF] + hextable[di & 0xF];
    }
    return s;
}

public static String digest(String s, String algorithm) {
    MessageDigest m = null;
    try {
        m = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return s;
    }

    try {
        m.update(s.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        m.update(s.getBytes());
    }
    return byteArrayToHex(m.digest());
}

public static String md5(String s) {
    return digest(s, "MD5");
}
Run Code Online (Sandbox Code Playgroud)