1 import java.security.MessageDigest;
 2 import java.security.NoSuchAlgorithmException;
 3 
 4 public class SHA1 {
 5      public String Encrypt(String strSrc) {
 6            MessageDigest md = null;
 7            String strDes = null;
 8 
 9             byte[] bt = strSrc.getBytes();
10             try {
11 
12                 md = MessageDigest. getInstance("SHA-1");
13                 md.update(bt);
14                 strDes = bytes2Hex(md.digest()); // to HexString
15            } catch (NoSuchAlgorithmException e) {
16                 System. out.println( "Invalid algorithm.");
17                  return null;
18            }
19             return strDes;
20      }
21 
22      public String bytes2Hex( byte[] bts) {
23            String des = "";
24            String tmp = null;
25             for ( int i = 0; i < bts. length; i++) {
26                 tmp = (Integer. toHexString(bts[i] & 0xFF));
27                  if (tmp.length() == 1) {
28                      des += "0";
29                 }
30                 des += tmp;
31            }
32             return des;
33      }
34 
35      public static void main(String[] args) {
36            SHA1 te = new SHA1();
37            String strSrc = "abcd";
38            System. out.println( "Use SHA:" + te.Encrypt(strSrc).toUpperCase());
39      }
40 }

相关文章:

  • 2022-02-12
  • 2021-12-02
猜你喜欢
  • 2021-09-29
  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2022-12-23
相关资源
相似解决方案