• 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;
    };
  • 相关阅读:
    JAVA 一个接口多个实现类
    关于Web服务器
    美团买菜IOS版设备风控浅析与算法还原
    阿里App防Bot新版AliTigerTally方案浅析与算法还原1
    使用php的openssl_encrypt和python的pycrypt进行跨语言的对称加密和解密问题
    一个把人民币小写转换为大写中文的方法
    《重构》代码坏味道
    git 合并分支
    java中SPI机制 代码改变世界
    echo print print_r的区别
  • 原文地址:https://www.cnblogs.com/yumianhu/p/3723442.html
Copyright © 2020-2023  润新知