package com.dc; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.Security; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; /* ******************** Class description ******************** * class : DcDES * @author : ncc * create time : 2017-12-19 10:19:50 am * @version : 1.0 * description : The DES algorithm is a symmetric encryption system in the encryption system, and it has become the US data encryption standard. It was developed by IBM in 1972. * Symmetric cryptosystem encryption algorithm. The plaintext is grouped by 64 bits, the key is 64 bits long, and the key is actually 56 bits involved in the DES operation * (The 8th, 16th, 24th, 32nd, 40th, 48th, 56th, and 64th bits are the check digit, so that each key has an odd number of 1s) the grouped plaintext group * Encryption method that forms a ciphertext group by bitwise substitution or exchange with a 56-bit key * @see : * ************************************************/ public class DcDES { // KeyGenerator provides the function of a symmetric key generator and supports various algorithms private KeyGenerator keygen; // SecretKey is responsible for storing the symmetric key private SecretKey deskey; // Cipher is responsible for completing the encryption or decryption work private Cipher c; // This byte array is responsible for storing the encrypted result private byte[] cipherByte; /** * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException */ public DcDES() throws NoSuchAlgorithmException, NoSuchPaddingException { Security.addProvider(new com.sun.crypto.provider.SunJCE()); // Instantiate a key generator that supports the DES algorithm (the algorithm name must be named as specified, otherwise an exception will be thrown) keygen = KeyGenerator.getInstance("DES"); // generate key deskey = keygen.generateKey(); //Generate a Cipher object and specify the DES algorithm it supports c = Cipher.getInstance("DES"); } /* ******************************************** * method name : Encrytor * description : encrypt the string * @return : byte[] * @param : @param str * @param : @return * @param : @throws InvalidKeyException * @param : @throws IllegalBlockSizeException * @param : @throws BadPaddingException * modified : ncc , 2017-12-19 * @see : * ********************************************/ public byte[] Encrytor(String str) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException { // Initialize the Cipher object according to the key, ENCRYPT_MODE represents the encryption mode c.init(Cipher.ENCRYPT_MODE, deskey); byte[] src = str.getBytes(); // Encrypt, save the result into cipherByte cipherByte = c.doFinal(src); return cipherByte; } /* ******************************************** * method name : Decryptor * description : decrypt the string * @return : byte[] * @param : @param buff * @param : @return * @param : @throws InvalidKeyException * @param : @throws IllegalBlockSizeException * @param : @throws BadPaddingException * modified : ncc , 2017-12-19 * @see : * ********************************************/ public byte[] Decryptor(byte[] buff) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException { // Initialize the Cipher object according to the key, DECRYPT_MODE represents the encryption mode c.init(Cipher.DECRYPT_MODE, deskey); cipherByte = c.doFinal(buff); return cipherByte; } /** * @param args * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws BadPaddingException * @throws IllegalBlockSizeException * @throws InvalidKeyException */ public static void main(String[] args) throws Exception { DcDES de1 = new DcDES(); String msg = "Welcome to the House of Grass!"; byte[] encontent = de1.Encrytor(msg); byte[] decontent = de1.Decryptor(encontent); System.out.println("Plaintext is: " + msg); System.out.println("After encryption:" + new String(encontent)); System.out.println("After decryption:" + new String(decontent)); } }