栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java简单数据加密方法DES实现过程解析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java简单数据加密方法DES实现过程解析

这篇文章主要介绍了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;


public class EncryUtils {
  public static final String KEY = "gEpCIKFVdPEBJ1pM5pLSviM2Nrj5C/A4iAw8ou+jiJpnrXigolapdcJXfmh2tECyuQnaFrvZHabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmn";
  
  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);
  }
  
  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;
  }
  
  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);
    
  }
  
  
  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);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135701.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号