请SHA1加密在Android? [英] Make SHA1 encryption on Android?
本文介绍了请SHA1加密在Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您可以建议我如何使用SHA1算法来加密字符串? 我搜索了。但没有运气。
Can you suggest me about how to encrypt string using SHA1 algorithm ? I've searched about it. But no luck.
在此先感谢。
推荐答案
binnyb的 convertToHex
方法不能正常工作。更正确的对我的作品是:
binnyb's convertToHex
method is not working properly. A more correct one that works for me is:
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9)) {
buf.append((char) ('0' + halfbyte));
}
else {
buf.append((char) ('a' + (halfbyte - 10)));
}
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] sha1hash = new byte[40];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
sha1hash = md.digest();
return convertToHex(sha1hash);
}
使用 SHA1
的方法,让您的SHA1字符串。
use the SHA1
method to get your sha1 string.
更新:提供了一个完整的答案
Update: providing a complete answer
这篇关于请SHA1加密在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文