欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 加解密(3DES)

程序员文章站 2022-06-14 20:46:55
...
package com.paic.umap.ucm.common.utils;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.util.encoders.Base64;

public class LoginDesUtil {
public static final String ALGORITHM_DES = "DESede/ECB/PKCS5Padding";
	
	public static final String ZZGJSPWDKEY = "1HH798GZDKUO2167W5GM5YNG";
	

	
	/**
	 * 3DES加密方法
	 * 
	 * @param value  待加密信息
	 * @param desKey 密钥
	 * @return  3DES加密后用Base64编码的字符串
	 */
	public static String encrypt(String value, String desKey) {
		String result = null;
		try {
			SecretKeySpec key = new SecretKeySpec(desKey.getBytes(), 0, 24, "DESede");
			Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
			cipher.init(Cipher.ENCRYPT_MODE, key);
			result = new String(Base64.encode(cipher.doFinal(value.getBytes("UTF-8"))), "UTF-8");
		} catch (Exception e) {
		} 
		
		return result;
		
	}
	
	/**
	 * 3DES解密方法
	 * 
	 * @param value  3DES加密后用Base64编码的字符串
	 * @param desKey 密钥
	 * @return 加密原文
	 */
	public static String decrypt(String value, String desKey) {
		String result = null;
		try {
			SecretKeySpec key = new SecretKeySpec(desKey.getBytes(), 0, 24, "DESede");
			Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
			cipher.init(Cipher.DECRYPT_MODE, key);
			result = new String(cipher.doFinal(Base64.decode(value)), "UTF-8");
		} catch (Exception e) {
		} 
		
		return result;
	}
	
	public static void main(String[] args) {
		String result="+=  +=//";
		result = result.replaceAll("[+]", "%2B");
		result = result.replaceAll("[/]", "%2F");
		result = result.replaceAll("[=]", "%3D");
		result = result.replaceAll("[ ]", "%20");
		System.out.println(result);
		result = result.replaceAll("%2F", "/");
		result = result.replaceAll("%3D", "=");
		result = result.replaceAll("%20", " ");
		result = result.replaceAll("%2B", "+");
		System.out.println(result);
		System.out.println(LoginDesUtil.encryptToURL("!@#+/\\$  %^&*()[]{}【】|!金jin123", ZZGJSPWDKEY));
		
		System.out.println(LoginDesUtil.decryptToURL(LoginDesUtil.encryptToURL("!@#+/\\$  %^&*()[]{}【】|!金jin123", ZZGJSPWDKEY), ZZGJSPWDKEY));
	}
	
	
	/**
	 * 3Des的key加密(建议优先考虑) ,用于加密的URL使用
	 * @param value 待加密信息
	 * @return  3DES加密后用Base64编码的字符串
	 */
	public static String encryptToURL(String value, String key){
		String result = encrypt(value, key);
		// 将加密后字符串中“+”、“/”、“=”转换成替代字符串
		result = result.replaceAll("[+]", "%2B");
		result = result.replaceAll("[/]", "%2F");
		result = result.replaceAll("[=]", "%3D");
		result = result.replaceAll("[ ]", "%20");
		return result;
	}
	
	/** 
	 * 3Des使用默认的key解密URL (建议优先考虑) 
	 * @param value 3DES加密后用Base64编码的字符串
	 * @return 加密原文
	 */
	public static String decryptToURL(String value,String key){
		String result = value.replaceAll("%2B", "+");
		result = result.replaceAll("%2F", "/");
		result = result.replaceAll("%3D", "=");
		result = result.replaceAll("%20", " ");
		result = decrypt(result, key);
		return result;
	}
	
	
	/**
	 * 3Des使用默认的key解密
	 * @param value 待解密信息
	 * @return  3DES加密后用Base64编码的字符串
	 */
	public static String decryptToZZGJS(String value){
		return decrypt(value, ZZGJSPWDKEY);
	}
	
	public static String sign(String text, String sign) {
		String sha1 = null;
		try {
			MessageDigest md = MessageDigest.getInstance("SHA1");
			md.update(text.getBytes());
			sha1 = byte2string(md.digest());
		} catch (Exception e) {
		}
		return sha1;
	}
	
	private static String byte2string(byte[] data) {
		StringBuffer sb = new StringBuffer();
		char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'A', 'B', 'C', 'D', 'E', 'F' };
		for(int i = 0; i < data.length; i++) {
			char[] t = new char[2];
			t[0] = hex[(data[i] >>> 4) & 0x0F];
			t[1] = hex[data[i] & 0x0F];
			sb.append(t);
		}
		return sb.toString();
	}
}




引用

http://dcloud.pa18.com/Citrix/XenApp/auth/silentDetection.aspx

相关标签: java 3des des