java MessageDigest加密方式

java MessageDigest加密方式

package com.oysept.demo;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * java自带加密方式
 * @author ouyangjun
 */
public class MessageDigestTest {

    public static void main(String[] args) throws NoSuchAlgorithmException {
		
        String str = "111222333";
        System.out.println("MD5加密: " + encryptMode(str, "MD5"));
        System.out.println("SHA加密: " + encryptMode(str, "SHA"));
        System.out.println("SHA1加密: " + encryptMode(str, "SHA1"));
        System.out.println("SHA-224加密: " + encryptMode(str, "SHA-224"));
        System.out.println("SHA-256加密: " + encryptMode(str, "SHA-256"));
        System.out.println("SHA-384加密: " + encryptMode(str, "SHA-384"));
        System.out.println("SHA-512加密: " + encryptMode(str, "SHA-512"));
    }
	
    public static String encryptMode(String str, String mode) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance(mode);
        // 更新
        md.update(str.getBytes());
        // 获取
        byte[] bt = md.digest();
        // 转换
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < bt.length; i++) {
            buffer.append(Character.forDigit((bt[i] & 240) >> 4, 16));
            buffer.append(Character.forDigit(bt[i] & 15, 16));
        }
        return buffer.toString();
    }
}

打印效果:

java MessageDigest加密方式_第1张图片

 

识别二维码关注个人微信公众号

本章完结,待续,欢迎转载!
 
本文说明:该文章属于原创,如需转载,请标明文章转载来源!

你可能感兴趣的:(Java)