• .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的加密方法了,请大家多多指教。

  • 相关阅读:
    vue-cli3.0配置开发环境,测试环境,线上环境
    jQuery使用CDN加速
    浏览器中JavaScript脚本代码的load、ready等方法的加载顺序
    使用 JavaScript 拦截和跟踪浏览器中的 HTTP 请求
    Node和NPM在Windows环境下绿色安装及配置
    Nodejs 中将html转换成pdf文件
    数学励志公式:每天进步一点点
    网页调用打印机打印时纸张A4的设置
    用JS或jQuery访问页面内的iframe,兼容IE/FF
    HTML to DOM
  • 原文地址:https://www.cnblogs.com/tonglei/p/6674150.html
Copyright © 2020-2023  润新知