js加密,c#解密

news/2024/10/18 19:25:35/

目录

    • js加密
    • c#解密
    • c#加密
    • js解密

js加密

javascript">    async function encryptText(plaintext) {// 将文本转换为ArrayBufferconst textEncoder = new TextEncoder();const dataBuffer = textEncoder.encode(plaintext);// 密钥和初始化向量,应该是一个安全的随机值const key = '123456789012345678901234567890ab'; // 32字节密钥const iv = '1234567890123456'; // 16字节初始化向量// 将密钥和初始化向量转换为ArrayBufferconst keyBuffer = new Uint8Array(key.split('').map(c => c.charCodeAt(0)));const ivBuffer = new Uint8Array(iv.split('').map(c => c.charCodeAt(0)));// 使用AES-CBC算法和PKCS7填充const cryptoKey = await window.crypto.subtle.importKey('raw',keyBuffer,{ name: 'AES-CBC', length: 256 },false,['encrypt']);// 加密数据const encrypted = await window.crypto.subtle.encrypt({ name: 'AES-CBC', iv: ivBuffer },cryptoKey,dataBuffer);// 将加密后的数据转换为Base64字符串const encryptedArray = new Uint8Array(encrypted);return btoa(String.fromCharCode(...encryptedArray));}

c#解密

        public string DecryptAes( string encryptedData, string key,string iv){// 转换为字节数组byte[] encryptedBytes = Convert.FromBase64String(encryptedData);// 创建解密器using (Aes aes = Aes.Create()){aes.Key = Encoding.UTF8.GetBytes(key);aes.IV = Encoding.UTF8.GetBytes(iv);aes.Mode = CipherMode.CBC;aes.Padding = PaddingMode.PKCS7;// 创建解密器ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);// 解密byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);// 输出解密后的数据return Encoding.UTF8.GetString(decryptedBytes);}}

c#加密

        public string EncryptAes(string content, string key, string iv){// 转换为字节数组byte[] contentBytes = Encoding.UTF8.GetBytes(content);// 创建解密器using (Aes aes = Aes.Create()){aes.Key = Encoding.UTF8.GetBytes(key);aes.IV = Encoding.UTF8.GetBytes(iv);aes.Mode = CipherMode.CBC;aes.Padding = PaddingMode.PKCS7;// 创建解密器ICryptoTransform decryptor = aes.CreateEncryptor(aes.Key, aes.IV);// 解密byte[] encryptedBytes = decryptor.TransformFinalBlock(contentBytes, 0, contentBytes.Length);// 输出解密后的数据return Convert.ToBase64String(encryptedBytes);}}

js解密

javascript">  async function decryptText(ciphertext) {// 密钥和初始化向量,应该是一个安全的随机值const key = '123456789012345678901234567890ab'; // 32字节密钥const iv = '1234567890123456'; // 16字节初始化向量// 将密钥和初始化向量转换为ArrayBufferconst keyBuffer = new Uint8Array(key.split('').map(c => c.charCodeAt(0)));const ivBuffer = new Uint8Array(iv.split('').map(c => c.charCodeAt(0)));// 将Base64字符串转换为ArrayBufferconst encryptedArray = new Uint8Array(atob(ciphertext).split('').map(c => c.charCodeAt(0)));// 导入密钥const cryptoKey = await window.crypto.subtle.importKey('raw',keyBuffer,{ name: 'AES-CBC', length: 256 },false,['decrypt']);// 解密数据const decrypted = await window.crypto.subtle.decrypt({ name: 'AES-CBC', iv: ivBuffer },cryptoKey,encryptedArray);// 将解密后的数据转换为文本const textDecoder = new TextDecoder();return textDecoder.decode(decrypted);}

http://www.ppmy.cn/news/1448991.html

相关文章

基于Springboot的交流互动系统

基于SpringbootVue的交流互动系统的设计与实现 开发语言:Java数据库:MySQL技术:SpringbootMybatis工具:IDEA、Maven、Navicat 系统展示 用户登录 首页 帖子信息 聚会信息 后台登录 后台管理首页 用户管理 帖子分类管理 帖子信息…

Getting started - 英文版 - English Version

🤗 ApiHug {Postman|Swagger|Api...} 快↑ 准√ 省↓ GitHub - apihug/apihug.com: All abou the Apihug apihug.com: 有爱,有温度,有质量,有信任ApiHug - API design Copilot - IntelliJ IDEs Plugin | Marketplace This pa…

2024人工智能/机器学习/machine learning/CV/NLP重点公式汇总(算法面试考试论文)

### CV # Diffusion Model 扩散模型http://deepnlp.org/equation/diffusion-model-forward-processhttp://deepnlp.org/equation/diffusion-model-forward-process-reparameterizationhttp://deepnlp.org/equation/diffusion-model-reverse-processhttp://deepnlp.org/equation…

安卓手机APP开发__媒体开发部分__常见问题答疑解惑

安卓手机APP开发__媒体开发部分__常见问题答疑解惑 目录 1.修复"Cleartext HTTP traffic not permitted"错误 2.修复"SSLHandshakeException", "CertPathValidatorException" 和 "ERR_CERT_AUTHORITY_INVALID" 错误 3.为什么一些媒…

中文输入法导致的高频事件

场景: input.addEventListener(input, (e) > {console.log(e.target.value) }); 当给一个输入框绑定了 input 事件,输入法切换到中文正在拼写的过程中也会触发 input 事件。 解决办法: 在中文拼写开始和结束的时候分别会触发 composit…

1 .算法的复杂度(超全)

目录 1.算法效率 1.1 如何衡量一个算法的好坏 1.2 算法的复杂度 1.3 复杂度在校招中的考察 2.时间复杂度 2.1 时间复杂度的概念 2.2 大O的渐进表示法 000 2.3常见时间复杂度计算举例 3.空间复杂度 4. 常见复杂度对比 5. 复杂度的oj练习 ~省流: ~数据结…

【Spring AI】02. 嵌入向量 API

文章目录 嵌入向量 APIAPI 概述EmbeddingClientEmbeddingRequestEmbeddingResponseEmbedding 可用实现 嵌入向量 API EmbeddingClient 接口旨在与人工智能和机器学习中的嵌入向量模型进行直接集成。其主要功能是将文本转换为数字向量,通常称为嵌入向量。这些嵌入向…

【VueUse库各模块函数使用方法系列---Browser模块下篇】

vueUse库是一个专门为Vue打造的工具库,提供了丰富的功能,包括监听页面元素的各种行为以及调用浏览器提供的各种能力等。其中的Browser模块包含了一些实用的函数,以下是这些函数的简介和使用方法: vueUse库Browser模块各函数简介及使用方法 vueUseBrowser函数1. `useScript…