javascript - Webcrypto AES-CBC Decrypt: Operation Error - The operation failed for an operation-specific reason - Stack Overflow

I have the following code to decrypt AES encrypted data with the Javascript Webcrypto-API, but it resul

I have the following code to decrypt AES encrypted data with the Javascript Webcrypto-API, but it results in an "OperationError" with the message "The operation failed for an operation-specific reason":

function loadHexToArrybuffer(hex)
{
	return new Uint8Array(hex.match(/[\da-f]{2}/gi).map(h => parseInt(h, 16)));
}

var iv = loadHexToArrybuffer("47b79d24e3ec47c528abdaed8f3fafde");
var rawKey = loadHexToArrybuffer("8af4d72873e4016cd73a1d5b851e9cb2");
var encryptedData = loadHexToArrybuffer("2bb7a12a7e59f9fa3c3d4ff0eb502cde3187338cc3137af785995b364fc5b3fe9c208f225c7472bb3de55de18a665863f63030d652b870c4610a70bc771e8bc584df7c3bd2ce3fc1940115e556178e740891f7cac450204a4959916ac9c9cd5aedd92cc7e74a7a581a6d47a6c29fb46eee13ffd3f70616844f8e2bb929c60ad9")

async function test()
{
	var algorithm = {name: "AES-CBC", iv: iv};
	var key = await window.crypto.subtle.importKey("raw", rawKey, algorithm, false, ["decrypt"]);

	try
	{
		var decrypted = await window.crypto.subtle.decrypt(algorithm, key, encryptedData);
	}
	catch (e)
	{
		console.log(e); // OperationError: The operation failed for an operation-specific reason
	}
}

test();

I have the following code to decrypt AES encrypted data with the Javascript Webcrypto-API, but it results in an "OperationError" with the message "The operation failed for an operation-specific reason":

function loadHexToArrybuffer(hex)
{
	return new Uint8Array(hex.match(/[\da-f]{2}/gi).map(h => parseInt(h, 16)));
}

var iv = loadHexToArrybuffer("47b79d24e3ec47c528abdaed8f3fafde");
var rawKey = loadHexToArrybuffer("8af4d72873e4016cd73a1d5b851e9cb2");
var encryptedData = loadHexToArrybuffer("2bb7a12a7e59f9fa3c3d4ff0eb502cde3187338cc3137af785995b364fc5b3fe9c208f225c7472bb3de55de18a665863f63030d652b870c4610a70bc771e8bc584df7c3bd2ce3fc1940115e556178e740891f7cac450204a4959916ac9c9cd5aedd92cc7e74a7a581a6d47a6c29fb46eee13ffd3f70616844f8e2bb929c60ad9")

async function test()
{
	var algorithm = {name: "AES-CBC", iv: iv};
	var key = await window.crypto.subtle.importKey("raw", rawKey, algorithm, false, ["decrypt"]);

	try
	{
		var decrypted = await window.crypto.subtle.decrypt(algorithm, key, encryptedData);
	}
	catch (e)
	{
		console.log(e); // OperationError: The operation failed for an operation-specific reason
	}
}

test();

Can anybody help me out here? Thanks ahead!

Share Improve this question edited Jul 4, 2018 at 12:24 Tonke asked Jul 4, 2018 at 11:24 TonkeTonke 1211 gold badge1 silver badge4 bronze badges 6
  • I can't reproduce your error (Safari 11.1.1). Code seems to be working fine here. I get CbTxsJz5GY6ID70a9Ji1UbhorbDmFEv0FtA58KBiAvCXpBa+D/2uDZv7acbgZtRvLbhAnHenYaJqHIy8WwYaipQsYjB13Ofnvkb5jRVNCdpNbIZ0zcJ8AzrCScPvZ+dF8hZbtsjAmdeBFxWGhd3TdAfJVvo7gn94ndir8A== in the decrypted buffer, in base64. – Touffy Commented Jul 4, 2018 at 12:30
  • 3 Note that for security reasons, you'll always receive an unhelpful "operationError" no matter what goes wrong with the Web Crypto API. – Touffy Commented Jul 4, 2018 at 12:32
  • Ah, that makes sens! Thanks for the feedback, I get this error here on both the latest Firefox and Chromium. The result might be correct, since I replaced the real data with random generated one. – Tonke Commented Jul 4, 2018 at 12:55
  • Weird. I do reproduce the error in Chrome. I don't see why. – Touffy Commented Jul 4, 2018 at 14:02
  • 2 Is there a debug mode, to get a more descriptive error message? – Tonke Commented Jul 4, 2018 at 15:48
 |  Show 1 more ment

