⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crypttool.java

📁 Jaoso新闻文章发布系统 0.9.1final 程序架构: Struts+Spring+Hibernate 主要功能:   ·新闻采用在线编辑器,可以象使用word一样编辑新闻,可简繁
💻 JAVA
字号:
package jaoso.framework.util;

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

import java.net.URLDecoder;
import java.net.URLEncoder;

import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * CryptTool ��װ��һЩ���ܹ��߷���, ��( 3DES, MD5 ��.
 * 
 * @author bonjovi
 * @version 1.0 2004-05-12
 */
public class CryptTool {
	/**
	 * Creates a new CryptTool object.
	 */
	public CryptTool() {
	}

	/**
	 * BASE64 ����.
	 * 
	 * @param src
	 *            String inputed string
	 * @return String returned string
	 */
	public static String base64Decode(String src) {
		BASE64Decoder decoder = new BASE64Decoder();

		try {
			return new String(decoder.decodeBuffer(src));
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * BASE64 ����(to byte[]).
	 * 
	 * @param src
	 *            String inputed string
	 * @return String returned string
	 */
	public static byte[] base64DecodeToBytes(String src) {
		BASE64Decoder decoder = new BASE64Decoder();

		try {
			return decoder.decodeBuffer(src);
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * BASE64 ����.
	 * 
	 * @param src
	 *            String inputed string
	 * @return String returned string
	 */
	public static String base64Encode(String src) {
		BASE64Encoder encoder = new BASE64Encoder();

		return encoder.encode(src.getBytes());
	}

	/**
	 * BASE64 ����(byte[]).
	 * 
	 * @param src
	 *            byte[] inputed string
	 * @return String returned string
	 */
	public static String base64Encode(byte[] src) {
		BASE64Encoder encoder = new BASE64Encoder();

		return encoder.encode(src);
	}

	/**
	 * 3DES ����(byte[]).
	 * 
	 * @param key
	 *            SecretKey
	 * @param crypt
	 *            byte[]
	 * @throws Exception
	 * @return byte[]
	 */
	public static byte[] desDecrypt(javax.crypto.SecretKey key, byte[] crypt) {
		Cipher cipher = null;
		byte[] rs = null;

		try {
			cipher = Cipher.getInstance("DESede");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}

		try {
			cipher.init(Cipher.DECRYPT_MODE, key);
		} catch (InvalidKeyException e1) {
			e1.printStackTrace();
		}

		try {
			rs = cipher.doFinal(crypt);
		} catch (IllegalStateException e2) {
			e2.printStackTrace();
		} catch (IllegalBlockSizeException e2) {
			e2.printStackTrace();
		} catch (BadPaddingException e2) {
			e2.printStackTrace();
		}

		return rs;
	}

	/**
	 * 3DES ����(String).
	 * 
	 * @param key
	 *            SecretKey
	 * @param crypt
	 *            byte[]
	 * @throws Exception
	 * @return byte[]
	 */
	public static String desDecrypt(SecretKey key, String crypt) {
		return new String(desDecrypt(key, crypt.getBytes()));
	}

	/**
	 * 3DES����(byte[]).
	 * 
	 * @param key
	 *            SecretKey
	 * @param src
	 *            byte[]
	 * @throws Exception
	 * @return byte[]
	 */
	public static byte[] desEncrypt(SecretKey key, byte[] src) {
		Cipher cipher = null;
		byte[] rs = null;

		try {
			cipher = Cipher.getInstance("DESede");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}

		try {
			cipher.init(Cipher.ENCRYPT_MODE, key);
		} catch (InvalidKeyException e1) {
			e1.printStackTrace();
		}

		try {
			rs = cipher.doFinal(src);
		} catch (IllegalStateException e2) {
			e2.printStackTrace();
		} catch (IllegalBlockSizeException e2) {
			e2.printStackTrace();
		} catch (BadPaddingException e2) {
			e2.printStackTrace();
		}

		return rs;
	}

	/**
	 * 3DES����(String).
	 * 
	 * @param key
	 *            SecretKey
	 * @param src
	 *            byte[]
	 * @throws Exception
	 * @return byte[]
	 */
	public static String desEncrypt(SecretKey key, String src) {
		return new String(desEncrypt(key, src.getBytes()));
	}

	/**
	 * ���3DES��Կ.
	 * 
	 * @param key_byte
	 *            seed key
	 * @throws Exception
	 * @return javax.crypto.SecretKey Generated DES key
	 */
	public static SecretKey genDESKey(byte[] key_byte) {
		//    javax.crypto.spec.DESKeySpec deskeyspec = new
		// javax.crypto.spec.DESKeySpec(
		//        key_byte);
		//    javax.crypto.SecretKeyFactory skf = javax.crypto.SecretKeyFactory.
		//        getInstance("DES", "SunJCE");
		//    return (javax.crypto.SecretKey) skf.generateSecret(deskeyspec);
		//KeyGenerator kg = KeyGenerator.getInstance("DESede");
		SecretKey k = null;
		k = new SecretKeySpec(key_byte, "DESede");

		return k;
	}

	/** Test crypt */
	public static void main(String[] args) {
		byte[] src_byte = "1234567812345678".getBytes();
		System.out.println(src_byte.length);

		byte[] key_byte = "123456781234567812345678".getBytes();

		// 3DES 24 bytes key
		try {
			// ���DES��Կ
			javax.crypto.SecretKey deskey;

			//���DES��Կ
			//      javax.crypto.KeyGenerator key =
			// javax.crypto.KeyGenerator.getInstance(
			//          "DES");
			//      key.init(56);
			//      deskey = key.generateKey();
			deskey = genDESKey(key_byte);
			System.out.println("Generator DES KEY OK");

			// DES�ӽ���
			byte[] encrypt;

			// DES�ӽ���
			byte[] decrypt;

			//����
			encrypt = desEncrypt(deskey, src_byte);
			System.out.println("encrypt=" + new String(encrypt));

			//����
			decrypt = desDecrypt(deskey, encrypt);
			System.out.println("decrypt=" + new String(decrypt));

			//      String s = "12345678";
			//      //����
			//      s = desEncrypt(deskey, s);
			//      System.out.println("encrypt=" + s);
			//      //����
			//      s = desDecrypt(deskey, s);
			//      System.out.println("decrypt=" + s);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		System.out.println("BASE64 Test:" + base64Decode(base64Encode("1234")));
	}

	/**
	 * MD5 ժҪ����(byte[]).
	 * 
	 * @param src
	 *            byte[]
	 * @throws Exception
	 * @return byte[] 16 bit digest
	 */
	public static byte[] md5Digest(byte[] src) {
		MessageDigest alg = null;

		try {
			alg = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		// MD5 is 16 bit message digest
		return alg.digest(src);
	}

	/**
	 * MD5 ժҪ����(String).
	 * 
	 * @param src
	 *            String
	 * @throws Exception
	 * @return String
	 */
	public static String md5Digest(String src) {
		return new String(md5Digest(src.getBytes()));
	}

	/**
	 * �Ը��ַ���� URL ����.
	 * 
	 * @param src
	 *            String
	 * @return String
	 */
	public static String urlEncode(String src) {
		try {
			src = URLEncoder.encode(src, "GB2312");

			return src;
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return src;
	}

	/**
	 * �Ը��ַ���� URL ����
	 * 
	 * @param value
	 *            ����ǰ���ַ�
	 * @return �������ַ�
	 */
	public String urlDecode(String value) {
		try {
			return URLDecoder.decode(value, "GB2312");
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return value;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -