如何解析Java簡(jiǎn)單數(shù)據(jù)加密方法及DES實(shí)現(xiàn)過程,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)公司2013年至今,先為皇姑等服務(wù)建站,皇姑等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為皇姑企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1.數(shù)據(jù)在網(wǎng)絡(luò)中傳輸時(shí),需要進(jìn)行加密處理
雙方約定一個(gè)相同的key(key不在網(wǎng)絡(luò)中進(jìn)行傳輸,只傳輸加密數(shù)據(jù)),然后根據(jù)將key根據(jù)一定的DES規(guī)則轉(zhuǎn)換,得到真正的key,在進(jìn)行加密和解密,為了增加安全性,加密過程中再加上編碼base64轉(zhuǎn)換,解密時(shí)先解碼base64
加密和解密的完整的代碼:
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;/** * 數(shù)據(jù)加密 DES方式 + Base64 * @author sun_flower * */public class EncryUtils { public static final String KEY = "gEpCIKFVdPEBJ1pM5pLSviM2Nrj5C/A4iAw8ou+jiJpnrXigolapdcJXfmh3tECyuQnaFrvZHabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmn"; /** * 測(cè)試 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Key convertSecretKey = generateSecret(KEY); String data = "{\"code\":\"100\",\"roleId\":[],\"userDesc\":\"測(cè)試\",\"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); } /** * 轉(zhuǎn)換key * @param key * @return * @throws GeneralSecurityException */ public static Key generateSecret(String key) throws GeneralSecurityException { byte[] bytesKey = key.getBytes(); DESKeySpec desKeySpec = new DESKeySpec(bytesKey);//實(shí)例化DESKey秘鑰的相關(guān)內(nèi)容 SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");//實(shí)例一個(gè)秘鑰工廠,指定加密方式 Key convertSecretKey = factory.generateSecret(desKeySpec); return convertSecretKey; } /** * 加密 * @param convertSecretKey * @param date * @return * @throws GeneralSecurityException */ public static String encodeString(Key convertSecretKey, String data) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");//通過Cipher這個(gè)類進(jìn)行加解密相關(guān)操作 cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey); byte[] enData = Base64.getEncoder().encode(data.getBytes()); byte[] result = cipher.doFinal(enData);//輸入要加密的內(nèi)容 System.out.println("加密的結(jié)果:" + Hex.encodeHexString(result)); return Hex.encodeHexString(result); } /** * 解密 * @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");//通過Cipher這個(gè)類進(jìn)行加解密相關(guān)操作 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("解密結(jié)果:" + new String(decode)); return new String(decode); }}
關(guān)于如何解析Java簡(jiǎn)單數(shù)據(jù)加密方法及DES實(shí)現(xiàn)過程問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。