国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java中DES加密解密

2019-11-26 15:16:13
字體:
來源:轉載
供稿:網友

廢話不多說,直接奉上代碼:

代碼一

package com.eabax.plugin.yundada.utils;import java.io.IOException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.security.spec.InvalidKeySpecException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import org.apache.commons.codec.binary.Base64;import sun.misc.BASE64Decoder;public class DESEncryptHelper {  private final static String DES = "DES";  /**   * 生成密鑰   * @param employeeCode   */  public static String getDESKey(String encryptStr){    if (!CacheManager.getCache().containsKey("encryptKey_"+encryptStr)) {      CacheManager.getCache().put("encryptKey_"+encryptStr, encryptStr+"tablemiyaokey");    }    String key = (String) CacheManager.getCache().get("encryptKey_"+encryptStr);    return key;  }   /**   * Description 根據鍵值進行解密   * @param data   * @param key 加密鍵byte數組   * @return   * @throws IOException   * @throws Exception   */  public static String decrypt(String data, String key) throws IOException,      Exception {    if (data == null)      return null;    BASE64Decoder decoder = new BASE64Decoder();    byte[] buf = decoder.decodeBuffer(data);    byte[] bt = decrypt(buf,key.getBytes());    return new String(bt);  }  /**   * 對字符串加密    * @param str   * @return   * @throws InvalidKeyException   * @throws IllegalBlockSizeException   * @throws BadPaddingException   * @throws InvalidKeySpecException   * @throws NoSuchAlgorithmException   * @throws NoSuchPaddingException   */  public static String getEncryptStr(String str,String encryptStr) throws InvalidKeyException,      IllegalBlockSizeException, BadPaddingException,      InvalidKeySpecException, NoSuchAlgorithmException,      NoSuchPaddingException {    //獲取key    String key = getDESKey(encryptStr);    //獲取密鑰    SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");    DESKeySpec keyspec = new DESKeySpec(key.getBytes());    SecretKey deskey = factory.generateSecret(keyspec);    // Cipher負責完成加密或解密工作    Cipher c = Cipher.getInstance("DES");    // 根據密鑰,對Cipher對象進行初始化,DECRYPT_MODE表示加密模式     c.init(Cipher.ENCRYPT_MODE, deskey);    byte[] src = str.getBytes();    // 該字節數組負責保存加密的結果    byte[] cipherByte = c.doFinal(src);    String enstr = new String(Base64.encodeBase64(cipherByte));    return enstr;  }   /**   * Description 根據鍵值進行解密   * @param data   * @param key 加密鍵byte數組   * @return   * @throws Exception   */  private static byte[] decrypt(byte[] data, byte[] key) throws Exception {    // 生成一個可信任的隨機數源    SecureRandom sr = new SecureRandom();    // 從原始密鑰數據創建DESKeySpec對象    DESKeySpec dks = new DESKeySpec(key);    // 創建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);    SecretKey securekey = keyFactory.generateSecret(dks);    // Cipher對象實際完成解密操作    Cipher cipher = Cipher.getInstance(DES);    // 用密鑰初始化Cipher對象    cipher.init(Cipher.DECRYPT_MODE, securekey, sr);    return cipher.doFinal(data);  }}

代碼二

package com.sinosoft.olyvem.common;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import sun.misc.BASE64Encoder;public class DES ...{  private byte[] desKey;  public DES(byte[] desKey) ...{    this.desKey = desKey;  }  public byte[] doEncrypt(byte[] plainText) throws Exception ...{    // DES算法要求有一個可信任的隨機數源    SecureRandom sr = new SecureRandom();    byte rawKeyData[] = desKey;/**//* 用某種方法獲得密匙數據 */    // 從原始密匙數據創建DESKeySpec對象    DESKeySpec dks = new DESKeySpec(rawKeyData);    // 創建一個密匙工廠,然后用它把DESKeySpec轉換成    // 一個SecretKey對象    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");    SecretKey key = keyFactory.generateSecret(dks);    // Cipher對象實際完成加密操作    Cipher cipher = Cipher.getInstance("DES");    // 用密匙初始化Cipher對象    cipher.init(Cipher.ENCRYPT_MODE, key, sr);    // 現在,獲取數據并加密    byte data[] = plainText;/**//* 用某種方法獲取數據 */    // 正式執行加密操作    byte encryptedData[] = cipher.doFinal(data);    return encryptedData;  }  public byte[] doDecrypt(byte[] encryptText) throws Exception ...{    // DES算法要求有一個可信任的隨機數源    SecureRandom sr = new SecureRandom();    byte rawKeyData[] = desKey; /**//* 用某種方法獲取原始密匙數據 */    // 從原始密匙數據創建一個DESKeySpec對象    DESKeySpec dks = new DESKeySpec(rawKeyData);    // 創建一個密匙工廠,然后用它把DESKeySpec對象轉換成    // 一個SecretKey對象    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");    SecretKey key = keyFactory.generateSecret(dks);    // Cipher對象實際完成解密操作    Cipher cipher = Cipher.getInstance("DES");    // 用密匙初始化Cipher對象    cipher.init(Cipher.DECRYPT_MODE, key, sr);    // 現在,獲取數據并解密    byte encryptedData[] = encryptText;/**//* 獲得經過加密的數據 */    // 正式執行解密操作    byte decryptedData[] = cipher.doFinal(encryptedData);    return decryptedData;  }  public static void main(String[] args) throws Exception ...{    String key = "FtpXPass";    String value = "olympic";    BASE64Encoder base64Encoder = new BASE64Encoder();    DES desEncrypt = new DES(key.getBytes());    byte[] encryptText = desEncrypt.doEncrypt(value.getBytes());    //System.out.println("doEncrypt  -  " + toHexString(encryptText));    System.out.println("doEncrypt  -  "        + base64Encoder.encode(encryptText));    byte[] decryptText = desEncrypt.doDecrypt("r9NGYcKAtdo=".getBytes());    System.out.println("doDecrypt  -  " + new String(decryptText));    //System.out.println("doDecrypt  -  " + toHexString(decryptText));  }  public static String toHexString(byte[] value) ...{    String newString = "";    for (int i = 0; i < value.length; i++) ...{      byte b = value[i];      String str = Integer.toHexString(b);      if (str.length() > 2) ...{        str = str.substring(str.length() - 2);      }      if (str.length() < 2) ...{        str = "0" + str;      }      newString += str;    }    return newString.toUpperCase();  }}

以上就是本文關于DES加密解密的代碼了,希望對大家學習java有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 勃利县| 民和| 岐山县| 龙里县| 潜山县| 龙山县| 石狮市| 宁河县| 武山县| 通江县| 清苑县| 青冈县| 新沂市| 微博| 明水县| 西城区| 镇坪县| 南部县| 黔江区| 鸡泽县| 岫岩| 宜昌市| 寿光市| 武平县| 庆城县| 蒙阴县| 仲巴县| 南平市| 宣城市| 内江市| 台中县| 武强县| 房产| 客服| 安宁市| 进贤县| 卫辉市| 叶城县| 大英县| 洛扎县| 乌拉特前旗|