des encryption and decryption tools

  Des recently doing a double symmetric encryption and decryption, hereby record it.

  des symmetric encryption, encryption is a more traditional manner, its encryption operation, a decryption operation using the same key, the sender and the recipient information when information transmission and processing of information is performed, which must be held together password, an encryption algorithm is symmetric.
  First two methods: encryption and decryption.
  
. 1  public   String encryptDes (Data String, String Key) throws Exception {
 2  
. 3          // Create DESKeySpec object using the original key data 
. 4          DESedeKeySpec DKS = new new DESedeKeySpec (key.getBytes ());
 . 5          // Create a key factory 
. 6          the SecretKeyFactory = SecretKeyFactory.getInstance KeyFactory ( "the DES" );
 . 7          // with the key into a plant to convert DESKeySpec SecretKey objects 
. 8          key K = keyFactory.generateSecret (DKS);
 . 9  
10          // initialize Cipher engine class parameters were: algorithm / work mode / fill pattern 
. 11          the cipher cipher = Cipher.getInstance ( "the DES / the CBC / PKCS5Padding");
 12          // initialization vector 
13 is          IvParameterSpec IPS = new new IvParameterSpec ( "12345678" .getBytes ());
 14          // initialize the encryption cipher 
15          cipher.init (Cipher.ENCRYPT_MODE, K, IPS);
 16          // perform encryption and outputs and after base64 transcoded encrypted byte array 
. 17          String STR = Base64.encodeBase64String (Cipher.doFinal (data.getBytes ( "UTF-. 8" )));
 18 is          return STR;
 . 19      }
 1 public static String decryptDes(String data, String key) throws Exception{
 2 
 3             DESedeKeySpec dks = new DESedeKeySpec(key.getBytes());
 4             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
 5             Key k = keyFactory.generateSecret(dks);
 6             Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
 7             IvParameterSpec ips = new IvParameterSpec("12345678".getBytes());
 8             cipher.init(Cipher.DECRYPT_MODE, k, ips);
 9             byte[] out = cipher.doFinal(Base64.decodeBase64(data));;
10             return new String(out, "UTF-8");
11     }

 

  ok, broadcast Kan毕.

  Some knowledge:

  Data filled type:

    NoPadding: API or algorithm itself does not process the data.
    PKCS5Padding:

            Before encryption: data length of 8 bytes modulo a remainder of m, if m> 0, the byte complement 8-m, 8-byte value of m, i.e. the difference between a few bytes to several bytes complement ,

                Byte value is the number of bytes added, if it is added 8 0 8 bytes.


            Decrypted: Take the last byte, the value of m, m remove the tail from the data bytes, that is, the original remaining data before encryption.

                Example, the difference is three bytes, then fill 11111333 four bytes to fill the difference is no difference to fill 88888888 11114444

  Vector:
    In the CBC (DES algorithm is only) mode, iv generation is a relatively common method of using a random number (or pseudo random) mechanism.
    iv action is mainly used to produce a first ciphertext block, so that the final ciphertext generated a difference (same plaintext case),
    so that password attacks more difficult, in addition no other iv use. The greatest advantage is that even if the same plaintext, the same key can generate different cipher text.

Finally, I wish every success, good health, away from the epidemic, create brilliant.
 

Guess you like

Origin www.cnblogs.com/liutian1912/p/12507982.html