项目中用到的数据加密方式是ecb模式的des加密得到的十六进制字符串。技术支持让写一个.net版的加密算法。这里做一下记录。
16进制使用的是bouncycastle。
import com.emaxcard.codec.codecexception; import com.emaxcard.codec.hex; import sun.misc.base64encoder; import javax.crypto.cipher; import javax.crypto.secretkey; import javax.crypto.spec.secretkeyspec; public class desencrypt { public static string encodeecb(string src, string key) throws codecexception { try { secretkey deskey = new secretkeyspec(key.getbytes("utf-8"), "desede"); cipher cipher = cipher.getinstance("desede/ecb/pkcs5padding"); cipher.init(cipher.encrypt_mode, deskey); byte[] cipherinfo = cipher.dofinal(src.getbytes("utf-8")); system.out.println("cipherinfo:"+new base64encoder().encode(cipherinfo)); return hex.encode(cipherinfo); } catch (exception var5) { throw new codecexception(var5); } } public static string decodeecb(string src, string key) throws codecexception { try { secretkey deskey = new secretkeyspec(key.getbytes("utf-8"), "desede"); cipher cipher = cipher.getinstance("desede/ecb/pkcs5padding"); cipher.init(cipher.decrypt_mode, deskey); byte[] decoderes = cipher.dofinal(hex.decode(src)); return new string(decoderes, "utf-8"); } catch (exception var5) { throw new codecexception(var5); } } }
public class hex { public hex() { } public static byte[] decode(string data) throws codecexception { try { return org.bouncycastle.util.encoders.hex.decode(data); } catch (exception var2) { throw new codecexception(var2.getmessage(), var2); } } public static string encode(byte[] data) { return new string(org.bouncycastle.util.encoders.hex.encode(data)); } public static void main(string[] args) throws codecexception { system.out.println(encode("a张y".getbytes())); system.out.println(new string(decode(""))); } }
using system; using system.io; using system.security.cryptography; using system.text; namespace consoleapplication1 { class desencrypt { public static string encodeecb(string encryptstring, string key) { byte[] keybytes = encoding.utf8.getbytes(key.substring(0, 8)); byte[] keyiv = keybytes; byte[] inputbytearray = encoding.utf8.getbytes(encryptstring); descryptoserviceprovider provider = new descryptoserviceprovider(); provider.mode = ciphermode.ecb; provider.padding = paddingmode.pkcs7; memorystream mstream = new memorystream(); cryptostream cstream = new cryptostream(mstream, provider.createencryptor(keybytes, keyiv), cryptostreammode.write); cstream.write(inputbytearray, 0, inputbytearray.length); cstream.flushfinalblock(); //return convert.tobase64string(mstream.toarray()); return hex.encode(mstream.toarray()); } public static string desdecrypt(string decryptstring, string key) { byte[] keybytes = encoding.utf8.getbytes(key.substring(0, 8)); byte[] keyiv = keybytes; //byte[] inputbytearray = convert.frombase64string(decryptstring); byte[] inputbytearray = hex.decode(decryptstring); descryptoserviceprovider provider = new descryptoserviceprovider(); provider.mode = ciphermode.ecb; provider.padding = paddingmode.pkcs7; memorystream mstream = new memorystream(); cryptostream cstream = new cryptostream(mstream, provider.createdecryptor(keybytes, keyiv), cryptostreammode.write); cstream.write(inputbytearray, 0, inputbytearray.length); cstream.flushfinalblock(); return encoding.utf8.getstring(mstream.toarray()); } } }
using system; using system.globalization; using system.text; namespace consoleapplication1 { sealed class hex { public static byte[] decode(string mhex) { mhex = mhex.replace(" ", ""); if (mhex.length <= 0) return null; byte[] vbytes = new byte[mhex.length / 2]; for (int i = 0; i < mhex.length; i += 2) if (!byte.tryparse(mhex.substring(i, 2), numberstyles.hexnumber, null, out vbytes[i / 2])) vbytes[i / 2] = 0; return vbytes; } public static string encode(byte[] data) { //** 以下两种方式都可以 //方式1 stringbuilder hexstring = new stringbuilder(); for (int i = 0; i < data.length; i++) { hexstring.appendformat("{0:x2}", data[i]); //system.convert.tostring(data[i], 16); } return hexstring.tostring(); //方式2 //return bitconverter.tostring(data).replace("-", "").tolower(); } } }
bitconverter.tostring方法签名:
// // 摘要: // 将指定的字节数组的每个元素的数值转换为它的等效十六进制字符串表示形式。 // // 参数: // value: // 字节数组。 // // 返回结果: // 由以连字符分隔的十六进制对构成的字符串,其中每一对表示 value 中对应的元素;例如“7f-2c-4a”。 // // 异常: // system.argumentnullexception: // value 为 null。 public static string tostring(byte[] value);
des是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法。与之对应的是非对称加密算法,例如rsa,它是公钥加密并且私钥解密。
des加密算法有如下几个要素:
在线des加密工具:http://tool.chacuo.net/cryptdes
ref:https://www.cnblogs.com/langtianya/p/3715975.html
如对本文有疑问, 点击进行留言回复!!
Springboot项目因为kackson版本问题启动报错解决方案
Java多线程下的其他组件之CyclicBarrier、Callable、Future和FutureTask详解
网友评论