Java簡單數(shù)據(jù)加密方法DES實現(xiàn)過程解析
這篇文章主要介紹了Java簡單數(shù)據(jù)加密方法DES實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
1.數(shù)據(jù)在網(wǎng)絡(luò)中傳輸時,需要進行加密處理
雙方約定一個相同的key(key不在網(wǎng)絡(luò)中進行傳輸,只傳輸加密數(shù)據(jù)),然后根據(jù)將key根據(jù)一定的DES規(guī)則轉(zhuǎn)換,得到真正的key,在進行加密和解密,為了增加安全性,加密過程中再加上編碼base64轉(zhuǎn)換,解密時先解碼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+jiJpnrXigolapdcJXfmh2tECyuQnaFrvZHabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmn"; /** * 測試 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Key convertSecretKey = generateSecret(KEY); String data = "{\"code\":\"100\",\"roleId\":[],\"userDesc\":\"測試\",\"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);//實例化DESKey秘鑰的相關(guān)內(nèi)容 SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");//實例一個秘鑰工廠,指定加密方式 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這個類進行加解密相關(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這個類進行加解密相關(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); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java class文件格式總結(jié)_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Java class文件格式總結(jié)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-06-06java正則表達式的應(yīng)用 java讀取文件并獲取電話號碼
這篇文章主要介紹了java正則表達式的應(yīng)用,應(yīng)用的內(nèi)容是java讀取文件并獲取電話號碼,感興趣的小伙伴們可以參考一下2015-11-11maven的settings.xml、pom.xml配置文件使用詳解
本文詳解了Maven中的配置文件settings.xml和pom.xml,闡述了它們的作用、配置項以及優(yōu)先級順序,settings.xml存在于Maven安裝目錄和用戶目錄下,分別作用于全局和當(dāng)前用戶,pom.xml位于項目根路徑下2024-09-09RabbitMQ中Confirm消息確認機制保障生產(chǎn)端消息的可靠性詳解
這篇文章主要介紹了RabbitMQ中Confirm消息確認機制保障生產(chǎn)端消息的可靠性詳解,生產(chǎn)者將數(shù)據(jù)發(fā)送到 RabbitMQ 的時候,可能數(shù)據(jù)就在半路給搞丟了,因為網(wǎng)絡(luò)問題啥的,都有可能,需要的朋友可以參考下2023-12-12