Android 實現SHA1加密演算法程式碼
阿新 • • 發佈:2019-01-25
實現SHA1加密程式碼
public static String getSecurityAppKey() {
return encryptToSHA(RequestTools.AppId + "UZ" +
RequestTools.AppKey + "UZ" + System.currentTimeMillis()) +
"." + System.currentTimeMillis();
}
public static String encryptToSHA (String info) {
byte[] digesta = null;
try {
MessageDigest alga = MessageDigest.getInstance("SHA-1");
alga.update(info.getBytes());
digesta = alga.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String rs = byte2hex(digesta);
return rs;
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs;
}
使用方法按照getSecurityAppKey方法使用,將要加密的字串寫到encryptToSHA中即可!