怎么在Java中使用DES實(shí)現(xiàn)數(shù)據(jù)加密

怎么在Java中使用DES實(shí)現(xiàn)數(shù)據(jù)加密?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)公司基于成都重慶香港及美國(guó)等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動(dòng)大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務(wù)器托管報(bào)價(jià),主機(jī)托管價(jià)格性價(jià)比高,為金融證券行業(yè)成都服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。

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)行加密和解密,為了增加安全性,加密過(guò)程中再加上編碼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");//通過(guò)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");//通過(guò)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);
  }
}

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

分享文章:怎么在Java中使用DES實(shí)現(xiàn)數(shù)據(jù)加密
文章路徑:http://bm7419.com/article44/jdgche.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站服務(wù)器托管關(guān)鍵詞優(yōu)化、定制網(wǎng)站、網(wǎng)站收錄、

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)