翼度科技»论坛 编程开发 JavaScript 查看内容

C#与Node JS互相实现DES加密解密

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
具体的加密算法可以可自行查询其区别,这里只是抛砖引玉,大部分加密方法基本都能通过改变传入参数来实现。
C#相关类文档: System.Security.Cryptography 命名空间 | Microsoft Learn
Node JS相关文档:Crypto | Node.js v16.20.0 Documentation (nodejs.org)
 
C#加密函数:
  1. 1 using System;
  2. 2 using System.ComponentModel;
  3. 3 using System.Security.Cryptography;
  4. 4 using System.Text;
  5. 5
  6. 6 namespace Hello
  7. 7 {
  8. 8     class HelloWorld
  9. 9     {
  10. 10         //默认的加密密钥,不得少于8位,否则会报错
  11. 11         private static readonly string key = "password";
  12. 12
  13. 13         static void Main(string[] args)
  14. 14         {
  15. 15             String text = HelloWorld.EncryptString("plaintext", key);
  16. 16             string decy = HelloWorld.DecryptString("9M2Z9AqQqdfoURRguzzSAA==", key);
  17. 17             Console.WriteLine(text);
  18. 18             Console.WriteLine(decy);
  19. 19         }
  20. 20
  21. 21         //解密算法
  22. 22         public static string DecryptString(string decryptString, string decryptKey)
  23. 23         {
  24. 24             try
  25. 25             {
  26. 26                 
  27. 27                 byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
  28. 28                  //初始化偏移向量,因为第一个明文分组没有前一组密文进行异或,所以这里是要有一个初始化向量的
  29. 29                 byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  30. 30                 byte[] inputByteArray = Convert.FromBase64String(decryptString);
  31. 31                 using DESCryptoServiceProvider DCSP = new();
  32. 32                 MemoryStream mStream = new MemoryStream();
  33. 33                 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  34. 34                 cStream.Write(inputByteArray, 0, inputByteArray.Length);
  35. 35                 cStream.FlushFinalBlock();
  36. 36                 return Encoding.UTF8.GetString(mStream.ToArray());
  37. 37             }
  38. 38             catch
  39. 39             {
  40. 40                 return decryptString;
  41. 41             }
  42. 42         }
  43. 43
  44. 44         //加密算法
  45. 45         public static string EncryptString(string encryptString, string encryptKey)
  46. 46         {
  47. 47             try
  48. 48             {
  49. 49                 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
  50. 50                 byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  51. 51                 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  52. 52                 using DESCryptoServiceProvider DCSP = new();
  53. 53                 MemoryStream mStream = new MemoryStream();
  54. 54                 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  55. 55                 cStream.Write(inputByteArray, 0, inputByteArray.Length);
  56. 56                 cStream.FlushFinalBlock();
  57. 57                 return Convert.ToBase64String(mStream.ToArray());
  58. 58             }
  59. 59             catch
  60. 60             {
  61. 61                 return encryptString;
  62. 62             }
  63. 63         }
  64. 64     }
  65. 65 }   
复制代码
 
控制台输出为

 
Node JS加密函数为:
  1. 1 const key = 'password'
  2. 2 const arr = [0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef]
  3. 3 const iv = Buffer.from(arr)
  4. 4
  5. 5 /**
  6. 6  * des-cbc加密函数
  7. 7  * @param plaintext 原文
  8. 8  * @param alg 加密方式,这里统一用cbc加密
  9. 9  * @returns {string} 密文
  10. 10  */
  11. 11 function encrypt(plaintext, alg) {
  12. 12     //创建加密对象,参数为
  13. 13     //加密方式(string), 密钥(string), 初始向量(ArrayBuffer,二进制数组)
  14. 14     let cipher = crypto.createCipheriv(alg, key, iv);
  15. 15
  16. 16     //自动填充,否则输入长度不为密码块的倍数时会报错
  17. 17     cipher.setAutoPadding(true);
  18. 18
  19. 19     //用加密对象进行加密,参数为
  20. 20     //data: 原始数据,一般为string,其他类型则忽略输入类型
  21. 21     //inputencoding: 数据的输入编码方式,这里转换为unicode
  22. 22     //outputencoding: 数据的输出编码方式,这里是用base64,其特点是存在非3倍数时末尾会出现'='
  23. 23     //return:返回加密密文,类型为string
  24. 24     let ciph = cipher.update(plaintext, 'utf8', 'base64');
  25. 25
  26. 26     //将剩余内容全部进行加密并返回总结果,因为上面的一次只能加密部分数据
  27. 27     ciph += cipher.final('base64')
  28. 28     return ciph
  29. 29 }
  30. 30
  31. 31 /**
  32. 32  * des-cbc解密数据
  33. 33  * @param ciphertext 密文
  34. 34  * @param alg 解密方式
  35. 35  * @returns {string} 原文
  36. 36  */
  37. 37 function decrypt(ciphertext, alg) {
  38. 38     //解码失败则返回空,因为有些早期数据没有密码
  39. 39     if(!ciphertext) return ''
  40. 40     let dcipher = crypto.createDecipheriv(alg, key, iv);
  41. 41     dcipher.setAutoPadding(true);
  42. 42     let ciph = dcipher.update(ciphertext, 'base64', 'utf8');
  43. 43     ciph += dcipher.final('utf8')
  44. 44     return ciph
  45. 45 }<br><br>//调用
复制代码
  1. console.log(encrypt('plaintext', 'des-cbc'))<br>console.log(decrypt('9M2Z9AqQqdfoURRguzzSAA==', 'des-cbc'))
复制代码
控制台输出为:

 注意:因为JS中不存在二进制数据类型,因此需要用到Buffer来转换。基本语法都是这样,若要其他加密算法以及编码方式,则更改对应参数即可,譬如
encrypt('plaintext', 'des-cfb') //cfb加密
let ciph = cipher.update(plaintext, 'utf8', 'hex'); //hex编码方式
 
个人笔记记录。
 

来源:https://www.cnblogs.com/ricardox3/p/17434333.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具