一、前言
在專案需要新增安全模組,客戶端呼叫服務端釋出的service必須要經過驗證,加密演算法採用DES,客戶端採用C#進行加密,服務端使用Java進行解密。廢話不多說,直接上程式碼。
二、客戶端
客戶端採用C#進行開發,C#進行DES加密解密程式碼清單如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO; namespace DESHelper { public class DESHelper { private static string m_encryptkey = "ENCRYPTT"; private static string m_str = "IAMACOEDR"; public static string DESEncrypt() { string str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + m_str; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(str); //建立加密物件的金鑰和偏移量 //原文使用ASCIIEncoding.ASCII方法的GetBytes方法 //使得輸入密碼必須輸入英文文字 des.Key = ASCIIEncoding.ASCII.GetBytes(m_encryptkey); des.IV = ASCIIEncoding.ASCII.GetBytes(m_encryptkey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString(); } public static string DESDecrypt(string pToDecrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } static void Main(string[] args) { string str = DESEncrypt(); Console.WriteLine(str); Console.WriteLine(DESDecrypt(str, m_encryptkey)); } } }
執行結果:
34DB26C86E933FB8F9C294A563BEF742D85451292A3C40C6FC8DF5A99C56EDCC
2016-03-10 12:43:05IAMACOEDR
三、伺服器
伺服器採用Java進行開發,Java進行DES加密解密程式碼清單如下:
import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec; import java.security.Key; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher; public class Des { //解密資料 public static String decrypt(String message,String key) throws Exception { byte[] bytesrc =convertHexString(message); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); byte[] retByte = cipher.doFinal(bytesrc); return new String(retByte); } // 加密資料 public static byte[] encrypt(String message, String key) throws Exception { Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); return cipher.doFinal(message.getBytes("UTF-8")); } public static byte[] convertHexString(String ss) { byte digest[] = new byte[ss.length() / 2]; for(int i = 0; i < digest.length; i++) { String byteString = ss.substring(2 * i, 2 * i + 2); int byteValue = Integer.parseInt(byteString, 16); digest[i] = (byte)byteValue; } return digest; } public static String toHexString(byte b[]) { StringBuffer hexString = new StringBuffer(); for (int i = 0; i < b.length; i++) { String plainText = Integer.toHexString(0xff & b[i]); if (plainText.length() < 2) plainText = "0" + plainText; hexString.append(plainText); } return hexString.toString(); } public static void main(String[] args) throws Exception { String key = "ENCRYPTT"; String value = "IAMACODER"; String formatString = java.net.URLEncoder.encode(value, "utf-8"); System.out.println("加密資料:"+ formatString); String encryptedString = toHexString(encrypt(formatString, key)); System.out.println("加密後的資料為:" + encryptedString); String decryptedString = java.net.URLDecoder.decode(decrypt(encryptedString, key), "utf-8") ; System.out.println("解密後的資料:" + decryptedString); } }
執行結果:
加密資料:IAMACODER
加密後的資料為:a8a3f8641ec18ddeff808105c493510e
解密後的資料:IAMACODER
四、測試C#加密&Java解密
將C#端加密的字串傳入Java端(替換decrypt中的encryptedString即可)直接進行解密,可以得到如下結果:
加密資料:IAMACODER
加密後的資料為:a8a3f8641ec18ddeff808105c493510e
解密後的資料:2016-03-10 12:43:05IAMACOEDR
五、總結
這是一個小的功能模組,有此需求的園友可以直接拿去使用,謝謝各位園友觀看~