How to analyze the simple data encryption method of Java and the implementation process of DES
How to analyze the simple data encryption method of Java and the implementation process of DES, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
1. When data is transmitted in the network, it needs to be encrypted.
The two sides agree on a same key (key does not transmit in the network, only encrypted data is transmitted), and then according to the conversion of key according to certain DES rules, the real key is obtained. Encryption and decryption are carried out. In order to increase security, encoding base64 conversion is added in the encryption process, and base64 is decoded first when decrypting.
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;/** * data encryption DES method + Base64 * @ author sun_flower * * / public class EncryUtils {public static final String KEY = "gEpCIKFVdPEBJ1pM5pLSviM2Nrj5C/A4iAw8ou+jiJpnrXigolapdcJXfmh3tECyuQnaFrvZHabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmn" / * Test * @ param args * @ throws Exception * / public static void main (String [] args) throws Exception {Key convertSecretKey = generateSecret (KEY) String data = "{\" code\ ":\" 100\ ",\" roleId\ ": [],\" userDesc\ ":\" Test\ ",\" sessionId\ ":\" 90EA80C89F6187BAB363C9347F759E39\ ",\" roleList\ ": [],\" userName\ ":\" chenpeng\ ",\" checkCode\ ":\" token\ ":\"\ ",\" password\ ":\" eFEBcXRwTW2oMFSDwGwUKQ==\ ",\" createTime\ ":\" 2019-05-27 15:30:14\ " \ "levelId\":\ "1\",\ "staffName\":\ ",\" id\ ": 1502,\" userType\ ":\" 1\ ",\" oldPwd\ ":\"\ "}" String enStr = encodeString (convertSecretKey, data); decodeString (convertSecretKey, enStr);} / * * transform key * @ param key * @ return * @ throws GeneralSecurityException * / public static Key generateSecret (String key) throws GeneralSecurityException {byte [] bytesKey = key.getBytes (); DESKeySpec desKeySpec = new DESKeySpec (bytesKey); / / instantiate the DESKey key SecretKeyFactory factory = SecretKeyFactory.getInstance ("DES"); / / instance a key factory, specify 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"); / / encryption-related operations cipher.init (Cipher.ENCRYPT_MODE, convertSecretKey) through Cipher; byte [] enData = Base64.getEncoder (). Encode (data.getBytes ()); byte [] result = cipher.doFinal (enData) / / enter the content to be encrypted System.out.println ("encryption result:" + Hex.encodeHexString (result)); return Hex.encodeHexString (result);} / * * decrypt * @ param convertSecretKey * @ param date * @ return * @ throws DecoderException * / public static String decodeString (Key convertSecretKey, String data) throws GeneralSecurityException, DecoderException {Cipher cipher = Cipher.getInstance ("DES/ECB/PKCS5Padding") / / perform encryption and decryption related operations through the class Cipher 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);}}"
This is the answer to the question about how to parse the simple data encryption method of Java and the implementation process of DES. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.