最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java基于Des對(duì)稱(chēng)加密算法實(shí)現(xiàn)的加密與解密功能詳解

 更新時(shí)間:2017年01月05日 11:46:40   作者:QH_JAVA  
這篇文章主要介紹了java基于Des對(duì)稱(chēng)加密算法實(shí)現(xiàn)的加密與解密功能,結(jié)合實(shí)例形式詳細(xì)分析了Des加密算法的功能、原理、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了java基于Des對(duì)稱(chēng)加密算法實(shí)現(xiàn)的加密與解密功能。分享給大家供大家參考,具體如下:

Des 加密相關(guān)類(lèi)介紹:

SecureRandom  這個(gè)類(lèi)是繼承自java.util.Random 這個(gè)類(lèi)

SecureRandom 這個(gè)類(lèi)的構(gòu)造器有三種,下面例舉兩種:

SecureRandom()構(gòu)造一個(gè)實(shí)現(xiàn)默認(rèn)隨機(jī)數(shù)算法的安全隨機(jī)數(shù)生成器 (RNG)。

SecureRandom(byte[] seed)構(gòu)造一個(gè)實(shí)現(xiàn)默認(rèn)隨機(jī)數(shù)算法的安全隨機(jī)數(shù)生成器 (RNG)。

DESKeySpec 這個(gè)類(lèi)是用來(lái)使用原始秘鑰來(lái)生成秘鑰的秘鑰內(nèi)容

DESKeySpec 有兩個(gè)構(gòu)造函數(shù):

DESKeySpec(byte[] key) 創(chuàng)建一個(gè) DESKeySpec 對(duì)象,使用 key 中的前 8 個(gè)字節(jié)作為 DES 密鑰的密鑰內(nèi)容。

DESKeySpec(byte[] key, int offset) 創(chuàng)建一個(gè) DESKeySpec 對(duì)象,使用 key 中始于且包含 offset 的前 8 個(gè)字節(jié)作為 DES-EDE 密鑰的密鑰內(nèi)容。

