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

福建外贸网站建设中华南大街网站建设

福建外贸网站建设,中华南大街网站建设,建筑装饰网站模板,wordpress首页文章列表只显示摘要deriveKey 方法的完整示例,演示如何使用 HMAC 作为密钥派生函数(KDF)来从一个给定的秘密(如密码)派生出一个新的 AES 加密密钥。 //创建一个函数来生成随机盐function getRandomSalt(length){let arraynew Uint8Array…

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.yayakq.cn/news/386116/

相关文章:

  • 网站开发课设心得体会wordpress机械模板下载
  • wordpress 整套模板下载seo网站三要素怎么做
  • 河南手机网站建设多少钱腾讯云 wordpress建站
  • 网站维护费用包括哪些做网站有什么平台
  • 做 爱 网站小视频在线观看做的好的网站着陆页
  • 网站建设公司企业网站管理系统seo网站优化工具大全
  • 国外哪个网站做c 挣钱南宁云尚网络有限公司
  • 搜索网站 模板网站底部模板代码
  • 抚顺网站开发自己买服务器建设网站
  • 深圳住房城乡建设局网站首页建设学院网站的通知
  • 域名代备案网站网站建设中涉及到的编程语言
  • 网站页面改版降权微信做一个小程序需要多少钱
  • 外贸新品开发网站上海网站推广广告
  • 如何修改网站后台地址中信建设有限公司简介
  • app优化网站微信 微网站开发
  • ui设计师证seo 论坛
  • 汕头网站推广公司广东网
  • 事业部网站建设方案禾天姿网站建设
  • 装饰设计网站模板推广方案有哪些
  • dedecms 网站 经常无法连接天津建设网站安全员考试查询
  • 亿联时代网站建设软件商店怎么下载
  • 官方网站开发合同三星单片机开发网站
  • 驾校网站开发计划书wordpress百度秀
  • 做图片的网站百度保障中心人工电话
  • 做私活一个网站大概多少钱网站系统建设申请报告
  • 网站建设课程设计文献综述建设银行不良资产处置网站
  • 国外的购物网站有哪些php婚庆网站源码
  • 江门网站建设企业开发一款app需要多少人
  • 电子商务网站建设与管理课后题答案6做平面什么网站好用
  • 重庆招聘一般上什么网站有没有什么做高数的网站