• C#加密NodeJS解密


    C#代码:

        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("encrypted as: {0}", TripleDESEncrypt("12345", "abcdefghijklmnop", "pgfYadhCout=", false));
    
            }
            public static string TripleDESEncrypt(string toEncrypt, string key, string iv, bool useHashing)
            {
                byte[] keyArray;
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
                byte[] ivArray = Convert.FromBase64String(iv);
    
                if (useHashing)
                {
                    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                }
                else
                    keyArray = UTF8Encoding.UTF8.GetBytes(key);
    
                var tdes = new TripleDESCryptoServiceProvider();
                tdes.Key = keyArray;
                tdes.IV = ivArray;
                // tdes.Mode = CipherMode.CBC;  // which is default     
                // tdes.Padding = PaddingMode.PKCS7;  // which is default
    
                ICryptoTransform cTransform = tdes.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0,
                    toEncryptArray.Length);
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
        }

    NodeJS的javascript代码:

    var crypto = require('crypto');
    var alg = 'des-ede-cbc';
    var key = new Buffer('abcdefghijklmnop', 'utf-8');
    var iv = new Buffer('pgfYadhCout=', 'base64');
    
    var EncryptDES = function (data) {
      var cipher = crypto.createCipheriv(alg, key, iv);
      var encoded = cipher.update(data, 'ascii', 'base64');
      encoded += cipher.final('base64');
      return encoded;
    };
    
    var DecryptDES = function (data) {   
      var encrypted = new Buffer(data, 'base64');                
      var decipher = crypto.createDecipheriv(alg, key, iv);
      var decoded = decipher.update(encrypted, 'binary', 'ascii');
      decoded += decipher.final('ascii');
      return decoded;
    };
  • 相关阅读:
    K8S Flannel容器集群网络部署
    css 使页面一秒变灰
    js 2023. 连接后等于目标字符串的字符串对
    js . 找出数组中的第一个回文字符串
    react中的插槽
    Ajax Interceptor安装使用介绍
    JS移位运算符(<<、>>和>>>)
    react列表渲染提取组件
    js 矩阵置零
    onMenuShareTimeline自定义分享内容无效,不显示标题、图标
  • 原文地址:https://www.cnblogs.com/yumianhu/p/3723442.html
Copyright © 2020-2023  润新知