項目中用到的數據加密方式是ECB模式的DES加密獲得的十六進制字符串。技術支持讓寫一個.net版的加密算法。這裏作一下記錄。html
16進制使用的是bouncycastle。java
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,它是公鑰加密而且私鑰解密。c#
des加密算法有以下幾個要素:數組
在線des加密工具:http://tool.chacuo.net/cryptdeside
注意:當DES加密使用的key與解密使用的key不同時,會報這個異常。工具
javax.crypto.BadPaddingException: Given final block not properly padded at com.emaxcard.codec.Desede.decodeECB(Desede.java:151)
ref:https://www.cnblogs.com/langtianya/p/3715975.htmlui