This article describes the analysis of Java implementation of DES encryption and decryption algorithm. Share it for your reference, as follows:
Introduction:
Data Encryption Algorithm (DEA) is a symmetric encryption algorithm that is likely to be the most widely used key system, especially in protecting the security of financial data. The initially developed DEA was embedded in hardware. Usually, automatic Teller Machines (ATMs) use DEA. It comes from IBM's research work, which also had patent rights for several years, but after it expired in 1983, it is in public scope and allows it to be used under certain conditions without the patent usage fee. It was officially adopted by the US government in 1977.
The emergence of practical DES deciphers after 1998 completely declared that the DES algorithm is no longer secure. In 1999, NIST issued a new standard, stipulating that DES algorithm can only be used in legacy encryption systems, but does not restrict the use of the DESede algorithm. Today's DES algorithm is launching the stage of history, and the AES algorithm is called its replacement.
Encryption principle:
DES uses a 56-bit key with an additional 8-bit parity bit to generate a maximum 64-bit packet size. This is an iterative block password using a technique called Feistel, where encrypted blocks of text are divided into two. Use a subkey to apply loop function to one half, and then perform an "exclusive OR" operation with the other half; then swap these two halves, and the process will continue, but the last loop will not be swapped. DES uses 16 cycles, four basic operations: exclusive or, permutation, substitution, and shift operations.
JDK's support for DES algorithm
Key length: 56 bits
Working mode: ECB/CBC/PCBC/CTR/CTS/CFB/CFB8 to CFB128/OFB/OBF8 to OFB128
Filling method: Nopadding/PKCS5Padding/ISO10126Padding/
Java encryption and decryption symmetric encryption algorithm DESede
DESede is the triple DES encryption algorithm, also known as 3DES or Triple DES. Use three (or two) different keys to encrypt the data block three times (or two) of DES (encrypting once is faster than doing ordinary encryption three times). The strength of the triple DES is approximately the same as the key strength of the 112-bit. The security is improved through the number of iterations, but it also causes the problem of low encryption efficiency. Because of the efficiency of the DESede algorithm, the AES algorithm was born.
So far, no one has given an effective way to attack triple DES. If you search for the keys in its key space, then because the space is too large, this is actually not feasible. If the differential attack method is used, the complexity increases exponentially compared to a single DES.
There are four models of triple DES
Java encryption code for 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 algorithm requires a trusted random number source SecureRandom sr = new SecureRandom(); // Create a DESKeySpec object from the original key data DESKeySpec dks = new DESKeySpec(key); // Create a key factory and use it to convert DESKeySpec into a SecretKey object SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // The Cipher object actually completes the encryption operation Cipher cipher = Cipher.getInstance(DES); // Initialize the Cipher object cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); // Formal execution of encryption operation return cipher.doFinal(src); } /** * * @param password Password* @param key Encryption 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 Sanfeng","test Chinese and English mixed-up @123654{"); System.out.println(encryptString); } //Output: B00542E93695F4CFCE34FC4393C2F4BF } Java implementation of DES decryption 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 DescryptCoder { private final static String DES = "DES"; /** * * @param src Data source* @param key key, length must be multiples of 8* @return * @throws Exception */ public static byte[] decrypt(byte[] src, byte[] key) throws Exception { // The DES algorithm requires a trusted random number source SecureRandom sr = new SecureRandom(); // Create a DESKeySpec object from the original key data DESKeySpec dks = new DESKeySpec(key); // Create a key factory, and then use it to convert the DESKeySpec object into a SecretKey object SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // The Cipher object actually completes the decryption operation Cipher cipher = Cipher.getInstance(DES); // Initialize the Cipher object cipher.init(Cipher.DECRYPT_MODE, securekey, sr); // Formal execution of 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("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 Chinese and English mixed-up @123654"); System.out.println(desencryptString); } //Output: is Zhang Sanfeng}I hope this article will be helpful to you. This is all for you to introduce the analysis content of Java implementation of DES encryption and decryption algorithm. I hope everyone will continue to follow our website! If you want to learn Java, you can continue to follow this website.