SHA-1加密算法

MD5并不安全,推荐使用SHA-1加密算法:

package com.sichang.util;


import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;


public class SHA_1 {


public static String Encrypt(String strSrc) {

MessageDigest md = null;

String strDes = null;


byte[] bt = strSrc.getBytes();

try {


md = MessageDigest.getInstance("SHA-1");

md.update(bt);

strDes = bytes2Hex(md.digest());

} catch (NoSuchAlgorithmException e) {

System.out.println("Invalid algorithm.");

return null;

}

return strDes;

}


public static String bytes2Hex(byte[] bts) {

String des = "";

String tmp = null;

for (int i = 0; i < bts.length; i++) {

tmp = (Integer.toHexString(bts[i] & 0xFF));

if (tmp.length() == 1) {

des += "0";

}

des += tmp;

}

return des;

}


public static void main(String[] args) {

String strSrc = "User Password";

System.out.println("Source String:" + strSrc);

System.out.println("Encrypted String:");

System.out.println("Use SHA:" + Encrypt(strSrc).toUpperCase());

System.out.println("Use SHA:" + Encrypt(strSrc).toUpperCase().length());

}


}


你可能感兴趣的:(SHA-1加密算法)