DES encryption algorithm
DES is the full name Data Encryption Standard, which is a block algorithm that uses key encryption. It was determined as the Federal Data Processing Standard (FIPS) by the National Standards Office of the U.S. Federal Government in 1976, and was later widely circulated internationally.
There are three entry parameters of the DES algorithm: Key, Data, and Mode. Among them, Key is 7 bytes, 56 bits in total, which is the working key of the DES algorithm; Data is 8 bytes, 64 bits in total, which is the data to be encrypted or decrypted; Mode is the working mode of DES: encryption or decryption.
The DES algorithm turns the 64-bit plaintext input block into a 64-bit ciphertext output block. The key it uses is also 56-bit. Its algorithm is mainly divided into two steps:
1) The initial permutation function is to recombinate the input 64-bit data blocks in bits and divide the output into two parts: L0 and R0, each part is 32 bits long. The permutation rule is to switch the 58th bit of the input to the first bit, the 50th bit to the second bit... and so on, the last bit is the original 7th bit. L0 and R0 are the two parts after the transposition output, L0 is the left 32 bits of the output, and R0 is the right 32 bits. Example: Set the input value before the transposition to D1D2D3...D64, then 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) After 16 iteration operations, L16 and R16 are obtained. Take this as input and perform inverse permutation. The inverse permutation is exactly the inverse operation of the initial permutation, thereby obtaining the ciphertext output.
This algorithm is a representative of the symmetric encryption algorithm system and is widely used in computer network systems.
Basic Java 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 developed by IBM in the United States, symmetric encryption algorithm*/ public class DES { // Algorithm name public static final String KEY_ALGORITHM = "DES"; // Algorithm name/encryption mode/fill method 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("Gasculitis F#*(x)".getBytes(), key); System.out.println(new String(decrypt(encrypt, key))); /* * Use CBC mode * Use the key factory to generate the key, 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("Gasculitis A%F#*(x)".getBytes()); //Encrypt 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 be 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; } /** * Encrypt* @param data Original text* @param key * @return ciphertext* @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); } /** * Decrypt* @param data Password* @param key * @return Plain text, original text* @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 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 is also used as Triple DES, */ public class TripleDES { // Algorithm name public static final String KEY_ALGORITHM = "DESede"; // Algorithm name/encryption mode/fill method 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("After encryption: " + new String(tripleDES.decrypt(encryptData))); tripleDES = new TripleDES("CBC"); tripleDES.encrypt2("sau8jzxlc DQV#><«|vm,'123`98(*^&%^^JCB ZX>>A<S<}}{"); System.out.println("After encryption: " + new String(encryptData)); System.out.println("After decrypt: "+ 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); } } /** * Encrypt* @param str * @return * @throws Exception */ public byte[] encrypt(String str) throws Exception { cipher.init(Cipher.ENCRYPT_MODE, secretKey); return encryptData = cipher.doFinal(str.getBytes()); } /** * Decrypt* @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(); } /** * Encrypt* @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()); } /** * Decrypt* @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); } }