java密码的加密与解密(3des)

本文介绍了一个Java实现的3DES加密和解密工具类,包括加密、解密、编码和解码方法。示例代码展示了如何使用密钥对字符串进行3DES加密和解密操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * 
 */
package com.csair.session.memcache;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.Security;


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


/**
 * @author Administrator
 *
 */
public class TripleDESEncrypt {
	private static final String Algorithm = "DESede";
//	public static byte[] keyBytes = "HjgoO8cvnRJeFu0Scj8EtNPR".getBytes();
	public static byte[] keyBytes = "hello world";//密钥


	private static TripleDESEncrypt encrypt = new TripleDESEncrypt();
	
	public static TripleDESEncrypt getInstance()
	{
		return encrypt;
	}
	
	// 24字节的密钥


	/*
	 * @ use DESede algorithm to enc the src
	 * 
	 * @ keybyte: secretkey 24 byte
	 * 
	 * @ src:the text needs to be encrypt
	 * 
	 * @ return the enc result
	 */
	public  byte[] encryptMode(byte[] src) {
		
		try {
			System.out.println("密钥:"+new String(keyBytes,"utf-8"));
			SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
			Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
			c1.init(Cipher.ENCRYPT_MODE, deskey);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}


	
	
	
	//加密
	public byte[] encrypt(String src) throws Exception
	{
		String srcString = encode(encryptMode(src.getBytes("UTF-8")));
		return srcString.getBytes("UTF-8");
	}
	
	/**
	 * 解密(currentKey解密失败,使用preKey解密)
	 * @param src
	 * @return
	 * @throws Exception 
	 * @throws Exception
	 */
	public String decrypt(byte[] src) throws Exception{
		byte[] srcBytes = null;
		String decryptStr = "";
		String srcString = new String(src,"UTF-8");
		
		try {
			//keyBytes = CryptoKeyUtil.getCryptoKey(CryptoKeyUtil.CURRENT).getBytes();
			srcBytes = decryptMode(decode(srcString),keyBytes);
		} catch (Exception e) {
			keyBytes = CryptoKeyUtil.getCryptoKey(CryptoKeyUtil.PRE).getBytes();
			System.out.println("*****************currentKey解密失败,使用preKey解密******************");
			srcBytes = decryptMode(decode(srcString),keyBytes);
			e.printStackTrace();
		}
		decryptStr = new String(srcBytes,"UTF-8");
		return decryptStr;
	}
	
	public  byte[] decryptMode(byte[] src , byte[] keyBytes) throws Exception{
		SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
		Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
		c1.init(Cipher.DECRYPT_MODE, deskey);
		return c1.doFinal(src);
		
	}
	
	/*
	 * @ use DESede algorithm to dec the src
	 * 
	 * @ keybyte: secretkey 24 byte
	 * 
	 * @ src:the text needs to be dec
	 * 
	 * @ return the dec result
	 */
	public  byte[] decryptMode(byte[] src) {
		try {
			SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
			Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
			c1.init(Cipher.DECRYPT_MODE, deskey);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}


	private void writeFile(String src) throws Exception {
		File file = new File("C://encrypt.txt");
		FileWriter fw = new FileWriter(file);
		fw.write(src);
		fw.close();
	}


	private String readFile(String path) {
		File file = new File(path);
		FileReader fr = null;
		try {
			fr = new FileReader(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		char[] cbuf = new char[10240];
		try {
			fr.read(cbuf);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String res = new String(cbuf);
		return res;
	}


	/**
	 * 编码
	 * 
	 * @param bstr
	 * @return String
	 */
	public  String encode(byte[] bstr) {
		return new sun.misc.BASE64Encoder().encode(bstr);
	}


	/**
	 * 解码
	 * 
	 * @param str
	 * @return string
	 */
	public  byte[] decode(String str) {
		byte[] bt = null;
		try {
			sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
			bt = decoder.decodeBuffer(str);
		} catch (IOException e) {
			e.printStackTrace();
		}


		return bt;
	}


	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {


		TripleDESEncrypt encrypt = new TripleDESEncrypt();
		// TODO Auto-generated method stub
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
//		String keyString ="This is a secret keynews";
//		byte[] keyBytes = keyString.getBytes();
//		System.out.println("密钥是:"+new String(keyBytes));
		String szSrc = "AFE0AFE6-8DBA-E6CB-3240-103AC6F94766";
		System.out.println("加密前的字符串:" + szSrc);
		byte[] encoded = TripleDESEncrypt.getInstance().encryptMode(szSrc.getBytes("UTF-8"));
		String encodedString = encrypt.encode(encoded);
		System.out.println("加密后的字符串:" + encodedString);
		
		//encodedString = "EBtjunqwdP67jCPdyAg0rnpGJed/nHTo32EdvpLNSd3+OGgW+BsNow==";
		
		String urlEncodeStr = URLEncoder.encode(encodedString, "UTF-8");
		System.out.println("UTF-9编码的字符串:" + urlEncodeStr);
		
		try {
			encrypt.writeFile(encodedString);
		} catch (Exception e) {
			e.printStackTrace();
		}	
		byte[] srcBytes = TripleDESEncrypt.getInstance().decryptMode(encrypt.decode(encodedString));
		System.out.println("解密后的字符串:" + (new String(srcBytes,"UTF-8")));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值