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-ecbaes-256-cbc

拓展阅读:AES加密算法的详细介绍与实现

        来源:CSDN

        作者:TimeShatter

Node.js 内置模块crypto加密模块(2) AES的更多相关文章

  1. Node.js 内置模块crypto加密模块(4) Diffie Hellman

    Diffie-Hellman( DH ):密钥交换协议/算法 ( Diffie-Hellman Key Exchange/Agreement Algorithm ) 百科摘录: Diffie-Hell ...

  2. Node.js 内置模块crypto加密模块(3) HMAC

    HMAC:哈希消息认证码 ( Hash-based Message Authentication Code ) HMAC是密钥相关的哈希算法 使用 HMAC 进行加密的Node实现的一种方法: &qu ...

  3. Node.js 内置模块crypto加密模块(5) RSA

    RSA加密算法 写在前面: 了解RSA算法的原理请查看下面的文章 一文搞懂 RSA 算法 来源:简书  作者:somenzz 在使用 Node 进行 RSA 加密之前我们首先需要获取RSA公共和私有密 ...

  4. Node.js 内置模块crypto加密模块(1) MD5 和 SHA

    MD5:消息摘要算法(Message-Digest Algorithm) SHA家族:安全散列算法( Secure Hash Algorithm ) 1.首先看一个简单的加密 "use st ...

  5. Node.js 内置模块crypto使用事件方法(onreadable)加密的一些问题

    javaScript代码如下: 'use strict'; const crypto = require('crypto'); //实例化一个AES加密对象 const aesEncrept = cr ...

  6. [Node.js] Gzip + crypto in stream

    We can using gzip and crypto with stream: const fs = require('fs') const zlib = require('zlib') cons ...

  7. Node.js 内置模块fs(文件系统)

    fs模块的三个常用方法 1.fs.readFile() -- 读文件 2.fs.writeFile() -- 写文件 3.fa.stat() -- 查看文件信息 fs模块不同于其它模块的地方是它有异步 ...

  8. Node.js 内置模块fs的readdir方法 查看某个文件夹里面包含的文件内容

    fs.readdir(path[, options], callback) 例: "use strict"; const fs = require("fs"); ...

  9. Node.js 内置模块Stream(流)

    "流"是一种抽象的数据结构 通过使用"流"可以将一段数据分割成几段,并按顺序传输,使用"流"可以降低对系统性能的要求,减少对CPU的消耗 S ...

随机推荐

  1. Git如何强制拉取一个远程分支到本地分支(转载)

    有时候,我们在使用git pull指令想把一个远程分支拉取到本地分支的时候,老是会拉取失败,这一般是因为某种原因,本地分支和远程分支的内容差异无法被git成功识别出来,所以git pull指令什么都不 ...

  2. HDU4825 Xor Sum —— Trie树

    题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  3. html5手机网站需要加的那些meta标签,手机网站自适应

    的html5相关meta和标签    a.<!-- 强制让文档与设备的宽度保持1:1 -->    <meta name="viewport" content=& ...

  4. matlab unique()函数

    a = [30, 3,6; 19, 20, 20; 30,3,  6;] b=unique(a,'rows') 返回结果是: b = [19,    20 ,20; 30, 3, 6] b=uniqu ...

  5. css中字体大小在不同浏览器兼容性问题

    css中使用font-size设定字体大小,不同浏览器的字体height一样,但是width不同,比如在火狐和谷歌中,font-size:20px,字体的高度变为20px,但是谷歌的字体宽度比火狐长 ...

  6. css设置文件编码

    在外部css文件的顶部,写入下面代码: @charset "UTF-8";

  7. T57

    “期待使我产生了介于幸福与恐惧之间的激动”The anticipation produced in me a sensation somewhat between bliss and fear他猛一下 ...

  8. BZOJ1926:[SDOI2010]粟粟的书架

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...

  9. python optparse命令解析模块

    来源:http://www.cnblogs.com/pping/p/3989098.html?utm_source=tuicool&utm_medium=referral 来源:http:// ...

  10. Html.Partial 和 Html.RenderPartial 、Html.Action 和 Html.RenderAction区别

    Html.Partial 和 Html.RenderPartial不需要为视图指定路径和文件扩展名.因为运行时定位部分视图与定位正常视力使用的逻辑相同.RenderPartial不是返回字符串,而是直 ...