1 Answer 1

Reset to default 0

You do not state what browser and version you are using, each version supports a different set of algorithms.

If you want a quick test on what each supports see: https://peculiarventures.github.io/pv-webcrypto-tests/

With that said, my first thought is I would not use CBC unless interoperability with an existing system that only uses CBC is necessary. You should look at AES-GCM instead, it is an authenticated mode of encryption which protects from certain attacks that plain CBC will not and is less error prone on usage.

Here is an encrypt example of GCM:

window.crypto.subtle.encrypt(
    {
        name: "AES-GCM",

        //Don't re-use initialization vectors!
        //Always generate a new iv every time your encrypt!
        //Remended to use 12 bytes length
        iv: window.crypto.getRandomValues(new Uint8Array(12)),

        //Additional authentication data (optional)
        additionalData: ArrayBuffer,

        //Tag length (optional)
        tagLength: 128, //can be 32, 64, 96, 104, 112, 120 or 128 (default)
    },
    key, //from generateKey or importKey above
    data //ArrayBuffer of data you want to encrypt
)
.then(function(encrypted){
    //returns an ArrayBuffer containing the encrypted data
    console.log(new Uint8Array(encrypted));
})
.catch(function(err){
    console.error(err);
});

If you need to use CBC here are some good examples on usage: https://github./diafygi/webcrypto-examples#aes-cbc

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745662020a4638894.html

