[J2SE]Java SE Security初探 -- 加解密

这里加解密以DES为例

首先加密解决过程的几个点作简要的说明
1.加密模式及补位
下面两行代码是等价,也就是JDK中对DES默认的算法是ECB模式,PKCS5Padding的补位方式(由于对加解密算法知识有限,所以就不对模式跟补位方式作解释)
Cipher cipher = Cipher.getInstance("DES");
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

2.固定密钥生成,DESKeySpec的参数必须是byte[8],也就是如果密钥超过8位,加密效果是一样的,而如果不够8位,一定要进行补位,一般用0补位
DESKeySpec keySpec = new DESKeySpec(generateDESKeyBytes("orz"));


具体步骤:
1.生成固定密钥,这里为了简化过程,完全可以做成随机密钥,随内容分发
	/**
	 * 生成固定密钥 设定密钥的为orz
	 * 
	 * @return
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	private static SecretKey generateDES_ECBMode_Key()
			throws InvalidKeyException, NoSuchAlgorithmException,
			InvalidKeySpecException {
		// 参数必须是byte[8]的数组
		DESKeySpec keySpec = new DESKeySpec(generateDESKeyBytes("orz"));

		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey key = keyFactory.generateSecret(keySpec);
		return key;
	}

 /**
	 * 生成byte[8]的数组,不足用0补足
	 * 
	 * @param keyWord
	 * @return
	 */
	private static byte[] generateDESKeyBytes(String keyWord) {
		byte[] key = new byte[8];

		byte[] tmp = keyWord.getBytes();
		for (int i = 0; i < key.length; i++) {
			if (i < tmp.length)
				key[i] = tmp[i];
		}
		return key;
	}



2.加密,返回16进制的字符串
	public static String cryptDES(String plainText)
			throws NoSuchAlgorithmException, NoSuchPaddingException,
			InvalidKeyException, InvalidKeySpecException,
			IllegalBlockSizeException, BadPaddingException {
		SecureRandom random = new SecureRandom();
		String encryptText = null;
		byte[] result = null;
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.ENCRYPT_MODE, generateDES_ECBMode_Key(), random);
		
		result = cipher.doFinal(plainText.getBytes());
		encryptText = StringBytesTransformUtils.bytesToHexString(result);
		return encryptText;
	}


3.解密
public static String decryptDES(String encryptText)
			throws NoSuchAlgorithmException, NoSuchPaddingException,
			InvalidKeyException, InvalidKeySpecException,
			IllegalBlockSizeException, BadPaddingException {
		SecureRandom random = new SecureRandom();
		String decryptText = null;
		byte[] result = null;
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.DECRYPT_MODE, generateDES_ECBMode_Key(), random);
		result = cipher.doFinal(StringBytesTransformUtils
				.hexStringToBytes(encryptText));
		// 还原原来的数据
		decryptText = new String(result);
		return decryptText;
	}


String-byte[]转换器,下面这段代码来自网上
/**
 * String-byte[]的转换器
 * 
 * @author Kevin Chen
 * 
 */
public class StringBytesTransformUtils {
	public static String bytesToHexString(byte[] bArray) {

		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	public static byte[] hexStringToBytes(String hexString) {
		int len = (hexString.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hexString.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	public static byte toByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(c);
		return b;
	}

}

你可能感兴趣的:(java,jdk,算法,J2SE,Security)