大家好,我是烤鸭:
今天分享的是java 和 php des 加密。
因为接口对接,难免不同语言,加密又是必不可少的。
作为接口的提供方,必须把加密规则写好,最好有不同语言的加密demo。
1. java版本的des加密解密工具类
DESTools.java
package com.xxxx.xxx.util;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.commons.codec.binary.Base64;
public class DESTools {
public static DESTools instance;
public static DESTools getInstace()
{
if(instance == null)
{
instance = new DESTools();
}
return instance;
}
Key key;
/**
* 密钥
*/
private static byte[] BOSS_SECRET_KEY = { 0x0c, 0x13, (byte) 0xe7,
(byte) 0xb2, 0x51, 0x0d, 0x75, (byte) 0xc3, 0x4f, (byte) 0xdd,
(byte) 0x4e, (byte) 0x51, 0x24, 0x36, (byte) 0xa6, (byte) 0x28,
0x0b, 0x13, (byte) 0xe2, (byte) 0xb8, 0x31, 0x0d, 0x75, (byte) 0xc1 };
private String key2 = "0b13e7b2510d75c24edd4b512436a8280b13e2b2310d75c1";
public DESTools() {
setKey(BOSS_SECRET_KEY);
}
/**
* 根据参数生成KEY
*/
public void setKey(byte[] strKey) {
try {
System.out.println(bytesToHexFun3(BOSS_SECRET_KEY));
DESKeySpec dks = new DESKeySpec(BOSS_SECRET_KEY);
SecretKeyFactory keyFactory;
keyFactory = SecretKeyFactory.getInstance("DES");
this.key = keyFactory.generateSecret(dks);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing DESTOOLS class. Cause: " + e);
}
}
/**
* 加密String明文输入,String密文输出
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
Base64 base64en = new Base64();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encodeAsString(byteMi);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing DESTOOLS class. Cause: " + e);
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文输入,String明文输出
* @param strMi
* @return
*/
public String getDesString(String strMi) {
Base64 base64De = new Base64();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = base64De.decode(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
throw new RuntimeException("Error initializing DESTOOLS class. Cause: " + e);
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文输入,byte[]密文输出
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing DESTOOLS class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文输入,以byte[]明文输出
* @param byteD
* @return
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing DESTOOLS class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
}
public static void main(String[] args) {
DESTools desTools = new DESTools();
String string = desTools.getEncString("19760519");
System.out.println(string);
}
/**
* 将16进制字符串转换为byte[]
*
* @param str
* @return
*/
public static byte[] toBytes(String str) {
if(str == null || str.trim().equals("")) {
return new byte[0];
}
byte[] bytes = new byte[str.length() / 2];
for(int i = 0; i < str.length() / 2; i++) {
String subStr = str.substring(i * 2, i * 2 + 2);
bytes[i] = (byte) Integer.parseInt(subStr, 16);
}
return bytes;
}
/**
* 方法三:
* byte[] to hex string
*
* @param bytes
* @return
for(byte b : bytes) { // 使用String的format方法进行转换
buf.append(String.format("%02x", new Integer(b & 0xff)));
}
return buf.toString();
}
}
可以看出秘钥是16进制的byte数组,先用上面的 bytesToHexFun3 方法获取字符串
得到的就是秘钥就是 0b13e7b2510d75c24edd4b512436a8280b13e2b2310d75c1
运行main方法,对123456就行加密得到aKZ47zr+p0I=,再解密
2. 相同的php加密和解密方法
<?php
$str = '0c13e7b2510d75c34fdd4e512436a6280b13e2b8310d75c1';
$string = des_ecb_encrypt('123456',Hex2String($str));
echo '加密结果++',$string;
echo '换行++++++++++++++++++++++++++++++++++';;
echo '解密结果++',des_ecb_decrypt($string,Hex2String($str));
function Hex2String($hex){
$string='';
for ($i=0; $i < strlen($hex)-1; $i+=2){
$string .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return $string;
}
function des_ecb_encrypt($data, $key){
return openssl_encrypt ($data, 'des-ecb', $key);
}
function des_ecb_decrypt ($data, $key){
return openssl_decrypt ($data, 'des-ecb', $key);
}
?>
$str 就是上面java加密解密的key,先转换成16进制byte数组,再进行加密解密。
主要调用的是openssl的des加密方法。
还有别的加密模式,可以参考这篇: https://www.36nu.com/post/252
如果你没安装wampserver的话,安装可以参考这篇。
https://blog.csdn.net/Angry_Mills/article/details/80790522
或者你懒得安装,也可以在线调试php代码。
在线调试地址: https://c.runoob.com/compile/1
调试结果如图: