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. myeclipse破解补丁激活失败方法

    查看本博客前,请先参考博客:http://blog.csdn.net/miss_kun/article/details/51819048 有时候,激活的时候不成功,比如我的是myeclipse2014 ...

  2. 20145239 《Java程序设计》实验三 实验报告

    详见我的parter20145224的博客:http://www.cnblogs.com/20145224kevs/p/5428892.html 感想:这次的实验看似容易,但很多点都需要注意,比如开源 ...

  3. log4net 初步使用

    自从知道了log4net之后,就一直使用的它,一直没有问题,最近由于项目变动,便将一部分的代码分离出来,然后咋UI项目中调用loghelper,便发现在本地测试一切正常,可是发布到服务器之后便不正常了 ...

  4. CodeForces - 540B School Marks —— 贪心

    题目链接:https://vjudge.net/contest/226823#problem/B Little Vova studies programming in an elite school. ...

  5. 应用程序无法启动(0*c000007b)

    2个插件就解决  一个是DX缺失工具检查那个 一个是运行库缺失检查

  6. 夏日户外风景PSD素材

    夏日户外风景PSD素材适用于向日葵素材背景图设计 地址:http://www.huiyi8.com/xiangrikui/​

  7. dataguard 下主备 online redo 与 standby redo log resize 重建

    环境说明: 本实验环境是一个节点的rac + 单节点 asm dg     database 与 grid 版本是 11.2.0.4 .提别提醒 如果是多节点集群,操作时需要特别注意 thread . ...

  8. Python3解leetcode Same Tree

    问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...

  9. linux 遍历目录+文件(优化版本)

    c++17 filesystem, regex 遍历目录 #include <stdio.h> #include <sys/types.h> #include <dire ...

  10. selenium上传文件,怎么操作

    #通过os.path.abspath()方法,打开图片的绝对路径,然后,定位上传按钮,然后,send_keys()方法中,添加这个文件路径就可以了