In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use JavaScript Service to implement DES encryption algorithm in .NET Core. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Node.js 's Crypto library provides a variety of encryption algorithms, which makes it very convenient for us to use cryptographic technology to solve problems in application development. The Crypto library is packaged and released with the Nodejs kernel, which mainly provides encryption, decryption, signature, verification and other functions. Crypto uses the OpenSSL library to implement its encryption technology, which provides a series of hash methods in OpenSSL, including encapsulation of hmac, cipher, decipher, signature and verification methods. Crypto official documentation: http://nodejs.org/api/crypto.html, the blog post http://blog.fens.me/nodejs-crypto/ is written in great detail. This article shows you how to use Crypto's DES algorithm to help us implement the immediately available DES algorithm.
1. Referring to the official document https://github.com/aspnet/JavaScriptServices/tree/dev/src/Microsoft.AspNetCore.NodeServices#microsoftaspnetcorenodeservices, we create a .NET Core Console application DotNETNodeApp and add the Microsoft.AspNetCore.NodeServices package reference:
Install-Package Microsoft.AspNetCore.NodeServices-Pre
2. Configuration environment. NET Core adopts dependency injection mode by default. We also need to use dependency injection in this JavaScriptService middleware. Refer to dudu's article: using dependency injection in .NET Core console programs.
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
});
/ / build the container
IServiceProvider serviceProvider = services.BuildServiceProvider ()
INodeServices nodeServices = serviceProvider.GetRequiredService ()
3. We create a Node folder in the project, and then add a cryptUtil.js with the following file content:
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)
}
}
There is a JS function that will be called in the. Net program to evaluate the result by passing in a Node-style callback function and three parameters. In NodeJS, a JS file represents a module. Module.exports means to provide the current function as an object for calling. We have two functions here that represent encryption / decryption respectively.
4. Create a Des class that encapsulates the function call of NodeJs:
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 EncryptDES (string data, string key, int iv)
{
Var result = await nodeServices.InvokeExportAsync (". / Node/cryptUtil", "encrypt", data, key, iv)
Return result
}
Public async Task DecryptDES (string data, string key, int vi)
{
Var result = await nodeServices.InvokeExportAsync (". / Node/cryptUtil", "decrypt", data,key, vi)
Return result
}
}
}
Let's take another look at InvokeExportAsync (``), which is an asynchronous method that gets a result by passing in a node.js script file (module) and three formal parameters.
Method signature: InvokeExportAsync (string moduleName, string exportName, params object [] args)
5. Let's test our packaging effect in the console.
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 ()
6. What is the performance of this use? let's take a look at the performance data with the performance test component BenchmarkDotNet, and use the method to refer to the .NET Core performance test component BenchmarkDotNet to support .NET Framework Mono: let's create a class DesBenchmark and add the Benchmark feature 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
});
/ / build the container
ServiceProvider = services.BuildServiceProvider ()
NodeServices = serviceProvider.GetRequiredService ()
}
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
}
}
}
Here are the results of the console output, and the performance is still good.
On how to use JavaScript Service in the .NET Core to achieve DES encryption algorithm to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.