Node.js 内置模块crypto加密模块(2) AES
AES:高级加密标准 ( Advanced Encryption Standard )
AES是一种对称加密算法:加密需要密钥,且加密密钥和解密密钥相同
下面是AES加密的Node实现:
"use strict";
const crypto = require("crypto");
//封装使用AES加密的方法
function aesEncrept(data, key){
//实例化一个cipher加密对象,使用aes192进行加密,key作为密钥
const cipher = crypto.createCipher("aes192",key);
//使用cipher对data进行加密,源数据类型为utf-8,输出数据类型为hex
let crypted = cipher.update(data, "utf-8", "hex");
crypted += cipher.final("hex");
return crypted;
}
//封装对应的AES解密方法
function aesDecrept(encrepted, key) {
//实例化一个decipher解密对象,使用aes192进行解密,key作为密钥
const decipher = crypto.createDecipher("aes192", key);
//使用decipher对encrepted进行解密,源数据类型为hex,输出数据类型为utf-8
let decrypted = decipher.update(encrepted, "hex", "utf-8");
decrypted += decipher.final("utf-8");
return decrypted;
}
//需要加密的数据
let data = "This is what needs to be encrepted";
//AES加密的密钥
let keyword = "This is the key";
//使用自定义的aesEncrept方法进行加密
let encrepted = aesEncrept(data, keyword);
//使用自定义的aesDecrept方法对加密数据进行解密
let decrepted = aesDecrept(encrepted, keyword);
console.log( "原始数据:" + data );
console.log( "经AES加密后:" + encrepted );
console.log( "经相应的解密后:" + decrepted );
注:
1.update方法只能对源数据的前16位进行加密,对加密数据的前32位进行解密;
2.final方法就是解决上面的缺陷,可以对剩余的数据进行加密/解密;
所以才有了下面的这个写法:
let decrypted = decipher.update(encrepted, "hex", "utf8");
decrypted += decipher.final("utf-8");
目的就是为了对全部的数据进行加密/解密
3.AES加密算法除了aes192外,还有aes-128-ecb,aes-256-cbc等
拓展阅读:AES加密算法的详细介绍与实现
来源:CSDN
作者:TimeShatter
Node.js 内置模块crypto加密模块(2) AES的更多相关文章
- Node.js 内置模块crypto加密模块(4) Diffie Hellman
Diffie-Hellman( DH ):密钥交换协议/算法 ( Diffie-Hellman Key Exchange/Agreement Algorithm ) 百科摘录: Diffie-Hell ...
- Node.js 内置模块crypto加密模块(3) HMAC
HMAC:哈希消息认证码 ( Hash-based Message Authentication Code ) HMAC是密钥相关的哈希算法 使用 HMAC 进行加密的Node实现的一种方法: &qu ...
- Node.js 内置模块crypto加密模块(5) RSA
RSA加密算法 写在前面: 了解RSA算法的原理请查看下面的文章 一文搞懂 RSA 算法 来源:简书 作者:somenzz 在使用 Node 进行 RSA 加密之前我们首先需要获取RSA公共和私有密 ...
- Node.js 内置模块crypto加密模块(1) MD5 和 SHA
MD5:消息摘要算法(Message-Digest Algorithm) SHA家族:安全散列算法( Secure Hash Algorithm ) 1.首先看一个简单的加密 "use st ...
- Node.js 内置模块crypto使用事件方法(onreadable)加密的一些问题
javaScript代码如下: 'use strict'; const crypto = require('crypto'); //实例化一个AES加密对象 const aesEncrept = cr ...
- [Node.js] Gzip + crypto in stream
We can using gzip and crypto with stream: const fs = require('fs') const zlib = require('zlib') cons ...
- Node.js 内置模块fs(文件系统)
fs模块的三个常用方法 1.fs.readFile() -- 读文件 2.fs.writeFile() -- 写文件 3.fa.stat() -- 查看文件信息 fs模块不同于其它模块的地方是它有异步 ...
- Node.js 内置模块fs的readdir方法 查看某个文件夹里面包含的文件内容
fs.readdir(path[, options], callback) 例: "use strict"; const fs = require("fs"); ...
- Node.js 内置模块Stream(流)
"流"是一种抽象的数据结构 通过使用"流"可以将一段数据分割成几段,并按顺序传输,使用"流"可以降低对系统性能的要求,减少对CPU的消耗 S ...
随机推荐
- Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...
- 《高性能Javascript》 Summary(二)
第四章.算法和流程控制 Algorithms And Flow Control 原因:代码整体结构是执行速度的决定因素之一.代码量少不一定运行速度快,代码量多不一定运行速度慢.性能损失与组织代码和具体 ...
- Swift 烧脑体操(二) - 函数的参数
前言 Swift 其实比 Objective-C 复杂很多,相对于出生于上世纪 80 年代的 Objective-C 来说,Swift 融入了大量新特性.这也使得我们学习掌握这门语言变得相对来说更加困 ...
- 51Nod 1294 修改数组 —— LIS
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1294 1294 修改数组 题目来源: HackerRank ...
- openfire build(2)
InterceptorManager PluginManager openfire 插件的中servlet 在web-custom.xml 中的配置 url 一定要小写,访问时不区别大写小 否则404 ...
- erlang的map基本使用
maps 适用于需要在运行时改变数据结构(record则不行)的场景,可以动态增加key 数据量不宜过大,具体多大没有实际数据, maps from_list 如果list表很长,则相应的耗时时间会 ...
- ivew组件的使用
iview的官网:https://www.iviewui.com/docs/guide/start 1.选择快速上手 2.安装 解压,cmd,cd进你解压后的文件,cnpm i 3.打包 npm ru ...
- python之系统编程 --线程
###########使用线程完成多任务################ from threading import Thread import time #1. 如果多个线程执行的都是同一个函数的话 ...
- 1087 All Roads Lead to Rome (30)(30 分)
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your ...
- ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...