程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 詳解DES加密算法及在Java法式中的應用示例

詳解DES加密算法及在Java法式中的應用示例

編輯:關於JAVA

詳解DES加密算法及在Java法式中的應用示例。本站提示廣大學習愛好者:(詳解DES加密算法及在Java法式中的應用示例)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解DES加密算法及在Java法式中的應用示例正文


DES加密算法
DES全稱為Data Encryption Standard,即數據加密尺度,是一種應用密鑰加密的塊算法,1976年被美國聯邦當局的國度尺度局肯定為聯邦材料處置尺度(FIPS),隨後在國際上普遍傳播開來。
DES算法的進口參數有三個:Key、Data、Mode。個中Key為7個字節共56位,是DES算法的任務密鑰;Data為8個字節64位,是要被加密或被解密的數據;Mode為DES的任務方法,有兩種:加密或解密。
DES算法把64位的明文輸出塊變成64位的密文輸入塊,它所應用的密鑰也是56位,其算法重要分為兩步:
1)初始置換
其功效是把輸出的64位數據塊按位從新組合,並把輸入分為L0、R0兩部門,每部門各長32位,其置換規矩為將輸出的第58位換到第一名,第50位換到第2位……依此類推,最初一名是本來的第7位。L0、R0則是換位輸入後的兩部門,L0是輸入的左32位,R0是右32位,例:設置換前的輸出值為D1D2D3……D64,則經由初始置換後的成果為:L0=D58D50……D8;R0=D57D49……D7。
其置換規矩見下表:

58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7,

2)逆置換
經由16次迭代運算後,獲得L16、R16,將此作為輸出,停止逆置換,逆置換正好是初始置換的逆運算,由此即獲得密文輸入。
此算法是對稱加密算法系統中的代表,在盤算機收集體系中普遍應用.

Java根本完成

package com.stone.security; 
 
import java.security.Key; 
import java.security.SecureRandom; 
 
import javax.crypto.Cipher; 
import javax.crypto.KeyGenerator; 
import javax.crypto.SecretKey; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.DESKeySpec; 
import javax.crypto.spec.IvParameterSpec; 
 
/** 
 * DES 算法    1972美國IBM研制,對稱加密算法 
 */ 
public class DES {  
  // 算法稱號 
  public static final String KEY_ALGORITHM = "DES"; 
  // 算法稱號/加密形式/填充方法 
  public static final String CIPHER_ALGORITHM_ECB = "DES/ECB/PKCS5Padding"; 
  public static final String CIPHER_ALGORITHM_CBC = "DES/CBC/PKCS5Padding"; 
   
  public static void main(String[] args) throws Exception { 
    /* 
     * 應用 ECB mode 
     * 密鑰生成器 生成密鑰 
     * ECB mode cannot use IV 
     */ 
    byte[] key = generateKey();  
    byte[] encrypt = encrypt("胃炎F#*(x)".getBytes(), key); 
    System.out.println(new String(decrypt(encrypt, key))); 
     
     
    /* 
     * 應用CBC mode 
     * 應用密鑰工場生成密鑰,加密 解密 
     * iv: DES in CBC mode and RSA ciphers with OAEP encoding operation. 
     */ 
    DESKeySpec dks = new DESKeySpec(generateKey()); 
    SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_ALGORITHM); 
    SecretKey secretKey = factory.generateSecret(dks); 
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC); 
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(getIV())); 
    byte[] enc = cipher.doFinal("胃炎A%F#*(x)".getBytes()); //加密 
     
    cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(getIV())); 
    byte[] dec = cipher.doFinal(enc); // 解密 
    System.out.println(new String(dec)); 
  } 
   
  static byte[] getIV() { 
    String iv = "asdfivh7"; //IV length: must be 8 bytes long 
    return iv.getBytes(); 
  } 
 
  /** 
   * 生成密鑰 
   * 
   * @return 
   * @throws Exception 
   */ 
  private static byte[] generateKey() throws Exception { 
    KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM); 
    keyGenerator.init(56); //des 必需是56, 此初始辦法不用須挪用 
    SecretKey secretKey = keyGenerator.generateKey(); 
    return secretKey.getEncoded(); 
  } 
 
  /** 
   * 復原密鑰 
   * 
   * @param key 
   * @return 
   * @throws Exception 
   */ 
  private static Key toKey(byte[] key) throws Exception { 
    DESKeySpec des = new DESKeySpec(key); 
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM); 
    SecretKey secretKey = keyFactory.generateSecret(des); 
    return secretKey; 
  } 
   
  /** 
   * 加密 
   * @param data 原文 
   * @param key 
   * @return 密文 
   * @throws Exception 
   */ 
  public static byte[] encrypt(byte[] data, byte[] key) throws Exception { 
    Key k = toKey(key); 
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB); 
    cipher.init(Cipher.ENCRYPT_MODE, k, new SecureRandom()); 
    return cipher.doFinal(data); 
  } 
  /** 
   * 解密 
   * @param data 密文 
   * @param key 
   * @return 明文、原文 
   * @throws Exception 
   */ 
  public static byte[] decrypt(byte[] data, byte[] key) throws Exception { 
    Key k = toKey(key); 
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB); 
    cipher.init(Cipher.DECRYPT_MODE, k, new SecureRandom()); 
    return cipher.doFinal(data); 
  } 
} 

