Using Javascriptservice to implement DES encryption algorithms in. NET Core

Source: Internet
Author: User
Tags base64 benchmark decrypt openssl library

The article "ASP. NET Core Love JavaScript" and "cross-platform NodeJS component solution." Netcore some issues that do not support the System.Drawing Graphics feature provides a set of solutions for our extended. NET Core API, which was seen last week. NET encryption and decryption algorithm found so far did not include DES algorithm, GitHub on just joined, specifically can see https://group.cnblogs.com/topic/75273.html.

The Crypto Library of node. JS provides a variety of cryptographic algorithms that make it very easy to use cryptography to solve problems in application development. The crypto library is packaged and distributed with the Nodejs kernel, which provides encryption, decryption, signing, verification and other functions. Crypto uses the OpenSSL library to implement its encryption technology, which provides a series of hashing methods in OpenSSL, including encapsulation of HMAC, cipher, decipher, signature, and validation methods. Crypto Official document: http://nodejs.org/api/crypto.html, blog post http://blog.fens.me/nodejs-crypto/written in very detailed. This article describes how to use the DES algorithm of Crypto to help us implement the DES algorithm that is available immediately.

1, we refer to the official document https://github.com/aspnet/JavaScriptServices/tree/dev/src/Microsoft.AspNetCore.NodeServices# Microsoftaspnetcorenodeservices, we create a. NET Core console Application Dotnetnodeapp, adding a Microsoft.AspNetCore.NodeServices package reference:

Install-package Microsoft.aspnetcore.nodeservices–pre

2, the configuration environment, the. NET core default is to adopt the dependency injection mode, we in this javascriptservice middleware also has the demand to use to the dependency injection, the specific reference Dudu article: uses the dependency injection in the. NET Core Console Program


Iservicecollection Services = new servicecollection ();
Injection
Services. Addnodeservices (options =
{
Options. ProjectPath = @ "C:\Users\geffz\Documents\visual Studio 2015\projects\dotnetnodeapp\src\dotnetnodeapp";
Options. Watchfileextensions = new[] {". js", ". Sass"};
... etc.-See other properties below
});

Building containers
IServiceProvider serviceprovider = Services. Buildserviceprovider ();
Inodeservices nodeservices = serviceprovider.getrequiredservice<inodeservices> ();

3, we create a node folder in the project, and then add a cryptutil.js, the file content is as follows:

var crypto = require (' crypto ');

Module.exports = {
    encrypt:function (Callback,plaintext, Key,iv) {
         var ECB =  ' DES-ECB ';
        var enkey = new Buffer (key);
        var eniv = new Buffer (iv? iv:0);
        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/* ERROR */, CIPH);
   },

Decrypt:function (Callback, Encrypt_text,key, iv) {
var ECB = ' DES-ECB ';
var dekey = new Buffer (key);
var deiv = new Buffer (iv? iv:0);
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);
}
};

Here is a JS function, which will be called in A. NET program, by passing in a node-style callback function and three parameters to calculate the result. In Nodejs, a JS file represents a module, which means that the module.exports current function is provided as an object for invocation, and we have two functions representing encryption/decryption respectively.

4. Create a des class encapsulated Nodejs function call:

Using Microsoft.AspNetCore.NodeServices;
Using System.Threading.Tasks;

Namespace Dotnetnodeapp
{
public class Des
{
Private Inodeservices nodeservices;

Public Des (Inodeservices nodeservices)
{
This.nodeservices = nodeservices;
}

Public Async task<string> Encryptdes (string data, string key, int IV)
{
var result = await nodeservices.invokeexportasync<string> ("./node/cryptutil", "Encrypt", data, key, IV);
return result;
}

Public Async task<string> Decryptdes (string data, string key, int vi)
{
var result = await nodeservices.invokeexportasync<string> ("./node/cryptutil", "Decrypt", Data,key, VI);
return result;
}
}
}

Let's take a look at InvokeExportAsync<T> ('), he's an asynchronous method that gets a result by passing in a node. js script file (module), three parameters.

Method signature:invokeexportasync<t> (String modulename, String exportname, params object[] args)


5, we test in the console of our packaging effect

Des desutil = new des (nodeservices);
String data = "Geffzhang";
string key = "12345678";

String temp = desutil.encryptdes (data, key, 0). Result;
Console.WriteLine (temp);
String end = Desutil.decryptdes (temp,key,0). Result;
Console.WriteLine (end);
Console.read ();

You can see the effect by running it:

6, how to use the performance, we use the performance test component benchmarkdotnet See performance data, using the method reference. NET Core Performance test components benchmarkdotnet support the. NET Framework Mono: We create a class Desbenchmark and add the benchmark attribute to the method

Using Benchmarkdotnet.attributes;
Using Microsoft.AspNetCore.NodeServices;
Using System;
Using Microsoft.Extensions.DependencyInjection;
Using Benchmarkdotnet.running;

Namespace Dotnetnodeapp
{
public class Desbenchmark
{
Private Iservicecollection services;
Private IServiceProvider serviceprovider;
Private Inodeservices nodeservices;

Public Desbenchmark ()
{
Iservicecollection Services = new servicecollection ();
Injection
Services. Addnodeservices (options =
{
Options. ProjectPath = @ "C:\Users\geffz\Documents\visual Studio 2015\projects\dotnetnodeapp\src\dotnetnodeapp";
Options. Watchfileextensions = new[] {". js", ". Sass"};
... etc.-See other properties below
});

Building containers
ServiceProvider = Services. Buildserviceprovider ();
Nodeservices = serviceprovider.getrequiredservice<inodeservices> ();
}

private string data = "Geffzhang";
private string encrydata = "utrlynkktafuxmjthplyoa==";
private string key = "12345678";

[Benchmark]
public string Encryptdes ()
{
Des desutil = new des (nodeservices);
return Desutil.encryptdes (data, key, 0). Result;
}

[Benchmark]
public string Decryptdes ()
{
Des desutil = new des (nodeservices);
Return Desutil.encryptdes (Encrydata, key, 0). Result;
}

}
}

The following is the result of the console output, performance is good

Using Javascriptservice to implement DES encryption algorithms in. NET Core

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.