具體的加密演算法可以可自行查詢其區別,這裡只是拋磚引玉,大部分加密方法基本都能通過改變傳入參數來實現。 C#相關類文檔: System.Security.Cryptography 命名空間 | Microsoft Learn Node JS相關文檔:Crypto | Node.js v16.20.0 ...
具體的加密演算法可以可自行查詢其區別,這裡只是拋磚引玉,大部分加密方法基本都能通過改變傳入參數來實現。
C#相關類文檔: System.Security.Cryptography 命名空間 | Microsoft Learn
Node JS相關文檔:Crypto | Node.js v16.20.0 Documentation (nodejs.org)
C#加密函數:
1 using System; 2 using System.ComponentModel; 3 using System.Security.Cryptography; 4 using System.Text; 5 6 namespace Hello 7 { 8 class HelloWorld 9 { 10 //預設的加密密鑰,不得少於8位,否則會報錯 11 private static readonly string key = "password"; 12 13 static void Main(string[] args) 14 { 15 String text = HelloWorld.EncryptString("plaintext", key); 16 string decy = HelloWorld.DecryptString("9M2Z9AqQqdfoURRguzzSAA==", key); 17 Console.WriteLine(text); 18 Console.WriteLine(decy); 19 } 20 21 //解密演算法 22 public static string DecryptString(string decryptString, string decryptKey) 23 { 24 try 25 { 26 27 byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8)); 28 //初始化偏移向量,因為第一個明文分組沒有前一組密文進行異或,所以這裡是要有一個初始化向量的 29 byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 30 byte[] inputByteArray = Convert.FromBase64String(decryptString); 31 using DESCryptoServiceProvider DCSP = new(); 32 MemoryStream mStream = new MemoryStream(); 33 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 34 cStream.Write(inputByteArray, 0, inputByteArray.Length); 35 cStream.FlushFinalBlock(); 36 return Encoding.UTF8.GetString(mStream.ToArray()); 37 } 38 catch 39 { 40 return decryptString; 41 } 42 } 43 44 //加密演算法 45 public static string EncryptString(string encryptString, string encryptKey) 46 { 47 try 48 { 49 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); 50 byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 51 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); 52 using DESCryptoServiceProvider DCSP = new(); 53 MemoryStream mStream = new MemoryStream(); 54 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 55 cStream.Write(inputByteArray, 0, inputByteArray.Length); 56 cStream.FlushFinalBlock(); 57 return Convert.ToBase64String(mStream.ToArray()); 58 } 59 catch 60 { 61 return encryptString; 62 } 63 } 64 } 65 }
控制台輸出為
Node JS加密函數為:
1 const key = 'password' 2 const arr = [0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef] 3 const iv = Buffer.from(arr) 4 5 /** 6 * des-cbc加密函數 7 * @param plaintext 原文 8 * @param alg 加密方式,這裡統一用cbc加密 9 * @returns {string} 密文 10 */ 11 function encrypt(plaintext, alg) { 12 //創建加密對象,參數為 13 //加密方式(string), 密鑰(string), 初始向量(ArrayBuffer,二進位數組) 14 let cipher = crypto.createCipheriv(alg, key, iv); 15 16 //自動填充,否則輸入長度不為密碼塊的倍數時會報錯 17 cipher.setAutoPadding(true); 18 19 //用加密對象進行加密,參數為 20 //data: 原始數據,一般為string,其他類型則忽略輸入類型 21 //inputencoding: 數據的輸入編碼方式,這裡轉換為unicode 22 //outputencoding: 數據的輸出編碼方式,這裡是用base64,其特點是存在非3倍數時末尾會出現'=' 23 //return:返回加密密文,類型為string 24 let ciph = cipher.update(plaintext, 'utf8', 'base64'); 25 26 //將剩餘內容全部進行加密並返回總結果,因為上面的一次只能加密部分數據 27 ciph += cipher.final('base64') 28 return ciph 29 } 30 31 /** 32 * des-cbc解密數據 33 * @param ciphertext 密文 34 * @param alg 解密方式 35 * @returns {string} 原文 36 */ 37 function decrypt(ciphertext, alg) { 38 //解碼失敗則返回空,因為有些早期數據沒有密碼 39 if(!ciphertext) return '' 40 let dcipher = crypto.createDecipheriv(alg, key, iv); 41 dcipher.setAutoPadding(true); 42 let ciph = dcipher.update(ciphertext, 'base64', 'utf8'); 43 ciph += dcipher.final('utf8') 44 return ciph 45 }
//調用
console.log(encrypt('plaintext', 'des-cbc'))
console.log(decrypt('9M2Z9AqQqdfoURRguzzSAA==', 'des-cbc'))
控制台輸出為:
註意:因為JS中不存在二進位數據類型,因此需要用到Buffer來轉換。基本語法都是這樣,若要其他加密演算法以及編碼方式,則更改對應參數即可,譬如
encrypt('plaintext', 'des-cfb') //cfb加密
let ciph = cipher.update(plaintext, 'utf8', 'hex'); //hex編碼方式
個人筆記記錄。