Java三重DES完成:

package com.stone.security; 
 
import javax.crypto.Cipher; 
import javax.crypto.KeyGenerator; 
import javax.crypto.SecretKey; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.DESedeKeySpec; 
import javax.crypto.spec.IvParameterSpec; 
 
/** 
 * 三重加密 3DES也作 Triple DES, 
 */ 
public class TripleDES { 
  // 算法稱號 
  public static final String KEY_ALGORITHM = "DESede"; 
  // 算法稱號/加密形式/填充方法 
  public static final String CIPHER_ALGORITHM_ECB = "DESede/ECB/PKCS5Padding"; 
  public static final String CIPHER_ALGORITHM_CBC = "DESede/CBC/PKCS5Padding"; 
   
  private KeyGenerator keyGen; 
  private SecretKey secretKey; 
  private SecretKey secretKey2; 
  private Cipher cipher; 
  private static byte[] encryptData; 
   
  public static void main(String[] args) throws Exception { 
    TripleDES tripleDES = new TripleDES("ECB"); 
    tripleDES.encrypt("sau8jzxlcvm,'123`98(*^&%^^JCB ZX>>A<S<}}{"); 
    System.out.println("加密後:" + new String(encryptData)); 
    System.out.println("解密後:"+ new String(tripleDES.decrypt(encryptData))); 
     
    tripleDES = new TripleDES("CBC"); 
    tripleDES.encrypt2("sau8jzxlc DQV#><«|vm,'123`98(*^&%^^JCB ZX>>A<S<}}{"); 
    System.out.println("加密後:" + new String(encryptData)); 
    System.out.println("解密後:"+ new String(tripleDES.decrypt2(encryptData))); 
  } 
   
  public TripleDES(String mode) throws Exception { 
    if ("ECB".equals(mode)) { 
//     cipher = Cipher.getInstance(KEY_ALGORITHM); 
      cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB); 
      keyGen = KeyGenerator.getInstance(KEY_ALGORITHM); 
      secretKey = keyGen.generateKey(); 
    } else if("CBC".equals(mode)) { 
      cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC); 
      keyGen = KeyGenerator.getInstance(KEY_ALGORITHM); 
      DESedeKeySpec spec = new DESedeKeySpec(keyGen.generateKey().getEncoded()); 
      secretKey2 = SecretKeyFactory.getInstance(KEY_ALGORITHM).generateSecret(spec); 
    } 
  } 
  /** 
   * 加密 
   * @param str 
   * @return 
   * @throws Exception 
   */ 
  public byte[] encrypt(String str) throws Exception { 
    cipher.init(Cipher.ENCRYPT_MODE, secretKey); 
    return encryptData = cipher.doFinal(str.getBytes()); 
  } 
  /** 
   * 解密 
   * @param encrypt 
   * @return 
   * @throws Exception 
   */ 
  public byte[] decrypt(byte[] encrypt) throws Exception { 
    cipher.init(Cipher.DECRYPT_MODE, secretKey); 
    return encryptData = cipher.doFinal(encrypt); 
  } 
  byte[] getIV() { 
    return "administ".getBytes(); 
  } 
  /** 
   * 加密 
   * @param str 
   * @return 
   * @throws Exception 
   */ 
  public byte[] encrypt2(String str) throws Exception { 
    cipher.init(Cipher.ENCRYPT_MODE, secretKey2, new IvParameterSpec(getIV())); 
    return encryptData = cipher.doFinal(str.getBytes()); 
  } 
  /** 
   * 解密 
   * @param encrypt 
   * @return 
   * @throws Exception 
   */ 
  public byte[] decrypt2(byte[] encrypt) throws Exception { 
    cipher.init(Cipher.DECRYPT_MODE, secretKey2, new IvParameterSpec(getIV())); 
    return encryptData = cipher.doFinal(encrypt); 
  } 
} 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved