Analysis of Des implementation process of java simple data encryption method

Time:2021-4-18

This article mainly introduces the java simple data encryption method des implementation process analysis, this article through the example code introduction is very detailed, for everyone’s study or work has a certain reference learning value, need friends can refer to

1. When the data is transmitted in the network, it needs to be encrypted

The two sides agree on the same key (the key is not transmitted in the network, only encrypted data is transmitted), and then the key is converted according to certain des rules to get the real key. In the process of encryption and decryption, in order to increase security, the encoding Base64 is added in the encryption process, and the base64 is decoded before decryption

Complete code for encryption and decryption:

package com.cmit.hall.plat.play.utils;

import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

/** 
 *Des mode of data encryption + Base64
 * @author sun_flower
 *
 */
public class EncryUtils {
  public static final String KEY = "gEpCIKFVdPEBJ1pM5pLSviM2Nrj5C/A4iAw8ou+jiJpnrXigolapdcJXfmh2tECyuQnaFrvZHabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmn";
  /**
   *Testing
   * @param args
   * @throws Exception
   */
  public static void main(String[] args) throws Exception {
    Key convertSecretKey = generateSecret(KEY);
    As well as the test, the session ID is associated with the following words: "90ea80c889f6187bab63b6187b63b63b63b63b63b3187b63b3187b31b63b31b63b63b63b63b37b73b37b37b73b31b37b63b9347f7759e39," "rolid", \ \ "", \ "",, \ "" "" "", \ "", \ "user user description as as as as as as the test, test, test, and the" session ID ",", "", \ "",, \ ",", \ "session ID", \ ", \", \ "", \ ", \" password password ", \" ", \", \ \ ", \", \ ", \ \" taking as the ebcxcxcxcwcwcwswswswswswswswswswswswswswswswswswswswswswswswswswswswswswswswsw"1", "staffname": "," Id ": 1502," UserType ":" 1 ",", \"oldPwd\":\"\"}";
    String enStr = encodeString(convertSecretKey, data);
    decodeString(convertSecretKey, enStr);
  }
  /**
   *Conversion key
   * @param key
   * @return
   * @throws GeneralSecurityException
   */
  public static Key generateSecret(String key) throws GeneralSecurityException {
    byte[] bytesKey = key.getBytes();
    Taskyspec deskeyspec = new taskyspec (byteskey); // instantiate the relevant contents of the Taskey secret key
    SecretKeyFactory factory =  SecretKeyFactory.getInstance ("des"); // an instance of a secret key factory, specifying the encryption method
    Key convertSecretKey = factory.generateSecret(desKeySpec);
    return convertSecretKey;
  }
  /**
   *Encryption
   * @param convertSecretKey
   * @param date
   * @return
   * @throws GeneralSecurityException
   */
  public static String encodeString(Key convertSecretKey, String data) throws GeneralSecurityException {
    Cipher cipher =  Cipher.getInstance ("DES / ECB / pkcs5padding"); // encrypt and decrypt through cipher class
    cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
    byte[] enData = Base64.getEncoder().encode(data.getBytes());
    byte[] result =  cipher.doFinal (endata); // enter the content to be encrypted
    System.out.println (encrypted results:+ Hex.encodeHexString (result));
    return Hex.encodeHexString(result);
    
  }
  
  /**
   *Decryption
   * @param convertSecretKey
   * @param date
   * @return
   * @throws GeneralSecurityException
   * @throws DecoderException 
   */
  public static String decodeString(Key convertSecretKey, String data) throws GeneralSecurityException, DecoderException {
    Cipher cipher =  Cipher.getInstance ("DES / ECB / pkcs5padding"); // encrypt and decrypt through cipher class
    cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
    byte[] hdata = Hex.decodeHex(data.toCharArray());
    byte[] result = cipher.doFinal(hdata);
    byte[] decode = Base64.getDecoder().decode(result);
    System.out.println (decryption result: + new string (decode));
    return new String(decode);
  }
}

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]