SecretKeyFactory , 密鑰工廠用來(lái)將密鑰(類(lèi)型 Key 的不透明加密密鑰)轉(zhuǎn)換為密鑰規(guī)范(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對(duì)秘密(對(duì)稱(chēng))密鑰進(jìn)行操作。

SecretKey對(duì)象,秘鑰對(duì)象,通過(guò)調(diào)用秘鑰工廠的generateSecret(DESKeySpec deskeyspace) 方法來(lái)生成秘鑰

Cipher 類(lèi)為加密和解密提供密碼功能,通過(guò)調(diào)用Cipher的getInstance("des") 來(lái)獲取實(shí)例

Cipher 對(duì)象調(diào)用init() 方法進(jìn)行對(duì)象的初始化,init() 方法的具體參數(shù)按照具體情況而定,有加密的也有解密的常量

最后調(diào)用Cipher的doFinal() 方法進(jìn)行加密解密。

在這里請(qǐng)教大家一個(gè)問(wèn)題,不管是第一種使用BASE64Encoder編碼還是第二種org.apache.commons.codec.binary.Base64編碼,在將String 轉(zhuǎn)化為byte以及將byte轉(zhuǎn)化為String 時(shí)需要 UTF-8/GBK 等編碼來(lái)編碼,解碼嗎?

一、使用了 sun.misc.BASE64Decoder 和BASE64Encoder 進(jìn)行解碼,編碼

package com.soufun.com;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
// 導(dǎo)入sun的64位編碼
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 *@author WHD
 *
 *即使導(dǎo)入sun.misc這個(gè)架包也會(huì)報(bào)錯(cuò),這時(shí)首先把你的JRE架包移除再導(dǎo)入一次就可以了
 */
public class DesUtil {
  // 定義加密方式
   private final static String DES = "DES";
   private final static String UTF8="GBK";
   static SecretKeyFactory keyFactory = null;
  static {
    try {
      keyFactory=SecretKeyFactory.getInstance("DES");
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
    public static void main(String[] args) throws Exception {
      long begin=new Date().getTime();
      String data = "aaades加密測(cè)試";
      // 注意:DES加密和解密過(guò)程中,密鑰長(zhǎng)度都必須是8的倍數(shù)
      String key = "qazwsxed";
      System.err.println(encrypt(data, key));
      System.err.println(decrypt(encrypt(data, key), key));
      long end =new Date().getTime();
      System.out.println(end-begin);
    }
    /**
     * Description 根據(jù)鍵值進(jìn)行加密
     * @param data
     * @param key 加密鍵byte數(shù)組
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
      // 使用指定的編碼獲取要加密的內(nèi)容,一般秘鑰都是字母或數(shù)字不用指定編碼,但指定也可以
      byte[] bt = encrypt(data.getBytes(UTF8), key.getBytes(UTF8));
      //注意:在加密和解密的時(shí)候使用sun的BASE64Encoder()進(jìn)行編碼和解碼不然會(huì)有亂碼
      //網(wǎng)上查看了很多實(shí)例,都沒(méi)有編碼和解碼,也說(shuō)沒(méi)有亂碼問(wèn)題,而我這里出現(xiàn)了亂碼,所以使用BASE64Encoder()進(jìn)行了編碼解碼
      String strs = new BASE64Encoder().encode(bt);
      return strs;
    }
    /**
     * Description 根據(jù)鍵值進(jìn)行解密
     * @param data
     * @param key 加密鍵byte數(shù)組
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException,
        Exception {
      if (data == null)
        return null;
      //注意:在加密和解密的時(shí)候使用sun的BASE64Encoder()進(jìn)行編碼和解碼不然會(huì)有亂碼
      BASE64Decoder decoder = new BASE64Decoder();
      byte[] buf = decoder.decodeBuffer(data);
      byte[] bt = decrypt(buf,key.getBytes());
      return new String(bt,UTF8);
    }
    /**
     * Description 根據(jù)鍵值進(jìn)行加密
     * @param data
     * @param key 加密鍵byte數(shù)組
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
      // 生成一個(gè)可信任的隨機(jī)數(shù)源
      SecureRandom sr = new SecureRandom();
      // 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象,也就是創(chuàng)建秘鑰的秘鑰內(nèi)容
      DESKeySpec dks = new DESKeySpec(key);
      // 密鑰工廠用來(lái)將密鑰(類(lèi)型 Key 的不透明加密密鑰)轉(zhuǎn)換為密鑰規(guī)范(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對(duì)秘密(對(duì)稱(chēng))密鑰進(jìn)行操作。
      // 這里改為使用單例模式
      //SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
      //根據(jù)提供的密鑰規(guī)范(密鑰材料)生成 SecretKey(秘鑰) 對(duì)象。
      SecretKey securekey = keyFactory.generateSecret(dks);
      // Cipher對(duì)象實(shí)際完成加密操作,此類(lèi)為加密和解密提供密碼功能
      Cipher cipher = Cipher.getInstance(DES);
      // 用密鑰和隨機(jī)源初始化此 Cipher。ENCRYPT_MODE用于將 Cipher 初始化為加密模式的常量。
      cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
      //正式執(zhí)行加密操作
      return cipher.doFinal(data);
    }
    /**
     * Description 根據(jù)鍵值進(jìn)行解密
     * @param data
     * @param key 加密鍵byte數(shù)組
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
      // 生成一個(gè)可信任的隨機(jī)數(shù)源
      SecureRandom sr = new SecureRandom();
      // 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象,也就是創(chuàng)建秘鑰的秘鑰內(nèi)容
      DESKeySpec dks = new DESKeySpec(key);
      // 密鑰工廠用來(lái)將密鑰(類(lèi)型 Key 的不透明加密密鑰)轉(zhuǎn)換為密鑰規(guī)范(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對(duì)秘密(對(duì)稱(chēng))密鑰進(jìn)行操作。
      // 這里改為使用單例模式
      //SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
      //根據(jù)提供的密鑰規(guī)范(密鑰材料)生成 SecretKey(秘鑰)對(duì)象。
      SecretKey securekey = keyFactory.generateSecret(dks);
      // Cipher類(lèi)為加密和解密提供密碼功能
      Cipher cipher = Cipher.getInstance(DES);
      // DECRYPT_MODE用于將 Cipher 初始化為解密模式的常量。
      cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
      // 正式進(jìn)行解密操作
      return cipher.doFinal(data);
    }
}

二、使用org.apache.commons.codec.binary.Base64 進(jìn)行解碼,編碼

package com.soufun.com;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
 *@author WHD
 *
 */
public class DesUtil {
  // 定義加密方式
   private final static String DES = "DES";
   private final static String UTF8="GBK";
   static SecretKeyFactory keyFactory = null;
  static {
    try {
      keyFactory=SecretKeyFactory.getInstance("DES");
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
    public static void main(String[] args) throws Exception {
      long begin=new Date().getTime();
      String data = "aaades加密測(cè)試";
      // 注意:DES加密和解密過(guò)程中,密鑰長(zhǎng)度都必須是8的倍數(shù)
      String key = "qazwsxed";
      System.err.println(encrypt(data, key));
      System.err.println(decrypt(encrypt(data, key), key));
      long end =new Date().getTime();
      System.out.println(end-begin);
    }
    /**
     * Description 根據(jù)鍵值進(jìn)行加密
     * @param data
     * @param key 加密鍵byte數(shù)組
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
      // 使用指定的編碼獲取要加密的內(nèi)容,一般秘鑰都是字母或數(shù)字不用指定編碼,但指定也可以
      byte[] bt = encrypt(data.getBytes(UTF8), key.getBytes());
      // 第一個(gè)使用了sun.misc.BASE64Encoder;進(jìn)行了編碼,但網(wǎng)上說(shuō)使用org.apache.commons.codec.binary.Base64比較好所以拿來(lái)試試
      String strs = Base64.encodeBase64String(bt);
      return strs;
    }
    /**
     * Description 根據(jù)鍵值進(jìn)行解密
     * @param data
     * @param key 加密鍵byte數(shù)組
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException,
        Exception {
      if (data == null)
        return null;
      // 使用org.apache.commons.codec.binary.Base64解碼
      byte [] buf=Base64.decodeBase64(data);
      byte[] bt = decrypt(buf,key.getBytes());
      return new String(bt,UTF8);
    }
    /**
     * Description 根據(jù)鍵值進(jìn)行加密
     * @param data
     * @param key 加密鍵byte數(shù)組
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
      // 生成一個(gè)可信任的隨機(jī)數(shù)源
      SecureRandom sr = new SecureRandom();
      // 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象,也就是創(chuàng)建秘鑰的秘鑰內(nèi)容
      DESKeySpec dks = new DESKeySpec(key);
      // 密鑰工廠用來(lái)將密鑰(類(lèi)型 Key 的不透明加密密鑰)轉(zhuǎn)換為密鑰規(guī)范(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對(duì)秘密(對(duì)稱(chēng))密鑰進(jìn)行操作。
      // 這里改為使用單例模式
      //SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
      //根據(jù)提供的密鑰規(guī)范(密鑰材料)生成 SecretKey(秘鑰) 對(duì)象。
      SecretKey securekey = keyFactory.generateSecret(dks);
      // Cipher對(duì)象實(shí)際完成加密操作,此類(lèi)為加密和解密提供密碼功能
      Cipher cipher = Cipher.getInstance(DES);
      // 用密鑰和隨機(jī)源初始化此 Cipher。ENCRYPT_MODE用于將 Cipher 初始化為加密模式的常量。
      cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
      //正式執(zhí)行加密操作
      return cipher.doFinal(data);
    }
    /**
     * Description 根據(jù)鍵值進(jìn)行解密
     * @param data
     * @param key 加密鍵byte數(shù)組
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
      // 生成一個(gè)可信任的隨機(jī)數(shù)源
      SecureRandom sr = new SecureRandom();
      // 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象,也就是創(chuàng)建秘鑰的秘鑰內(nèi)容
      DESKeySpec dks = new DESKeySpec(key);
      // 密鑰工廠用來(lái)將密鑰(類(lèi)型 Key 的不透明加密密鑰)轉(zhuǎn)換為密鑰規(guī)范(底層密鑰材料的透明表示形式),反之亦然。秘密密鑰工廠只對(duì)秘密(對(duì)稱(chēng))密鑰進(jìn)行操作。
      // 這里改為使用單例模式
      //SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
      //根據(jù)提供的密鑰規(guī)范(密鑰材料)生成 SecretKey(秘鑰)對(duì)象。
      SecretKey securekey = keyFactory.generateSecret(dks);
      // Cipher類(lèi)為加密和解密提供密碼功能
      Cipher cipher = Cipher.getInstance(DES);
      // DECRYPT_MODE用于將 Cipher 初始化為解密模式的常量。
      cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
      // 正式進(jìn)行解密操作
      return cipher.doFinal(data);
    }
}

一、二中使用到的架包下載地址:

下載: sun.misc.BASE64Decoder
下載:apache的Base64編碼、解碼器 。

三、未使用任何編碼,解碼架包

package com.soufun.com;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
 *@author WHD
 *
 */
public class DESCrypt {
  static SecretKeyFactory secretKeyFactory = null;
  //Cipher 的“算法/模式/填充”
  static final String CIPHER = "DES/CBC/PKCS5Padding";
  static {
    try {
      // 在靜態(tài)代碼塊中獲取秘鑰工程
      secretKeyFactory = SecretKeyFactory.getInstance("DES");
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
  }
  // 定義常量 ,編碼格式
  private static final String UTF8 = "GBK";
  /*
   * 對(duì)象緩存的容器
   */
  static abstract class Cache {
    private final Map innerCache = new HashMap();
    protected abstract Object createValue(Object key) throws Exception;
    public Object get(Object key) throws Exception {
      Object value;
      synchronized (innerCache) {
        value = innerCache.get(key);
        if (value == null) {
          value = new CreationPlaceholder();
          innerCache.put(key, value);
        }
      }
      if (value instanceof CreationPlaceholder) {
        synchronized (value) {
          CreationPlaceholder progress = (CreationPlaceholder) value;
          if (progress.value == null) {
            progress.value = createValue(key);
            synchronized (innerCache) {
              innerCache.put(key, progress.value);
            }
          }
          return progress.value;
        }
      }
      return value;
    }
    static final class CreationPlaceholder {
      Object value;
    }
  }
  /*
   * hex->str & str->hex
   */
  public static byte[] stringToHex(String ss) {
    // 字符串轉(zhuǎn)化we
    byte digest[] = new byte[ss.length() / 2];
    for (int i = 0; i < digest.length; i++) {
      String byteString = ss.substring(2 * i, 2 * i + 2);
      int byteValue = Integer.parseInt(byteString, 16);
      digest[i] = (byte) byteValue;
    }
    return digest;
  }
  public static String hexToString(byte b[]) {
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < b.length; i++) {
      String plainText = Integer.toHexString(0xff & b[i]);
      if (plainText.length() < 2) {
        hexString.append("0");
      }
      hexString.append(plainText);
    }
    return hexString.toString();
  }
  private static byte[] _convertKeyIv(String text) throws IOException {
    if (text.length() == 8) {
      return text.getBytes(UTF8);
    }
    if (text.startsWith("0x") && text.length() == 32) {
      byte[] result = new byte[8];
      for (int i = 0; i < text.length(); i += 2) {
        if (text.charAt(i++) == '0' && text.charAt(i++) == 'x') {
          try {
            result[i / 4] = (byte) Integer.parseInt(
                text.substring(i, i + 2), 16);
          } catch (Exception e) {
            throw new IOException("TXT '" + text + "' is invalid!");
          }
        }
      }
      return result;
    }
    throw new IOException("TXT '" + text + "' is invalid!");
  }
  /*
   * SecretKey & IvParameterSpec的緩存
   */
  private static Cache SecretKeySpecs = new Cache() {
    protected Object createValue(Object key) throws Exception {
      SecretKey secretKeyObj = null;
      try {
        secretKeyObj = secretKeyFactory.generateSecret(new DESKeySpec(
            _convertKeyIv((String) key)));
      } catch (Exception e) {
        e.printStackTrace();
      }
      return secretKeyObj;
    }
  };
  private static Cache IvParamSpecs = new Cache() {
    protected Object createValue(Object key) throws Exception {
      IvParameterSpec ivObj = null;
      ivObj = new IvParameterSpec(_convertKeyIv((String) key));
      return ivObj;
    }
  };
  /*
   * 加密&解密
   */
  public static String encrypt(String text, String authKey, String authIv) {
    SecretKey secretKeyObj = null;
    IvParameterSpec ivObj = null;
    try {
      secretKeyObj = (SecretKey) SecretKeySpecs.get(authKey);
      ivObj = (IvParameterSpec) IvParamSpecs.get(authIv);
    } catch (Exception e) {
      e.printStackTrace();
    }
    byte[] data = null;
    try {
      data = text.getBytes(UTF8);
    } catch (Exception e) {
      e.printStackTrace();
    }
    byte[] authToken = null;
    try {
      authToken = encrypt(data, secretKeyObj, ivObj);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return hexToString(authToken);
  }
  public static byte[] encrypt(byte[] data, SecretKey secretKey,
      IvParameterSpec iv) throws Exception {
    Cipher cipher = Cipher.getInstance(CIPHER);
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
    return cipher.doFinal(data);
  }
  public static String decrypt(String hexString, String authKey, String authIv)
      throws Exception {
    SecretKey secretKeyObj = null;
    IvParameterSpec ivObj = null;
    try {
      secretKeyObj = (SecretKey) SecretKeySpecs.get(authKey);
      ivObj = (IvParameterSpec) IvParamSpecs.get(authIv);
    } catch (Exception e) {
      e.printStackTrace();
    }
    String text = decrypt(hexString, secretKeyObj, ivObj);
    return text;
  }
  public static String decrypt(String message, SecretKey secretKey,
      IvParameterSpec iv) throws Exception {
    byte[] data = stringToHex(message);
    return decrypt(data, secretKey, iv);
  }
  public static String decrypt(byte[] data, SecretKey secretKey,
      IvParameterSpec iv) throws Exception {
    Cipher cipher = Cipher.getInstance(CIPHER);
    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
    byte[] retByte = cipher.doFinal(data);
    return new String(retByte);
  }
  public static void main(String[] args) throws Exception {
    long begin= new Date().getTime();
    String authKey = "w8f3k9c2";
    String authIv = "w8f3k9c2";
    String text = "aaades加密測(cè)試";
    // 140CB412BA03869F
    // 140cb412ba03869f
    // 對(duì)原文進(jìn)行加密
    String encryptedText = encrypt(text, authKey, authIv);
    System.out.println("encryptedText:" + encryptedText);
    // 對(duì)密文進(jìn)行還原
    String plainText = decrypt(encryptedText, authKey, authIv);
    System.out.println("plainText:" + plainText);
    //2a329740ce15f549be64190b183a5be2
    long end =new Date().getTime();
    System.out.println(end-begin);
  }
}

PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:

密碼安全性在線檢測(cè):
http://tools.jb51.net/password/my_password_safe

高強(qiáng)度密碼生成器:
http://tools.jb51.net/password/CreateStrongPassword

迅雷、快車(chē)、旋風(fēng)URL加密/解密工具:
http://tools.jb51.net/password/urlrethunder

在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

顺平县| 太康县| 集贤县| 孙吴县| 平陆县| 海晏县| 三门县| 泰宁县| 九台市| 陇西县| 丹东市| 雷波县| 尼勒克县| 琼中| 巩义市| 六枝特区| 云和县| 横峰县| 西乡县| 海城市| 厦门市| 平邑县| 甘孜县| 靖州| 额尔古纳市| 化德县| 平舆县| 德安县| 安化县| 沅江市| 商河县| 庐江县| 内乡县| 濮阳县| 蕲春县| 屏南县| 平陆县| 元阳县| 清丰县| 凉城县| 新和县|