Java 对称加解密(DES,3DES,ASE)和BASE64

package fwk;

import java.io.IOException;
import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

/**
 * this class is used to encrypt and decrypt
 * 
 * @author fangwk
 * @date 2016-8-3 14:11:27
 */
public class CipherUtil {
    /**
     * encrypt base on Base64
     * 
     * @param target
     *            The target (which is used to encrypt) @return @throws
     */
    public static String encryptByBase64(byte[] target) {
	BASE64Encoder encoder = new BASE64Encoder();
	return encoder.encode(target);

    }

    /**
     * decrypt base on Base64
     * 
     * @param target
     *            The target (which is used to decrypt)
     * @return
     */
    public static byte[] decryptByBase64(String target) {
	BASE64Decoder decoder = new BASE64Decoder();
	try {
	    return decoder.decodeBuffer(target);
	} catch (IOException e) {
	    e.printStackTrace();
	}
	return null;

    }

    /**
     * encrypt base on DES or 3DES or AES.
     * 
     * @param target
     *            The target (which is used to encrypt)
     * @param algorithm
     *            The algorithm can be DES or DESede or AES
     * @param key
     *            The key (which is key to handle this encrypt)
     * @return
     */
    public static String encrypt(String target, String algorithm, String key) {
	try {

	    byte[] targetToByte = target.getBytes("UTF-8");
	    Cipher cipher = Cipher.getInstance(algorithm);
	    cipher.init(Cipher.ENCRYPT_MODE, getKey(key, algorithm));
	    byte[] result = cipher.doFinal(targetToByte);
	    // System.out.println("base64:" +encryptByBase64(result));
	    return encryptByBase64(result); // 需要BASE64包装一下,否则会抛出异常

	} catch (Exception e) {
	    e.printStackTrace();
	}
	return null;

    }

    /**
     * decrypt base on DES or 3DES or AES.
     * 
     * @param target
     *            The target (which is used to decrypt)
     * @param algorithm
     *            The algorithm can be DES or DESede or AES
     * @param key
     *            The key (which is key to handle this decrypt)
     * @return
     */
    public static String decrypt(String target, String algorithm, String key) {

	try {

	    byte[] targetToByte = decryptByBase64(target); // 需要BASE64包装一下,否则会抛出异常
	    Cipher cipher = Cipher.getInstance(algorithm);
	    cipher.init(Cipher.DECRYPT_MODE, getKey(key, algorithm));
	    byte[] result = cipher.doFinal(targetToByte);
	    // System.out.println("base:" + new String(result,"UTF-8"));
	    return new String(result, "UTF-8");

	} catch (Exception e) {
	    e.printStackTrace();
	}
	return null;
    }

    public static Key getKey(String key, String algorithm) {
	try {
	    KeyGenerator generator = KeyGenerator.getInstance(algorithm);
	    generator.init(new SecureRandom(key.getBytes()));
	    return generator.generateKey();

	} catch (Exception e) {
	    e.printStackTrace();
	}
	return null;

    }

    public static void main(String[] args) {
	String target = "测试";
	// String target = "test";
	// DES
	String encrypt = encrypt(target, "DES", "im a key");
	System.out.println("加密后:" + encrypt);
	String decrypt = decrypt(encrypt, "DES", "im a key");
	System.out.println("解密后:" + decrypt);
	// 3DES
	/*
	 * String encrypt = encrypt(target, "DESede", "im a key");
	 * System.out.println("加密后:" + encrypt); String decrypt =
	 * decrypt(encrypt, "DESede", "im a key"); System.out.println("解密后:" +
	 * decrypt);
	 */
	// AES
	/*
	 * String encrypt = encrypt(target, "AES", "im a key");
	 * System.out.println("加密后:" + encrypt); String decrypt =
	 * decrypt(encrypt, "AES", "im a key"); System.out.println("解密后:" +
	 * decrypt);
	 */
    }

}

控制台:

            加密后:6Q2FSLKepBE=

            解密后:测试

你可能感兴趣的:(Java)