`

DES加解密

阅读更多

 

package com.my.framework.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 说明:DES加解密
 * @version 1.0
 */
public class DESEncrypter {
	/**
	 * 加密Clipher
	 */
	private Cipher enCipher;
	/**
	 * 解密clipher
	 */
	private Cipher deCipher;
	/**
	 * KEY
	 */
	private SecretKey key = null;
	/**
	 * 
	 */
	private static DESEncrypter instance;

	public static synchronized DESEncrypter getInstance() throws InvalidKeyException,
			NoSuchAlgorithmException, NoSuchPaddingException {
		if (instance == null) {
			instance = new DESEncrypter();
			instance.init();
		}
		return instance;
	}

	public synchronized void init() throws NoSuchAlgorithmException,
			NoSuchPaddingException, InvalidKeyException {
		try {
			SecureRandom sr = new SecureRandom();
	        DESKeySpec dks = new DESKeySpec("myKey".getBytes());
	        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			key = keyFactory.generateSecret(dks);
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} 

		enCipher = Cipher.getInstance("DES");
		deCipher = Cipher.getInstance("DES");
		enCipher.init(Cipher.ENCRYPT_MODE, key);
		deCipher.init(Cipher.DECRYPT_MODE, key);
	}

	/**
	 * 加密
	 * 
	 * @param str
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 */
	public String enCrypt(String str) throws UnsupportedEncodingException,
			IllegalBlockSizeException, BadPaddingException {
		byte[] utf8 = str.getBytes("UTF8");
		byte[] enc = enCipher.doFinal(utf8);
		return new BASE64Encoder().encode(enc);
	}

	/**
	 * des解密
	 * 
	 * @param str
	 * @return
	 * @throws IOException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 */
	public String deCrypt(String str) throws IOException,
			IllegalBlockSizeException, BadPaddingException {
		byte[] dec = new BASE64Decoder().decodeBuffer(str);
		byte[] utf8 = deCipher.doFinal(dec);
		return new String(utf8, "UTF8");
	}

	/**
	 * 生成KEY
	 * 
	 * @return
	 */
/*	public static SecretKey generateSecretKey() {
		SecretKey secretKey = null;

		try {
			secretKey = KeyGenerator.getInstance("DES").generateKey();

			FileOutputStream keyFile = new FileOutputStream("d://rd_ds.key");
			ObjectOutputStream keyObject = new ObjectOutputStream(keyFile);
			keyObject.writeObject(secretKey);
			keyFile.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return secretKey;
	}*/

	public static void main(String[] args) throws InvalidKeyException,
			NoSuchAlgorithmException, NoSuchPaddingException,
			IllegalBlockSizeException, BadPaddingException, IOException {
		// DESEncrypter.generateSecretKey();
		DESEncrypter des = DESEncrypter.getInstance();
		String crypt = des.enCrypt("clgl_test");
		System.out.println(crypt);
		String dc = des.deCrypt("irfiM+w48a88LPME0HnP3g==");
		System.out.println(dc);
	}
}

 

备注: DESKeySpec等类来源于jdk

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics