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

  • 相关阅读:
    032 Gradle 下载的依赖jar包在哪?
    031 can't rename root module,Android Studio修改项目名称
    030 Cannot resolve symbol'R' 问题解决汇总大全
    029 Android Studio层级显示目录文件
    028 You are about to commit CRLF line separators to the Git repository.It is recommended to set the core. autocrlf Git attribute to true to avoid line separator issues If you choose Fix and Comit ,
    027 【Android基础知识】Android Studio 编译慢及 Adb connection Error:远程主机强迫关闭了一个现有的连接
    026 Android Studio 和Gradle版版本对应关系
    025 Cause: org.jetbrains.plugins.gradle.tooling.util.ModuleComponentIdentifierIm
    024 Android Studio上传项目到Github 最全记录
    023 解决AndroidStudio下载gradle慢的问题
  • 原文地址:https://www.cnblogs.com/tonglei/p/6674150.html
Copyright © 2011-2022 走看看