栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java中的DES加密与解密工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中的DES加密与解密工具类

DES 加密与解密工具类
public class DesUtil {

	private SecretKey key = null;// 密钥
	
	// 定义加密算法,可用(DES,DESede,Blowfish,AES)不同的加密方式结果会不同
	private static String algorithm = "AES";
	
	private static DesUtil desUtil = null;

	public DesUtil() {
	}

	public static DesUtil getInstance(String strKey) {
		desUtil = new DesUtil();
		desUtil.createKey(strKey);
		return desUtil;
	}

	// algorithm 算法
	public void createKey(String strKey) {
		try {
			KeyGenerator kg = KeyGenerator.getInstance(DesUtil.algorithm);
			byte[] bt = strKey.getBytes("UTF-8");
			SecureRandom sr = new SecureRandom(bt);
			kg.init(sr);
			this.setKey(kg.generateKey());
		} catch (Exception e) {
		}
	}

	// 加密方法,返回密文
	public String getEnCodeString(String dataStr) {
		byte[] miwen = null;// 密文
		byte[] mingwen = null;// 明文
		Cipher cipher;
		String result = "";// 密文字符串
		try {
			mingwen = dataStr.getBytes("UTF-8");
			cipher = Cipher.getInstance(DesUtil.algorithm);
			cipher.init(Cipher.ENCRYPT_MODE, this.getKey());
			miwen = cipher.doFinal(mingwen);
			result = base64.encode(miwen);// 或者可以用下面的方法得到密文,结果是不一样的,都可以正常解密
			// result=byte2hex(miwen);//密文结果类似2C:37:B0:18:06:64:99:61:DE:60:58:C1:CF:9E:B2:97
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// 解密方法,返回明文
	public String getDecodeString(String codeStr) {
		byte[] miwen = null;
		byte[] mingwen = null;
		String resultData = "";// 返回的明文
		Cipher cipher;
		try {
			miwen = base64.decode(codeStr);
			cipher = Cipher.getInstance(DesUtil.algorithm);
			cipher.init(Cipher.DECRYPT_MODE, this.getKey());
			mingwen = cipher.doFinal(miwen);
			resultData = new String(mingwen, "UTF-8");
		} catch (Exception e) {
			return "密钥不正确或其他原因导致异常,无法解密!";
		}
		return resultData;
	}

	// 二进制转字符串
	public String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0xFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
			if (n < b.length - 1)
				hs = hs + ":";
		}
		return hs.toUpperCase();
	}

	public SecretKey getKey() {
		return key;
	}

	public void setKey(SecretKey key) {
		this.key = key;
	}

	@Contract(pure = true)
    public static String getAlgorithm() {
		return algorithm;
	}

	public static void setAlgorithm(String algorithm) {
		DesUtil.algorithm = algorithm;
	}
	
	public static void main(String[] args) {
		// 以下是加密方法algorithm="AES"的测试
		
		System.out.println(DesUtil.getInstance("lushuaiyin").getEnCodeString("hello"));
		// 输出 LDewGAZkmWHeYFjBz56ylw==
		// 将上面的密文解密:
		System.out.println(DesUtil.getInstance("lushuaiyin").getDecodeString("LDewGAZkmWHeYFjBz56ylw=="));
		// 输出 hello
				
		// 改变密钥测试
		System.out.println(DesUtil.getInstance("suolong").getEnCodeString("hello"));
		// 输出 /RLowOJ+Fr3KdMcdJeNatg==
		System.out.println(DesUtil.getInstance("suolong").getDecodeString("/RLowOJ+Fr3KdMcdJeNatg=="));
		// 输出 hello
		// 如果使用不正确的密钥解密,将会:
		System.out.println(DesUtil.getInstance("suolong").getDecodeString("LDewGAZkmWHeYFjBz56ylw=="));
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/287746.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号