DES解密

Posted yaobiluo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DES解密相关的知识,希望对你有一定的参考价值。

import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
String key ="你自己的desKEY";//引用key
String body=prev.getResponseDataAsString();
import org.json.JSONObject; //导入
import org.json.*;
prev.setDataEncoding("utf-8");
// 加密方法
public String DecryptAsDoNet(String message) {
    try {
    	   byte[] encrypted1 = new BASE64Decoder().decodeBuffer(message);
        Base64.Encoder encoder = Base64.getEncoder();
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        byte[] encryptbyte = cipher.doFinal(encrypted1);
        String str =new String(encryptbyte,"utf-8");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 			     
        return str;
        }
    catch (Throwable e) {
        e.printStackTrace();
        return "";
    }
}	
    
 // 解密
String decrypt = DecryptAsDoNet(body);
vars.put("DESSign",decrypt);
JSONObject resbonseJson = new JSONObject(decrypt); //解析json
String code = resbonseJson.get("code").toString();
String Token = resbonseJson.get("data").get("accessToken").toString();
log.info("请求登录返回报文"+decrypt);
vars.put("DESSign",decrypt);
vars.put("rescode",code);
log.info("登录接口响应code"+code);
vars.put("Token",Token);
log.info("登录token:" + Token)

以上是关于DES解密的主要内容,如果未能解决你的问题,请参考以下文章

swift中的DES加密解密算法

JAVA DES加密解密代码范例

des加密 c++ java

DES加密解密与AES加密解密

java des 默认采用啥加密模式

5.Java 加解密技术系列之 DES