Java簡單數(shù)據(jù)加密方法DES實現(xiàn)過程解析
這篇文章主要介紹了Java簡單數(shù)據(jù)加密方法DES實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
1.數(shù)據(jù)在網(wǎng)絡中傳輸時,需要進行加密處理
雙方約定一個相同的key(key不在網(wǎng)絡中進行傳輸,只傳輸加密數(shù)據(jù)),然后根據(jù)將key根據(jù)一定的DES規(guī)則轉換,得到真正的key,在進行加密和解密,為了增加安全性,加密過程中再加上編碼base64轉換,解密時先解碼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); } /** * 轉換key * @param key * @return * @throws GeneralSecurityException */ public static Key generateSecret(String key) throws GeneralSecurityException { byte[] bytesKey = key.getBytes(); DESKeySpec desKeySpec = new DESKeySpec(bytesKey);//實例化DESKey秘鑰的相關內容 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這個類進行加解密相關操作 cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey); byte[] enData = Base64.getEncoder().encode(data.getBytes()); byte[] result = cipher.doFinal(enData);//輸入要加密的內容 System.out.println("加密的結果:" + 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這個類進行加解密相關操作 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("解密結果:" + new String(decode)); return new String(decode); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java class文件格式總結_動力節(jié)點Java學院整理
這篇文章主要介紹了Java class文件格式總結的相關資料,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-06-06maven的settings.xml、pom.xml配置文件使用詳解
本文詳解了Maven中的配置文件settings.xml和pom.xml,闡述了它們的作用、配置項以及優(yōu)先級順序,settings.xml存在于Maven安裝目錄和用戶目錄下,分別作用于全局和當前用戶,pom.xml位于項目根路徑下2024-09-09RabbitMQ中Confirm消息確認機制保障生產端消息的可靠性詳解
這篇文章主要介紹了RabbitMQ中Confirm消息確認機制保障生產端消息的可靠性詳解,生產者將數(shù)據(jù)發(fā)送到 RabbitMQ 的時候,可能數(shù)據(jù)就在半路給搞丟了,因為網(wǎng)絡問題啥的,都有可能,需要的朋友可以參考下2023-12-12