des加密演算法有如下幾個要素: DES加密模式:這裡選ECB 填充:java是pkcs5padding,.net是pkcs7padding。網上說PKCS5Padding與PKCS7Padding基本上是可以通用的。 字元集:utf-8 輸出:base64、hex 密碼/Key:8個字元(共64位... ...
項目中用到的數據加密方式是ECB模式的DES加密得到的十六進位字元串。技術支持讓寫一個.net版的加密演算法。這裡做一下記錄。
java版:
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(""))); } }
.net(c#)版:
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
DES是一種對稱加密演算法,所謂對稱加密演算法即:加密和解密使用相同密鑰的演算法。與之對應的是非對稱加密演算法,例如RSA,它是公鑰加密並且私鑰解密。
des加密演算法有如下幾個要素:
- DES加密模式:這裡選ECB
- 填充:java是pkcs5padding,.net是pkcs7padding。網上說PKCS5Padding與PKCS7Padding基本上是可以通用的。
- 字元集:utf-8
- 輸出:base64、hex
- 密碼/Key:8個字元(共64位)------java要求傳24個字元,不過加密也是截取的前8位
- 待加密/解密的文本
線上des加密工具:http://tool.chacuo.net/cryptdes
ref:https://www.cnblogs.com/langtianya/p/3715975.html