java php des加密 byte数组16进制 DESTools
大家好,我是烤鴨:
? ? 今天分享的是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就行加密得到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
調試結果如圖:
總結
以上是生活随笔為你收集整理的java php des加密 byte数组16进制 DESTools的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS开发之——屏幕适配-AutoLay
- 下一篇: 知名车联网架构图