我正在尝试用Java创建一个简单的String to SHA1转换器,这就是我所拥有的…
public static String toSHA1(byte[] convertme) { MessageDigest md = null; try { md = MessageDigest.getInstance("SHA-1"); } catch(NoSuchAlgorithmException e) { e.printStackTrace(); } return new String(md.digest(convertme)); }
当我通过它时toSHA1("password".getBytes()),我[�a�ɹ??�%l�3~��.知道它可能是像UTF-8这样的简单编码修复程序,但是有人可以告诉我我应该怎么做才能得到我想要的东西5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8吗?还是我这样做完全错误?
toSHA1("password".getBytes())
�a�ɹ??�%l�3~��
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
这是我将字符串转换为sha1的解决方案。它在我的Android应用程序中运行良好:
private static String encryptPassword(String password) { String sha1 = ""; try { MessageDigest crypt = MessageDigest.getInstance("SHA-1"); crypt.reset(); crypt.update(password.getBytes("UTF-8")); sha1 = byteToHex(crypt.digest()); } catch(NoSuchAlgorithmException e) { e.printStackTrace(); } catch(UnsupportedEncodingException e) { e.printStackTrace(); } return sha1; } private static String byteToHex(final byte[] hash) { Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format("%02x", b); } String result = formatter.toString(); formatter.close(); return result; }