• .Net Core中使用NodeJs加解密DES,MD5,AES,REA


       鉴于使用.net core我们的加解密也同时迁移到了跨平台上,我使用的是NodeJs加解密的。废话不多说了,还是来干活吧。

    1.创建Node项目

    2.添加package.json

    {
      "name": "node-encrpty",
      "version": "0.0.0",
      "description": "NodeEncrpty",
      "main": "app.js",
      "author": {
        "name": "tl"
      },
      "dependencies": {
        "crypto": "0.0.3"
      }
    }

    红字标准的是要使用的npm包。

    --------------------

    已下是NodeJs核心代码了。

    var crypto = require('crypto');
    var fs = require('fs');

    module.exports = {
        Md5encrypt: function(callback, content) {
            var md5 = crypto.createHash('md5');
            md5.update(content);
            var result = md5.digest('hex').toUpperCase();
            callback(null, result);
            //return result;
        },
        DESencrypt: function(callback, plaintext, key) {
            var ecb = 'DES';
            var enkey = new Buffer(key);
            var iv = key;
            var eniv = new Buffer(iv ? iv : 0, 'binary');
            var cipher = crypto.createCipheriv(ecb, enkey, eniv);
            cipher.setAutoPadding(true) //default true
            var ciph = cipher.update(plaintext, 'utf8', 'base64');
            ciph += cipher.final('base64');
            callback(null, ciph);
            //return ciph;
        },
        DESdecrypt: function(callback, encrypt_text, key) {
            var ecb = 'DES';
            var dekey = new Buffer(key);
            var iv = key;
            var deiv = new Buffer(iv ? iv : 0, 'binary');
            var decipher = crypto.createDecipheriv(ecb, dekey, deiv);
            decipher.setAutoPadding(true);
            var txt = decipher.update(encrypt_text, 'base64', 'utf8');
            txt += decipher.final('utf8');
            callback(null, txt);
            //return txt;
        },
        RSAencrypt: function(callback, plaintext, key) {
            var data = new Buffer(plaintext);
            var result = crypto.publicEncrypt({ key: key, padding: crypto.constants.RSA_PKCS1_PADDING }, data).toString('base64');
            callback(null, result);
            //return result;
        },
        RSAdecrypt: function(callback, encrypt_text, key) {
            var data = new Buffer(encrypt_text, 'base64');
            var result = crypto.privateDecrypt({ key: key, passphrase: '123456', padding: crypto.constants.RSA_PKCS1_PADDING }, data).toString('utf8');
            callback(null, result);
            //return result;
        },
        AESencrypt: function(callback, plaintext, key) {
            var ecb = 'aes-128-ecb';
            var clearEncoding = 'utf8';
            var iv = "";
            var cipherEncoding = 'base64';
            var cipher = crypto.createCipheriv(ecb, key, iv);
            var cipherChunks = [];
            cipherChunks.push(cipher.update(plaintext, clearEncoding, cipherEncoding));
            cipherChunks.push(cipher.final(cipherEncoding));
            var result = cipherChunks.join('');
            callback(null, result);
            //return result;

        },
        AESdecrypt: function(callback, encrypt_text, key) {
            iv = "";
            var clearEncoding = 'utf8';
            var cipherEncoding = 'base64';
            var cipherChunks = [];
            var decipher = crypto.createDecipheriv('aes-128-ecb', key, iv);
            decipher.setAutoPadding(true);
            cipherChunks.push(decipher.update(encrypt_text, cipherEncoding, clearEncoding));
            cipherChunks.push(decipher.final(clearEncoding));
            var result = cipherChunks.join('');
            callback(null, result);
            //return result;
        }
    }

    3.使用属性注入类
    public NodeEncrpty Cryptor { get; set; }

    //RSA

    Cryptor.RSAdecrypt(model.EncryptKey).Result;

    //AES

    Cryptor.AESdecrypt(Data, AesKey).Result;

    //MD5

    Cryptor.MD5encrypt(data).Result;

    //DES

    Cryptor.DESencrypt(data,Des_Key).Result;

    好了以上就是NodeJs的加密方法了,请大家多多指教。

  • 相关阅读:
    通过PROFINET网络实现SINAMICS 120的PN IO OPC通讯,起动及调速控制
    Python datetime获取当前年月日时分秒
    计算机网络:套接字(Socket)| Python socket实现服务器端与客户端通信,使用TCP socket阿里云ECS服务器与本机通信
    Ubuntu16.04安装、卸载宝塔软件
    Ubuntu一键安装LAMP,LNMP
    STM32使用K型热电偶测温:运算放大器+内置ADC+K型热电偶分度表+中间温度定律 | K型热电偶的温度-热电势曲线
    盘点几种DIY加密狗的制作方法,适用于穿越机模拟器
    变频器通讯参数PKW和PZD的含义
    穿越机从0到起飞:选件
    西门子S7-1200PLC不让下载一直报“模块具有激活的测试和调试功能,防止下载到设备”解决方法
  • 原文地址:https://www.cnblogs.com/tonglei/p/6674150.html
Copyright © 2020-2023  润新知