PHP DES-ECB加密对接Java解密
最近公司有个业务,需要对接第三方接口,但是参数是需要加密的,对方也只提供了一个java的demo,在网上到处搜索,没有找到直接就能用的方法,后来还是跟公司的Android工程师对接出来的,在这里记录一下大致的流程。
首先说明一下对方要求的接口请求方式,格式为:http://ip:port/interface/method?data=摘要@@16进制字符串
说明:
1. 请求参数需要组合成a=1&b=2&c=3格式的参数串;
2. 摘要的生成方法为md5('a=1&b=2&c=3');
3. 16进制字符串的生成方法是将参数串进行**对称加密(DES-ECB)**后再转成16进制字符串(bin2hex函数);
对方提供的demo(java版本)如下:
public class DesUtils {
/** 默认密钥 */
private static String strDefaultKey = "seeyonssokey";
/** 加密工具 */
private Cipher encryptCipher;
/** 解密工具 */
private Cipher decryptCipher;
/**
* 加密字符串
* @param strIn 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encryptCipher.doFinal(strIn.getBytes()));
}
/**
* 解密字符串
* @param strIn 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
return new String(decryptCipher.doFinal(hexStr2ByteArr(strIn)));
}
/**
* 将 byte 数组转换为表示 16 进制值的字符串, 如: byte[]{8,18} 转换为: 0813 , 和 public static
* byte[] hexStr2ByteArr(String strIn) 互为可逆的转换过程
* @param arrB 需要转换的 byte 数组
* @return 转换后的字符串
* @throws Exception 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for(int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while(intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if(intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
/**
* 将 表 示 16 进 制 值 的 字 符 串 转 换 为 byte 数 组 , 和 public static String
* byteArr2HexStr(byte[] arrB) 互为可逆的转换过程
* @param strIn 需要转换的字符串
* @return 转换后的 byte 数组
* @throws Exception 本方法不处理任何异常,所有异常全部抛出
* @author <a href="mailto:leo841001@163.com">LiGuoQing</a>
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for(int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte)Integer.parseInt(strTmp, 16);
}
return arrOut;
}
/**
* 从指定字符串生成密钥,密钥所需的字节数组长度为 8 位 不足 8 位时后面补 0 , 超出 8 位只取前 8 位
* @param arrBTmp 构成该字符串的字节数组
* @return 生成的密钥
* @throws java.lang.Exception
*/
private static Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for(int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
// 生成密钥
Key key = new SecretKeySpec(arrB, "DES");
return key;
}
public static String encrypt(String strIn, String secretkey) throws Exception {
Key key = getKey(secretkey.getBytes("utf-8"));
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return byteArr2HexStr(cipher.doFinal(strIn.getBytes("utf-8")));
}
public static String decrypt(String strIn, String secretkey) throws Exception {
Key key = getKey(secretkey.getBytes("utf-8"));
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] param = cipher.doFinal(hexStr2ByteArr(strIn));
return new String(param, "utf-8");
}
}
PHP实现加密功能的的方法有两种:
- mcrypt方式(适用于php7.0以前的版本):
<?php
$text = 'a=1&b=2&c=3'; //参数串
$key = 'abcd1234'; // 密钥
echo $ret = self::encryptForDES($text , $key); //加密
/**
* 字符串加密(加密方法:DES-ECB)
* @param string $input 待加密字符串
* @param string $key 对称加密密钥
* @return string
*/
function encryptData($input, $key)
{
$size = mcrypt_get_block_size('des','ecb'); // 计算加密算法的分组大小
$input = self::pkcs5_pad($input, $size); // 对字符串进行补码
$td = mcrypt_module_open('des', '', 'ecb', '');
$iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
@mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = bin2hex($data); // 将加密后的字符串转化为16进制
return $data;
}
/**
- 对字符串进行补码
- @param string $text 原字符串
- @param string $blockSize 加密算法的分组大小
- @return string
*/
function pkcs5_pad($text, $blockSize)
{
$pad = $blockSize - (strlen($text) % $blockSize);
return $text . str_repeat(chr($pad), $pad);
}
输出为:990389f0aad8d12014ca6a45cd72cdf7
- openssl方式(适用于php7.0以后的版本):
<?php
$text = 'a=1&b=2&c=3'; // 参数串
$key = 'abcd1234'; // 密钥
echo encryptData($text, $key); // 加密
/**
* 字符串加密(加密方法:DES-ECB)
* @param string $input 待加密字符串
* @param string $key 对称加密密钥
* @return string
*/
function encryptData($input, $key)
{
$ivlen = openssl_cipher_iv_length('DES-ECB'); // 获取密码iv长度
$iv = openssl_random_pseudo_bytes($ivlen); // 生成一个伪随机字节串
$data = openssl_encrypt($input, 'DES-ECB', $key, $options=OPENSSL_RAW_DATA, $iv); // 加密
return bin2hex($data);
}
输出为:990389f0aad8d12014ca6a45cd72cdf7
原文地址:https://segmentfault.com/a/1190000016888916
PHP DES-ECB加密对接Java解密的更多相关文章
- DES c#加密后java解密
public static String byteArr2HexStr(byte[] bytIn) { StringBuilder builder = new StringBuilder(); for ...
- 利用DES,C#加密,Java解密代码
//C#加密 /// <summary> /// 进行DES加密. /// </summary> /// <param name="pToEncrypt&quo ...
- node-rsa加密,java解密调试
用NODE RSA JS 加密解密正常,用JAVA RSAUtils工具类加密解密正常.但是用node加密玩的java解密不了.原因:node默认的是 DEFAULT_ENCRYPTION_SCHEM ...
- c#加密,java解密(3DES加密)
c#代码 using System; using System.Security; using System.Security.Cryptography; using System.IO; using ...
- js前端3des加密 后台java解密
import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import java ...
- 前端 js加密 后台java 解密 RSA
前端代码 : $.ajax({ type:"GET", url:"http://localhost:8084/getPulbicKey", dataType:& ...
- RSA非对称加密,使用OpenSSL生成证书,iOS加密,java解密
最近换了一份工作,工作了大概一个多月了吧.差不多得有两个月没有更新博客了吧.在新公司自己写了一个iOS的比较通用的可以架构一个中型应用的不算是框架的一个结构,并已经投入使用.哈哈 说说文章标题的相关的 ...
- python des ecb 加密 demo
# -*- coding:utf-8 -*- from pyDes import * def hexString2bytes(src): ret =[] for i in range(len(src) ...
- DES ECB 模式 JAVA PHP C# 实现 加密 解密 兼容
版本一: JAVA: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sun.misc.BASE64Decoder; i ...
随机推荐
- python 3.x 学习笔记11 (静态、类、属性、特殊成员方法)
1.静态方法通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法.静态方法是不可以访问实例变量或类变量的即没有self,一个不能访问实例变量和类变量的方法,其实相当于跟类本身已经没什 ...
- 一袭白衣一 IDEA的破解安装以及汉化
DEA是一款比eclipse用起来更好用的一款代码编辑器,本人之前也是一直在用eclipse来写代码,后来发现了IDEA用起来会更顺手,所以又转用IDEA了,今天给大家分享一下IDEA的下载安装破解以 ...
- 大数相乘(牛客网ac通过)
2019-05-172019-05-17 大数相乘基本思想: 相乘相加,只不过大于10先不进位到计算完后统一进位 #include <iostream> #include <stri ...
- win10 无法访问XP 共享目录原因
win10 无法访问XP 共享目录原因 *现象: 在地址栏中输入\\192.168.100.5 (XP文件服务器),出现:.....找不到网络路径, 此连接尚未还原. ...
- 关于Scrapy爬虫项目运行和调试的小技巧(下篇)
前几天给大家分享了关于Scrapy爬虫项目运行和调试的小技巧上篇,没来得及上车的小伙伴可以戳超链接看一下.今天小编继续沿着上篇的思路往下延伸,给大家分享更为实用的Scrapy项目调试技巧. 三.设置网 ...
- 基本数据类型(int,bool,str)
1.int bit_lenth() 计算整数在内存中占用的二进制码的长度 十进制 二进制 长度(bit_lenth()) 1 1 1 2 10 2 4 100 3 8 1000 4 16 10000 ...
- selenium自动化(一).........................................搭建环境
一 环境搭建 安装python(建议使用py3) py2和py3在语法上会有一定的差别 第三方插件逐步转向py3,很多py2的插件已经停止维护 本教程的所有代码基于py3 安装selenium插件 ...
- 51nod 1079 中国剩余定理模板
中国剩余定理就是同余方程组除数为质数的特殊情况 我直接用同余方程组解了. 记得exgcd后x要更新 还有先更新b1再更新m1,顺序不能错!!(不然会影响到b1的更新) #include<cstd ...
- 【codeforces 128C】Games with Rectangle
[题目链接]:http://codeforces.com/problemset/problem/128/C [题意] 让你一层一层地在n*m的网格上画k个递进关系的长方形;(要求一个矩形是包含在另外一 ...
- CodeForces - 552E Vanya and Brackets
Vanya and Brackets Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u ...