CryptoTools加密与解密

CryptoTools加密与解密

  java常用的加密算法有MD5(Message Digest algorithm 5,信息摘要算法)

            BASE64(严格地说,属于编码格式,而非加密算法)

            SHA(Secure Hash Algorithm,安全散列算法)

            HMAC(Hash Message Authentication Code,散列消息鉴别码)

            CryptoTools (对称分组式加密)

  前四种是常用的也是基本耳熟的加密算法,这里主要解释CryptoTools算法,现将代码贴与这里,与大家分享。

package com.lzjs.lzgdesbgateway.util;

import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class CryptoTools {
  private final byte[] DESkey = "lzgdesb@123".getBytes("UTF-8");// 设置密钥,略去
  private final byte[] DESIV = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF};//
  设置向量,略去

  private AlgorithmParameterSpec iv = null;// 加密算法的参数接口,IvParameterSpec是它的一个实现
  private Key key = null;

  public CryptoTools() throws Exception {
    DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
    iv = new IvParameterSpec(DESIV);// 设置向量
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
    key = keyFactory.generateSecret(keySpec);// 得到密钥对象

  }

  public String encode(String data) throws Exception {
    Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
    enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量
    byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
    BASE64Encoder base64Encoder = new BASE64Encoder();
    return base64Encoder.encode(pasByte);
}

public String decode(String data) throws Exception {
    Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    deCipher.init(Cipher.DECRYPT_MODE, key, iv);
    BASE64Decoder base64Decoder = new BASE64Decoder();

    byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));

    return new String(pasByte, "UTF-8");
}
public static void main(String[] args) {
  try {
    String test = "4132413241241324";
    CryptoTools des = new CryptoTools();
    System.out.println("加密前的字符:"+test);
    System.out.println("加密后的字符:"+des.encode(test));
    System.out.println("解密后的字符:"+des.decode(des.encode(test)));
  } catch (Exception e) {
  e.printStackTrace();
  }
}
}

posted @ 2017-09-06 14:37  流年煮雪  阅读(2355)  评论(0编辑  收藏  举报