zoukankan      html  css  js  c++  java
  • .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的加密方法了,请大家多多指教。

  • 相关阅读:
    axios基本用法
    Iframe父子窗口之间的跨域事件调用和传值
    js 比较两个日期的大小
    小程序webview实践
    小程序入口构造工具&二维码测试工具
    小程序无限层级路由方案
    TypeScript基础类型,类实例和函数类型声明
    小程序多业务线融合【完整分包业务接入】
    浅谈React16框架
    CSS Modules 与 scoped 的不一样
  • 原文地址:https://www.cnblogs.com/tonglei/p/6674150.html
Copyright © 2011-2022 走看看