温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Java中使用DES实现数据加密

发布时间:2021-05-27 18:18:39 来源:亿速云 阅读:138 作者:Leah 栏目:编程语言

怎么在Java中使用DES实现数据加密?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

1.数据在网络中传输时,需要进行加密处理

双方约定一个相同的key(key不在网络中进行传输,只传输加密数据),然后根据将key根据一定的DES规则转换,得到真正的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;

/** 
 * 数据加密 DES方式 + Base64
 * @author sun_flower
 *
 */
public class EncryUtils {
  public static final String KEY = "gEpCIKFVdPEBJ1pM5pLSviM2Nrj5C/A4iAw8ou+jiJpnrXigolapdcJXfmh3tECyuQnaFrvZHabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmn";
  /**
   * 测试
   * @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);
  }
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI