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=

            解密后:测试
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值