首页>代码>JAVA加密算法工具>/3645411796829184.java
package com.mine.util;

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

public class DecryptUtil {

	public static void main(String[] args) throws DigestException {
		System.out.println(decrypt("123456", "SHA-1", Boolean.FALSE));
		System.out.println(decrypt("123456", "SHA-256", Boolean.FALSE));
		System.out.println(decrypt("123456", "MD5", Boolean.FALSE));
	}

	/**
	 * 对一个字符串进行指定算法的加密后,得到其十六进制字符串
	 * 
	 * @param decrypt			需要加密的字符串
	 * @param decryptType		加密的算法类型,如 SHA-1,MD5
	 * @param upper				加密结果是否转换为大写
	 * @return					加密后的字符串
	 * @throws DigestException	出错
	 */
	public static String decrypt(String decrypt, String decryptType, Boolean upper) throws DigestException {
		try {
			// 指定算法
			MessageDigest digest = MessageDigest.getInstance(decryptType);
			digest.update(decrypt.getBytes());
			// 获取字节数组
			byte messageDigest[] = digest.digest();
			// Create Hex String
			StringBuffer hexString = new StringBuffer();
			// 字节数组转换为【十六进制】数
			for (int i = 0; i < messageDigest.length; i++) {
				String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexString.append(0);
				}
				hexString.append(shaHex);
			}
			return upper ? hexString.toString().toUpperCase() : hexString.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			throw new DigestException("签名错误!");
		}
	}

}
最近下载更多
crosa_Don  LV18 2022年4月1日
charles1256  LV11 2021年12月15日
jiajia91323  LV23 2019年4月19日
栈道小生  LV10 2019年1月10日
1805909489  LV1 2018年11月20日
gaoxin222  LV14 2018年11月15日
近在咫尺-莫  LV6 2018年11月5日
ycd2013_8  LV10 2018年7月25日
liuJian2414  LV5 2018年5月13日
mhxc_004  LV4 2018年5月9日
最近浏览更多
ClydeSon  LV5 2023年12月28日
EFWAGGFAWGR 2023年10月19日
暂无贡献等级
1358849392  LV21 2023年5月9日
wuziayng1232  LV10 2023年2月21日
不择细流 2023年1月5日
暂无贡献等级
微信网友_6191697646571520  LV6 2022年11月24日
微信网友_6220678544494592  LV1 2022年11月15日
heqian  LV16 2022年10月17日
mikpro 2022年8月1日
暂无贡献等级
huevnn  LV5 2022年6月15日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友