DES&AES加密算法

package com.goma.crypto.util;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class CryptoUtils {
	private static final String ALGORITHM = "DES";

	
	/**
	 * 将位byte数组转换密钥Key
	 * @param key
	 * @param algorithm
	 * @return
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	private static Key toKey(byte[] key, String algorithm) throws Exception {
		SecretKey secretKey = null;
		if (algorithm.equals(ALGORITHM)) {
			DESKeySpec dks = new DESKeySpec(key);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
			secretKey = keyFactory.generateSecret(dks);
		} else {
			//当使用其他对称加密算法时,如AES、Blowfish等算法时,运行此分支
			secretKey = new SecretKeySpec(key, algorithm);
		}
		return secretKey;

	}

	/**
	 * 根据指定的密钥及算法,将字符串进行解密
	 * @param data
	 * @param key
	 * @param algorithm
	 * @return
	 */
	public static String decrypt(String data, byte[]key, String algorithm) {
		try {
			Cipher cipher = Cipher.getInstance(algorithm);
			cipher.init(Cipher.DECRYPT_MODE, toKey(key, algorithm));
			String result = new String(cipher.doFinal(encryptBASE64(data)), "UTF-8");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据指定的密钥及算法对指定字符串进行可逆加密
	 * @param data
	 * @param key
	 * @param algorithm
	 * @return
	 */
	public static String encrypt(String data, byte[]key, String algorithm) {
		try {
			Cipher cipher = Cipher.getInstance(algorithm);
			cipher.init(Cipher.ENCRYPT_MODE, toKey(key, algorithm));
			return decryptBASE64(cipher.doFinal(data.getBytes("UTF-8")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 生成密钥
	 * @return
	 * @throws Exception
	 */
	public static String initKey() throws Exception {
		return initKey(null);
	}

	/**
	 * 通过SecureRandom类生成密钥
	 * @param seed
	 * @return
	 * @throws Exception
	 */
	public static String initKey(String seed) throws Exception {
		SecureRandom secureRandom = null;
		if (seed != null) {
			secureRandom = new SecureRandom(seed.getBytes());
		} else {
			secureRandom = new SecureRandom();
		}
		KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
		kg.init(secureRandom);
		SecretKey secretKey = kg.generateKey();
		return decryptBASE64(secretKey.getEncoded());
	}
	
	/**
	 * 64位byte数组转换为16进制的字符串
	 * @param data
	 * @return
	 */
	private static final String decryptBASE64(byte[] data) {
		StringBuilder valueHex = new StringBuilder();
		for (int i = 0, tmp; i < data.length; i++) {
			tmp = data[i] & 0xff;
			if (tmp < 16) {
				valueHex.append(0);
			}
			valueHex.append(Integer.toHexString(tmp));
		}
		return valueHex.toString();
	}
	
	/**
	 * 16进制表示的字符串转换为64位byte数组
	 * @param hexString
	 * @return byte[]
	 */
	private static byte[] encryptBASE64(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		char[] hexChars = hexString.toCharArray();
		int length = hexString.length();
		byte[] d = new byte[length >>> 1];
		for (int n = 0; n < length; n += 2) {
			String item = new String(hexChars, n, 2);
			d[n >>> 1] = (byte) Integer.parseInt(item, 16);
		}
		return d;
	}

}

 

package com.goma.crypto.util;


public class CryptoTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			String randomStr = CryptoUtils.initKey("298&*%^$$#^@#*tudfghjkl;'iuytrtyjmnvfgl;");
//			String randomStr = CryptoUtils.initKey();/
			System.out.println("密匙:"+randomStr);
			byte[] key = randomStr.getBytes(); 
//			byte[]key = "ABCDEFGHIJKLMNO".getBytes();
			String rlt = CryptoUtils.encrypt("DES", key, "Blowfish");
			System.out.println("密文:"+rlt);
			System.out.println("原文:"+CryptoUtils.decrypt(rlt, key, "Blowfish"));
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}

 

另可参考:http://www.iteye.com/topic/1122076

http://security.group.iteye.com/group/wiki/1710-one-way-encryption-algorithm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值