DES encryption, des encryption algorithm
Encryption is classified into symmetric encryption and asymmetric encryption. Asymmetric encryption uses different keys for encryption and decryption, such as RSA. symmetric encryption uses the same key for encryption and decryption, such as DES. Although asymmetric encryption is more secure, the computing process is complex and time-consuming. It is generally used only for key information (such as account and password). For non-key information, symmetric encryption is generally used, such as DES.
The full name of DES is Data Encryption Standard, which is the Data Encryption Standard. No matter what the plaintext is, the encrypted ciphertext is a string consisting of 16 characters (0-f.
The encryption code is as follows:
private static byte[] encrypt(byte[] data, byte[] key) throws Exception { SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); }
The main code for decryption is as follows:
private static byte[] decrypt(byte[] data, byte[] key) throws Exception { SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data); }