淄博网站建设淄博wordpress 商品 模板
news/
2025/9/22 18:41:35/
文章来源:
淄博网站建设淄博,wordpress 商品 模板,珠海logo设计,怎么查公司名称是否被注册商标大家好#xff0c;我是烤鸭:今天分享的是java 和 php des 加密。因为接口对接#xff0c;难免不同语言#xff0c;加密又是必不可少的。作为接口的提供方#xff0c;必须把加密规则写好#xff0c;最好有不同语言的加密demo。1. java版本的des加密解密工具类DESTools.j…大家好我是烤鸭:今天分享的是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* returnfor(byte b : bytes) { // 使用String的format方法进行转换buf.append(String.format(%02x, new Integer(b 0xff)));}return buf.toString();}
}
可以看出秘钥是16进制的byte数组先用上面的 bytesToHexFun3 方法获取字符串
得到的就是秘钥就是 0b13e7b2510d75c24edd4b512436a8280b13e2b2310d75c1运行main方法对123456就行加密得到aKZ47zrp0I再解密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 ($i0; $i strlen($hex)-1; $i2){$string . chr(hexdec($hex[$i].$hex[$i1]));}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
调试结果如图
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/910006.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!