Detailed DES encryption algorithm and the use of the Java program Example _java

Source: Internet
Author: User
Tags decrypt

DES encryption algorithm
des, known as the data encryption Standard, is an encryption standard, a block algorithm using key cryptography, which was identified by the U.S. federal government's national standards office as a federal data Processing standard (FIPS) in 1976, and subsequently widely circulated internationally.
The DES algorithm has three entry parameters: Key, Data, Mode. The key is 7 bytes A total of 56 bits, is the DES algorithm's work key, data is 8 bytes 64 bits, is to be encrypted or decrypted; Mode is the way des works, there are two kinds: encryption or decryption.
Des algorithm to the 64-bit plaintext input block into 64-bit ciphertext output block, it uses the key is also 56 bits, the algorithm is mainly divided into two steps:
1) Initial permutation
Its function is to put the input of the 64-bit data block by bit, and the output is divided into L0, R0 two parts, each length of 32 bits, the replacement rules for the input of the 58th place to the first, 50th to the 2nd bit ... And so on, the last one is the original 7th place. L0, R0 is the output of two parts, L0 is the output of the left 32-bit, R0 is the right 32-bit, example: set to change before the input value of d1d2d3 ... D64, the result after the initial permutation is: L0=d58d50 ... D8; R0=d57d49 ... D7.
The replacement rules are shown in the following table:

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) Inverse permutation
After 16 iterations, we get L16 and R16, take this as input, carry on the inverse permutation, the inverse permutation is just the inverse operation of the initial permutation, then the output of ciphertext is obtained.
This algorithm is a representative of symmetric encryption algorithm, which is widely used in computer network system.

Java Basic Implementation

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 Algorithm 1972 United States IBM developed, symmetric encryption algorithm */public class DES {//algorithm name public static final String key_algorithm = "D 
  ES "; 
  Algorithm name/encryption mode/Fill mode 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 {* * * use ECB mode * key generator to generate key * ECB mode  
    Cannot use IV * * byte[] key = GenerateKey (); 
    Byte[] Encrypt = Encrypt ("Gastritis f#* (x)". GetBytes (), key); 
     
     
    System.out.println (New String (Decrypt (encrypt, key)); * * Use CBC mode * to generate keys using the key factory, encrypt and decrypt * 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 ("Gastritis a%f#* (x)". GetBytes ()); 
    Encryption Cipher.init (Cipher.decrypt_mode, Secretkey, New Ivparameterspec (Getiv ())); byte[] Dec = cipher.dofinal (ENC); 
  Decrypt System.out.println (new String (DEC));  
  Static byte[] Getiv () {String IV = "ASDFIVH7";//iv length:must is 8 bytes long return iv.getbytes ();  /** * Generate Key * * @return * @throws Exception/private static byte[] GenerateKey () throws 
    Exception {Keygenerator keygenerator = keygenerator.getinstance (key_algorithm); Keygenerator.init (56); DEs must be 56, this initial method does not have to call Secretkey Secretkey = Keygenerator.generatekey (); 
  return secretkey.getencoded (); /** * Restore Key * * @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; /** * Encryption * @param data original * @param key * @return ciphertext * @throws Exception/Public stat 
    IC 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); /** * decryption * @param data redaction * @param key * @return PlainText, original * @throws Exception * * * public Stati C 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 Triple DES implementation:

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; /** * Triple encryption 3DES also as Triple DES, */public class TripleDES {//algorithm name public static final String key_algorithm = "D 
  Esede "; 
  Algorithm name/encryption mode/Fill mode 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 ') (*^&% ^^ JCB zx>>a<s<}}{"); 
    SYSTEM.OUT.PRINTLN ("Encrypted:" + new String (encryptdata)); System.oUt.println ("After decryption:" + New String (Tripledes.decrypt (encryptdata))); 
    TripleDES = new TripleDES ("CBC"); 
    Tripledes.encrypt2 ("sau8jzxlc dqv#><«|vm, ' 123 ' (*^&% ^^ JCB zx>>a<s<}}{"); 
    SYSTEM.OUT.PRINTLN ("Encrypted:" + new String (encryptdata)); 
  System.out.println ("After decryption:" + New String (Tripledes.decrypt2 (encryptdata))); Public TripleDES (String mode) throws Exception {if ("ECB". Equals (Mode)) {//cipher = Cipher.getinstan 
      CE (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); }/** * Encryption * @param str * @return * @throws Exception/public byte[] Encrypt (String str) throws Exception {Ciph 
    Er.init (Cipher.encrypt_mode, Secretkey); 
  return encryptdata = Cipher.dofinal (Str.getbytes ()); /** * Decryption * @param encrypt * @return * @throws Exception/public byte[] Decrypt (byte[) Encryp 
    T) throws Exception {Cipher.init (Cipher.decrypt_mode, Secretkey); 
  return encryptdata = cipher.dofinal (encrypt); 
  } byte[] Getiv () {return "Administ". GetBytes (); /** * Encryption * @param str * @return * @throws Exception/public byte[] Encrypt2 (String str) thro 
    WS Exception {cipher.init (Cipher.encrypt_mode, SecretKey2, New Ivparameterspec (Getiv ())); 
  return encryptdata = Cipher.dofinal (Str.getbytes ()); /** * Decryption * @param encrypt * @return * @throws Exception/public byte[] Decrypt2 (byte[) encry PT) throws Exception {Cipher.init (Cipher.decrypt_mode, SecretKey2, New Ivparameterspec (Getiv ())); 
  return encryptdata = cipher.dofinal (encrypt);  } 
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.