相关推荐

  • HLS最全知识库

    HLS最全知识库副标题-FPGA高层次综合HLS(二)-Vitis HLS知识库高层次综合(High-level Synthesis)简称HLS,指的是将高层次语言描述的逻辑结构,自动转换成低抽象级语言描述的电路模型的过程。对于AMD Xi

    1小时前
    00
  • 国产之光!!让你的Docker管理更优雅!

    大家好,我是热爱开源的了不起!我们都知道,Docker是个好东西,能帮我们把应用打包成容器,方便部署和管理。但问题来了,Docker的命令行操作对新手来说有点复杂,一不小心就容易出错。而且,有时候我们只是想简单地管理一下容器,却得记住一堆命

    1小时前
    00
  • 非nvidia卡torchvision报错修复: operator torchvision::nms does not exist

    在Ascend 910b上安装vllm, 会自动把torchaudio和torchvision安装上去.安装前代码语言:shell复制pip list | grep torchtorch

    1小时前
    00
  • AlignRAG:浙江大学提出的可泛化推理对齐框架,助力 RAG 系统解决推理失配问题

    近年来,检索增强生成(Retrieval-Augmented Generation, RAG)成为知识驱动文本生成的核心范式。然而,现有的 RAG 系统在推理过程中常常出现“推理失配”问题,即模型的推理路径与检索到的证据不一致,导致生成内容

    57分钟前
    00
  • 开源在线考试系统

    看到调问已经开始扩展在线考试的场景,试了一下,发现在线考试的基本能力都已经支持了。主要是考试中的各种计分功能,包括对每道题的选项设置分值计算、考试时间限制等,和官方了解了一下,考试中的其他各项能力也在逐步完善,有需求可以随时

    46分钟前
    00
  • Go 语言 Mock 实践

    Mock 是软件测试中的一项关键技术,尤其在单元测试领域,可谓是“顶梁柱”般的存在,几乎不可或缺。它通过模拟真实对象的行为,使我们能在不依赖外部系统的情况下,专注测试代码的核心逻辑。对于测试开发、自动化测试,乃至性能测试中的某些场景,合理使

    42分钟前
    00
  • 如何快速判断 Flutter 库是否需要适配鸿蒙?纯 Dart 库无需适配!

    在鸿蒙开发中,选择合适的 Flutter 库至关重要。纯 Dart 库因其跨平台特性,无需适配即可直接使用。但对于新手来说,如何判断一个库是否为纯 Dart 库呢?本文将为你提供清晰的判断方法和实用技巧。一、检查 pubspec.yaml

    41分钟前
    00
  • 深度学习在DOM解析中的应用:自动识别页面关键内容区块

    爬虫代理摘要本文介绍了如何在爬取东方财富吧()财经新闻时,利用深度学习模型对 DOM 树中的内容区块进行自动识别和过滤,并将新闻标题、时间、正文等关键信息分类存储。文章聚焦爬虫整体性能瓶颈,通过指标对比、优化策略、压测数据及改进结果,展示了

    36分钟前
    10
  • 拥抱国产化:转转APP的鸿蒙NEXT端开发尝鲜之旅

    本文由转转技术团队赵卫兵分享,原题“鸿蒙新篇章:转转 APP 的 HarmonyOS Next 开发之旅”,下文进行了排版优化和内容修订。1、引言2023 年在华为开发者大会(HDC.Together)上,除了面向消费者的 HarmonyO

    25分钟前
    00
  • maxwell遇到的一则问题

    结论和原因maxwell的元数据库里面没有存储全部的schema数据(就是少数据了),导致相关表的DDL校验失败。PS:我这里maxwell的作用只是采集库表修改情况的统计粗粒度指标,因为之前maxwell在运行报错的时候,直接修改了pos

    23分钟前
    00
  • 实现一个 MySQL 配置对比脚本需要考虑哪些细节?

    作者:李彬,爱可生 DBA 团队成员,负责项目日常问题处理及公司平台问题排查。爱好有亿点点多,吉他、旅行、打游戏爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。本文约 1500 字,预计阅读需要 3 分钟。引言想

    20分钟前
    00
  • 人工智能适合什么人学

    一、引言:人工智能浪潮下的新机遇在当今科技飞速发展的时代,人工智能(AI)无疑是最为耀眼的技术明星之一。从智能语音助手到自动驾驶汽车,从医疗诊断辅助到金融风险预测,人工智能正以前所未有的速度改变着我们的生活和工作方式。随着全球领先的终身学习

    15分钟前
    00
  • 子网掩码是怎么“掩”的?用积木教你彻底搞懂!

    子网掩码是怎么“掩”的?用积木教你彻底搞懂!前言肝文不易,点个免费的赞和关注,有错误的地方请指出,看个人主页有惊喜。作者:神的孩子都在歌唱你是不是也曾被“子网掩码”这个术语搞得晕头转向?明明是学网络的第一步,却像是打开了数学世界的大门:2

    14分钟前
    00
  • 1.54G 雨晨 26100.3775 Windows 11 IoT 企业版 LTSC 24H2 极速版

    精简AERO外主题并增加一套壁纸主题(默认启用)误杀导致功能界面空白、因WMIC被默认移除系统可能会多次重启。 拒止连接 www.5909 拒止连接 www.mnpc 拒止连接 quark 拒止

    14分钟前
    00
  • Java&Activiti7实战:轻松构建你的第一个工作流

    本文已收录在Github,关注我,紧跟本系列专栏文章,咱们下篇再续!

    9分钟前
    00
  • 用Xshell8配置密钥登陆

    1.首先在服务端查看root.sshauthorized_keys是否存在,这是存储公钥的文件,若不存在需新建此文件 2. 打开Xshell8,选择"新建",选择"新建用户密钥生成向导" 给用户

    8分钟前
    00
  • 人工智能应用领域有哪些

    人工智能应用领域有哪些一、引言随着科技的飞速发展,人工智能(AI)已经逐渐渗透到我们生活的方方面面,成为推动社会进步的重要力量。从医疗健康到金融服务,从教育学习到智能制造,人工智能以其独特的技术优势,为各行各业带来了前所未有的变革。本文旨在

    7分钟前
    00
  • 在Windows上使用MetaMCP的完整指南

    在当今AI助手工具快速发展的时代,如何有效管理各种MCP(Model Control Protocol)服务成为了一个挑战。MetaMCP应运而生,它是

    3分钟前
    00
  • win11家庭版改为专业版

    找到“我的电脑”--“设置”--“系统”--“激活”--“更改密钥” 输入密钥“G49HN-9YQCT-684C3-R7T3F-3DBQB 即可成功。

    1分钟前
    00
  • VoidZero 的野心,开发者的福音!

    前言昨天分享了尤雨溪公司 VoidZero 最新的产品 TSDown,我相信肯定有同学和我一样好奇,尤雨溪为什么要推出这么多工具,来增加大家的学习压力!今天我们从整体上分析下,这些产品的功能和目的!正文VoidZero 是尤雨溪于 2020

    1分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信