Java自带的md5、sha和base64加密怎么用

  • jdk自带base64加密的使用
  • jdk自带sha加密
  • jdk自带md5加密

jdk自带base64加密的使用

public static void base64(String s) {
        try {
            BASE64Encoder encoder = new BASE64Encoder();
            String encode = encoder.encode(s.getBytes());  //加密
            System.out.println("encode: " + encode);

            BASE64Decoder decoder = new BASE64Decoder();
            String decode = new String(decoder.decodeBuffer(encode));  //解密
            System.out.println("decode: " + decode);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

jdk自带sha加密

 public static void sha(String s) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA");
            System.out.println(Hex.encodeHexString(digest.digest(s.getBytes())));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

jdk自带md5加密

public static void md5(String s) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(s.getBytes());
            byte[] digest = md.digest();
            System.out.println(Hex.encodeHexString(digest));  //使用的自带的Hex转换为十六进制
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(java)