Java implements DES encryption and decryption algorithm parsing

  • 2020-05-10 18:17:10
  • OfStack

The example of this paper describes the implementation of Java encryption and decryption algorithm DES. I will share it with you for your reference as follows:
 

Brief introduction:

The data encryption algorithm (Data Encryption Algorithm, DEA) is a symmetric encryption algorithm and is probably the most widely used key system, especially in the security of protecting financial data. DEA was originally developed in embedded hardware. Typically, atms (Automated Teller Machine, ATM) use DEA. It came out of the research work of IBM, which also had a patent on it for a few years, but which had expired in 1983 and was in the public domain, allowing it to be used under certain conditions without royalties. It was officially adopted by the U.S. government in 1977.

After 1998, the emergence of practical DES decoder completely declared that DES algorithm was no longer secure. In 1999, NIST issued a new standard, which stipulated that DES algorithm could only be used for legacy encryption systems, but the use of DESede algorithm was not restricted. Today's DES algorithm is just launched the stage of history, AES algorithm as his replacement.

Encryption principle:

DES USES a 56-bit key with an additional 8-bit parity bit to produce a maximum 64-bit packet size. This is an iterative block password, using a technique called Feistel, which splits the encrypted block of text in half. The loop function is applied to one half using the subkey, and then the output is "xor" calculated with the other half. And then you swap these two halves, and you keep going, but you don't swap the last one. DES USES 16 cycles, using xor, substitution, substitution, and shift operations for four basic operations.

JDK support for DES algorithm

Key length: 56 bits

Work mode: ECB/CBC/PCBC/CTR/CTS/CFB/CFB8 to CFB128 OFB/OBF8 to OFB128

Filling way: Nopadding/PKCS5Padding/ISO10126Padding /

Java encryption decryption symmetric encryption algorithm DESede

DESede is the 3-weight DES encryption algorithm, also known as 3DES or Triple DES. Use three (or two) different keys to encrypt a block of data three (or two) times (one times faster than three times with normal encryption). The strength of the 3-weight DES is about the same as that of the 112-bit key. The number of iterations improves the security, but it also causes the low efficiency of encryption. Because of the efficiency of DESede algorithm, AES algorithm was born.

So far, no one has come up with an effective way to attack triple DES. Doing a brute force search of a key in its key space is actually not feasible because the space is too large. With the differential attack method, the complexity increases exponentially compared to 1DES alone.

The 3-weight DES comes in four models

DES-EEE3 USES three different keys to perform three encryption transformations in sequence. DES-EDE3, using three different keys, encipher-decryption-encryption transformation in turn. DES-EEE2, where the key K1=K3, is encrypted three times in sequence. DES-EDE2, in which the key K1=K3, is successively encrypted, decrypted and encrypted.

Java encryption code for the DES algorithm


package com.favccxx.codelib;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class EncryptCoder {
           
  private final static String DES = "DES";
           
  public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
    // DES The algorithm requires 1 A trusted random number source 
    SecureRandom sr = new SecureRandom();
    //  Create from the original key data DESKeySpec object 
    DESKeySpec dks = new DESKeySpec(key);
    //  create 1 A key factory, and then use it to DESKeySpec Converted to 1 a SecretKey object 
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher The object actually does the encryption 
    Cipher cipher = Cipher.getInstance(DES);
    //  Initialize with a key Cipher object 
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
    //  Formally perform the encryption operation 
    return cipher.doFinal(src);
  }
           
  /**
   *
   * @param password  password 
   * @param key  Encrypted string 
   * @return
   */
  public final static String encrypt(String password, String key) {
    try {
      return byte2String(encrypt(password.getBytes(), key.getBytes()));
    } catch (Exception e) {
    }
    return null;
  }
           
  public static String byte2String(byte[] b) {
    String hs = "";
    String stmp = "";
    for (int n = 0; n < b.length; n++) {
      stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
      if (stmp.length() == 1)
        hs = hs + "0" + stmp;
      else
        hs = hs + stmp;
    }
    return hs.toUpperCase();
  }           
  public static void main(String[] args){
    String encryptString = encrypt("is zhang 3 feng ","test Noise in both English and Chinese 7 bad 8 Mix and match @123654{");
    System.out.println(encryptString);
  }          
  // Output: B00542E93695F4CFCE34FC4393C2F4BF          
}

Implementation of DES decryption algorithm by Java


package com.favccxx.codelib;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DescryptCoder {
     
  private final static String DES = "DES";
     
  /**
   *
   * @param src  The data source 
   * @param key  The key, the length must be 8 A multiple of 
   * @return
   * @throws Exception
   */
  public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
    // DES The algorithm requires 1 A trusted random number source 
    SecureRandom sr = new SecureRandom();
    //  Create from the original key data 1 a DESKeySpec object 
    DESKeySpec dks = new DESKeySpec(key);
    //  create 1 A key factory, and then use it to DESKeySpec Object conversion to 1 a SecretKey object 
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher The object actually completes the decryption operation 
    Cipher cipher = Cipher.getInstance(DES);
    //  Initialize with a key Cipher object 
    cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
       
    //  Officially execute the decryption operation 
    return cipher.doFinal(src);
  }
     
  public final static String decrypt(String data, String key) {
    try {
      return new String(decrypt(String2byte(data.getBytes()), key.getBytes()));
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
     
  public static byte[] String2byte(byte[] b) {
    if ((b.length % 2) != 0)
      throw new IllegalArgumentException(" The length is not even ");
    byte[] b2 = new byte[b.length / 2];
    for (int n = 0; n < b.length; n += 2) {
      String item = new String(b, n, 2);
      b2[n / 2] = (byte) Integer.parseInt(item, 16);
    }
    return b2;
  }
     
  public static void main(String[] args){
    String desencryptString = decrypt("B00542E93695F4CFCE34FC4393C2F4BF","test Noise in both English and Chinese 7 bad 8 Mix and match @123654");
    System.out.println(desencryptString);
  }
     
  // Output: is zhang 3 feng 
}

Hope this article is helpful to you, Java implementation of DES encryption and decryption algorithm parsing content is introduced here. We hope you continue to pay attention to our website! Stay tuned to learn about java.


Related articles: