当前位置: 首页 > news >正文

javascript deriveKey和deriveBits()由主密钥派生出新的密钥进行加密

deriveKey 方法的完整示例,演示如何使用 HMAC 作为密钥派生函数(KDF)来从一个给定的秘密(如密码)派生出一个新的 AES 加密密钥。

//创建一个函数来生成随机盐function getRandomSalt(length){let array=new Uint8Array(length);window.crypto.getRandomValues(array);return array;}//生成随机数字function randomFloat(){const fooArray=new Uint32Array(1);const maxUint32=0xFFFFFFFF;return crypto.getRandomValues(fooArray)[0];}//生成随机密码,导入密钥当中function getKeyMaterial(){//const password=window.prompt("请输入你的密码");//就是为加密时用密码//这里不用输入密码,我们采用随机生成的数字,这样可以增加破解难度,提升安全性const password=randomFloat();const enc=new TextEncoder();return window.crypto.subtle.importKey("raw",enc.encode(password),"PBKDF2",false,["deriveBits","deriveKey"]);}
//创建一个函数来派生密钥async function deriveAeskey(salt,info,keyLength){//生成已有密钥//不能直接用new Textcoder().encoder(password)这样来生成密码,必须用这个函数的importKey导入密钥才行const passwordBuffer=await getKeyMaterial();const derivedKey=await crypto.subtle.deriveKey({name:'PBKDF2',salt:salt,iterations:10000,hash:'SHA-256'},passwordBuffer,{name:'AES-GCM',length:keyLength},true,["encrypt","decrypt"]);return derivedKey;}//使用派生的密钥进行加密async function encryptData(derivedKey,dataToEncrypt){const encoder=new TextEncoder();const dataBuffer=encoder.encode(dataToEncrypt);const iv=window.crypto.getRandomValues(new Uint8Array(12));const encryption=await crypto.subtle.encrypt({name:'AES-GCM',iv:iv},derivedKey,dataBuffer);return {ciphertext:encryption,iv:Array.from(iv)}}//使用派生的密钥进行解密async function decryptData(derivedKey,ciphertext,iv){const decrypted=await crypto.subtle.decrypt({name:'AES-GCM',iv:new Uint8Array(iv)},derivedKey,ciphertext);const decoder=new TextDecoder();return decoder.decode(decrypted);}//最后把他们综合到一起,进行加密和解密,以及做加密后内容的演式(async function(){const salt=getRandomSalt(16);const info=new Uint8Array();const keyLength=256;try{//派生的密钥const derivedKey=await deriveAeskey(salt,info,keyLength);//需要加密的数据const dataToEncrypt="this is data";const {ciphertext,iv}=await encryptData(derivedKey,dataToEncrypt);//这里是演式你加密后的数据样式console.log(String.fromCharCode(...new Uint8Array(ciphertext)));//解密数据const decryptedData=await decryptData(derivedKey,ciphertext,iv);console.log('解密数据:',decryptedData);}catch(error){console.log('加解密时发生错误',error);}})();
http://www.lryc.cn/news/409130.html

相关文章:

  • 基于微信小程序的自习室选座系统/基于Java的自习室选座系统/自习室管理系统的设计与实现
  • echarts所遇到的问题,个人记录
  • Skyeye云智能制造企业版源代码全部开放
  • Springboot 整合Elasticsearch
  • WeNet环境配置与aishell模型训练
  • 【C++的剃刀】我不允许你还不会AVL树
  • React搭建Vite项目及各种项目配置
  • Linux Vim教程:多文件编辑与窗口管理
  • C语言进阶 11.结构体
  • Vue--解决error:0308010C:digital envelope routines::unsupported
  • go-kratos 学习笔记(6) 数据库gorm使用
  • 记录:vite打包报错 error during build: Error: Parse error @:1:1
  • Python 消费Kafka手动提交 批量存入Elasticsearch
  • oracle 基础知识表的主键
  • opencascade AIS_MouseGesture AIS_MultipleConnectedInteractive源码学习
  • Unity Apple Vision Pro 开发:如何把 PolySpatial 和 Play To Device 的版本从 1.2.3 升级为 1.3.1
  • 大数据时代,区块链是如何助力数据开放共享的?
  • 睿抗2024省赛----RC-u4 章鱼图的判断
  • py2exe,一个神奇的 Python 库
  • 博途PLC网络连接不上
  • 哪个邮箱最安全最好用啊
  • 企业微信开发智能升级:AIGC技术赋能,打造高效沟通平台
  • Apache Doris + Paimon 快速搭建指南|Lakehouse 使用手册(二)
  • Inno setup pascal编码下如何美化安装界面支持带边框,圆角,透明阴影窗口
  • SQL语句(以MySQL为例)——单表、多表查询
  • C++第二十八弹---进一步理解模板:特化和分离编译
  • 正则表达式的独占模式,懒惰模式等有那些区别
  • 【INTEL(ALTERA)】Quartus® Prime Pro Edition 软件 v24.2 中,哪些 Agilex™ 5 IP 功能的硬件验证有限?
  • Lua编程
  • 2019数字经济公测大赛-VMware逃逸