Base64加密和解密解決方案(個人項目經驗)

在使用base64加密的時候會出現很多問題,在這裏給大家分享一下,下面就是base64加密和解密代碼,僅供參考。

在Base64Testl.java類中書寫以下代碼:

package com.millery.base64.utils;

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

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

import org.apache.commons.codec.binary.Base64;

/**
 * 
 * @項目名稱:millery-base64
 * @類名稱:Base64Test
 * @類描述:base64加密解密工具類
 * @創建人:millery 
 * @創建時間:2016年1月5日 上午10:25:04 
 * @version:
 */
public class Base64Test {
	private final static String HEX = "0123456789EFGHIJ";
	//這裏IV和myKey必須是8位,超過或者少於8位會報錯
	private final static byte[] IV = { 0, 2, 4, 5, 6, 7, 9 };
	private static String myKey = "miller+u";

/**
	 * 
	 * @描述:base64加密
	 * @創建人:millery
	 * @創建時間:2016年1月5日 上午10:26:56
	 * @param prePassword
	 * @param secret
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String prePassword, String secret)
			throws Exception {
		SecretKeySpec key = new SecretKeySpec(secret.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV));

		byte[] results = cipher.doFinal(prePassword.getBytes());
		return Base64.encodeBase64String(results);
	}

	/**
	 * 
	 * @描述:base64解密
	 * @創建人:millery
	 * @創建時間:2016年1月5日 上午10:27:15
	 * @param buf
	 * @return
	 */
	public static String decrypt(String encStr, String secret) throws Exception {
		SecretKeySpec key = new SecretKeySpec(secret.getBytes(), "DES");
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV));

		byte[] result = cipher.doFinal(Base64.decodeBase64(encStr));
		return new String(result);
	}

	public static String toHex(byte[] buf) {
		if (buf == null)
			return "";
		StringBuilder result = new StringBuilder(2 * buf.length);
		for (int i = 0; i < buf.length; i++) {
			result.append(HEX.charAt((buf[i] >> 4) & 0x0f)).append(
					HEX.charAt(buf[i] & 0x0f));
		}
		return result.toString();
	}

	public static byte[] toByte(String hexString) {
		int len = hexString.length() / 2;
		byte[] result = new byte[len];
		for (int i = 0; i < len; i++)
			result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
					16).byteValue();
		return result;
	}

	/**
	 * 
	 * @描述:base64加密
	 * @創建人:millery
	 * @創建時間:2016年1月5日 上午9:35:38
	 * @param plainText
	 * @return
	 */
	public static String encrypt(String plainText) {
		try {
			String enKey = encrypt(plainText, myKey);
			// 爲了防止加密後出現特殊字符,比如"+",參數是傳遞不過去的
			enKey = URLEncoder.encode(enKey, "utf-8");
			return enKey;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @描述:base64解密
	 * @創建人:millery
	 * @創建時間:2015年12月28日 下午2:57:54
	 * @param encryptedText
	 * @return
	 */
	public static String decrypt(String encryptedText) {
		try {
			if (encryptedText.indexOf("%") != -1) {
				encryptedText = URLDecoder.decode(encryptedText, "utf-8");
			}
			return MyCodec.decrypt(encryptedText, myKey);
		} catch (Exception e) {
			return "false";
		}
	}

	/**
	 * 
	 * @描述:測試主方法
	 * @創建人:millery
	 * @創建時間:2016年1月5日 上午9:35:24
	 * @param args
	 */
	public static void main(String[] args) {
		String str = encrypt("123456789");
		System.out.println(str);
		System.out.println(decrypt(str));
	}
}

上面的代碼直接運行main方法加密和解密接可以了。


發佈了44 篇原創文章 · 獲贊 68 · 訪問量 54萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章