欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

Java

當(dāng)前位置:主頁 > 軟件編程 > Java >

Java簡單數(shù)據(jù)加密方法DES實現(xiàn)過程解析

來源:本站原創(chuàng)|時間:2020-01-10|欄目:Java|點擊: 次

這篇文章主要介紹了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í)有所幫助,也希望大家多多支持我們。

上一篇:java控制臺打印本月的日歷

欄    目:Java

下一篇:MyBatis執(zhí)行Sql的流程實例解析

本文標題:Java簡單數(shù)據(jù)加密方法DES實現(xiàn)過程解析

本文地址:http://mengdiqiu.com.cn/a1/Java/8890.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有