亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在Java項目中實現一個DES加密算法

發布時間:2020-11-23 17:03:31 來源:億速云 閱讀:171 作者:Leah 欄目:編程語言

如何在Java項目中實現一個DES加密算法?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。

Base64.java

package com.mstf.des;
 
import java.io.UnsupportedEncodingException;
 
/**
 * base64編碼/解碼
 * @author ceet
 *
 */
public class Base64 {
 
  public static String encode(String data) {
    return new String(encode(data.getBytes()));
  }
 
  public static String decode(String data) {
    try {
      return new String(decode(data.toCharArray()),"utf-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
      return null;
    } 
  }
 
  private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
      .toCharArray();
 
  private static byte[] codes = new byte[256];
 
  static {
    for (int i = 0; i < 256; i++) {
      codes[i] = -1;
    }
    for (int i = 'A'; i <= 'Z'; i++) {
      codes[i] = (byte) (i - 'A');
    }
 
    for (int i = 'a'; i <= 'z'; i++) {
      codes[i] = (byte) (26 + i - 'a');
    }
    for (int i = '0'; i <= '9'; i++) {
      codes[i] = (byte) (52 + i - '0');
    }
    codes['+'] = 62;
    codes['/'] = 63;
  }
 
  public static char[] encode(byte[] data) {
    char[] out = new char[((data.length + 2) / 3) * 4];
    for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
      boolean quad = false;
      boolean trip = false;
 
      int val = (0xFF & (int) data[i]);
      val <<= 8;
      if ((i + 1) < data.length) {
        val |= (0xFF & (int) data[i + 1]);
        trip = true;
      }
      val <<= 8;
      if ((i + 2) < data.length) {
        val |= (0xFF & (int) data[i + 2]);
        quad = true;
      }
      out[index + 3] = alphabet[(quad &#63; (val & 0x3F) : 64)];
      val >>= 6;
      out[index + 2] = alphabet[(trip &#63; (val & 0x3F) : 64)];
      val >>= 6;
      out[index + 1] = alphabet[val & 0x3F];
      val >>= 6;
      out[index + 0] = alphabet[val & 0x3F];
    }
    return out;
  }
 
  public static byte[] decode(char[] data) {
    int tempLen = data.length;
    for (int ix = 0; ix < data.length; ix++) {
      if ((data[ix] > 255) || codes[data[ix]] < 0) {
        --tempLen;
      }
    }
    int len = (tempLen / 4) * 3;
    if ((tempLen % 4) == 3) {
      len += 2;
    }
    if ((tempLen % 4) == 2) {
      len += 1;
 
    }
    byte[] out = new byte[len];
 
    int shift = 0;
    int accum = 0;
    int index = 0;
 
    for (int ix = 0; ix < data.length; ix++) {
      int value = (data[ix] > 255) &#63; -1 : codes[data[ix]];
 
      if (value >= 0) {
        accum <<= 6;
        shift += 6;
        accum |= value;
        if (shift >= 8) {
          shift -= 8;
          out[index++] = (byte) ((accum >> shift) & 0xff);
        }
      }
    }
 
    if (index != out.length) {
      throw new Error("Miscalculated data length (wrote " + index
          + " instead of " + out.length + ")");
    }
 
    return out;
  }
}

DESUtil.java

package com.mstf.des;
 
import java.security.Key;
import java.security.SecureRandom;
 
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
 
/**
 * DES對稱算法(加密/解密)
 *
 * @author ceet
 *
 */
public class DESUtil {
 
  private Key key;
 
  public DESUtil(String strKey) {
    setKey(strKey);
  }
 
  public void setKey(String strKey) {
    try {
      KeyGenerator generator = KeyGenerator.getInstance("DES");
      generator.init(new SecureRandom(strKey.getBytes())); // 根據參數生成key
      this.key = generator.generateKey();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  public String encrypt(String source) {
    return encrypt(source, "utf-8");
  }
 
  public String decrypt(String encryptedData) {
    return decrypt(encryptedData, "utf-8");
  }
 
  public String encrypt(String source, String charSet) {
    String encrypt = null;
    try {
      byte[] ret = encrypt(source.getBytes(charSet));
      encrypt = new String(Base64.encode(ret));
    } catch (Exception e) {
      e.printStackTrace();
      encrypt = null;
    }
    return encrypt;
  }
 
  public String decrypt(String encryptedData, String charSet) {
    String descryptedData = null;
    try {
      byte[] ret = descrypt(Base64.decode(encryptedData.toCharArray()));
      descryptedData = new String(ret, charSet);
    } catch (Exception e) {
      e.printStackTrace();
      descryptedData = null;
    }
    return descryptedData;
  }
 
  private byte[] encrypt(byte[] primaryData) {
    try {
      Cipher cipher = Cipher.getInstance("DES"); // Cipher對象實際完成加密操作
      cipher.init(Cipher.ENCRYPT_MODE, this.key); // 用密鑰初始化Cipher對象(加密)
 
      return cipher.doFinal(primaryData);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
 
  private byte[] descrypt(byte[] encryptedData) {
    try {
      Cipher cipher = Cipher.getInstance("DES"); // Cipher對象實際完成解密操作
      cipher.init(Cipher.DECRYPT_MODE, this.key); // 用密鑰初始化Cipher對象(解密)
 
      return cipher.doFinal(encryptedData);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
 
  public static void main(String[] args) {
    String code = "ceet";
    DESUtil desUtil = new DESUtil("key");
    String encrypt = desUtil.encrypt(code);
    String decrypt = desUtil.decrypt(encrypt);
    System.out.println("原內容:" + code);
    System.out.println("加密:" + encrypt);
    System.out.println("解密:" + decrypt);
  }
}

看完上述內容,你們掌握如何在Java項目中實現一個DES加密算法的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

延边| 韶山市| 米脂县| 民乐县| 安乡县| 阿巴嘎旗| 中卫市| 竹山县| 三江| 乳源| 同德县| 柳河县| 洛宁县| 湟中县| 金坛市| 岳阳市| 项城市| 怀集县| 昌乐县| 布尔津县| 务川| 新津县| 犍为县| 子洲县| 德令哈市| 武隆县| 盐边县| 常熟市| 西城区| 尼玛县| 汉川市| 阿拉善右旗| 镶黄旗| 娱乐| 柳州市| 靖西县| 乌拉特前旗| 汪清县| 师宗县| 射洪县| 鸡东县|