如何使用SJCL解密AES [英] How to decrypt AES with SJCL

查看:362
本文介绍了如何使用SJCL解密AES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用斯坦福Javascript加密库(SJCL)通过给定的密码和密钥解密AES,但是我无法传递密钥:

I want to decrypt AES by given cipher and key with the Stanford Javascript Crypto Library (SJCL), but i can't pass the key:

var key = 'key';
var cipher = 'abjslö';
var aes = new sjcl.cipher.aes(key);

var plaintext = aes.decrypt(cipher);
alert(plaintext);

此操作无效。参考文档,密钥必须是 4、6或8个单词的数组。

This dosen't work. Referred to the documentation, the key has to be "an array of 4, 6 or 8 words".

这怎么办?

推荐答案

加密


  1. 创建一个随机单词数组,它将
    用作我们的IV(初始化向量)。

  1. Create an array of random words which will serve as our IV(initialisation vector).

然后您需要使用随机密钥创建位数组(大小取决于加密级别
和类型)

Then you need to create a bit array using a random key(size depends upon level of encryption and the type)

然后使用键数组创建密码。

Then you create a cypher using the key array.

最后使用密码和IV。 (您也可以
也添加元数据以检查其真实性)

And finally encode your data using the cypher and the IV. (you can also add meta data to check the authenticity)

现在只需连接IV和加密的位数组,最后连接
将其转换为base64字符串并通过。

Now just concat the IV and the encrypted bit array and finally convert it to a base64 string and pass.

请注意,如果没有AES-GCM,则无法解密IV。

解密


  1. 在解密加密字符串中的IV时。

  1. While decrypting slice the IV from the encryted string.

现在,使用密钥创建一个密码,并使用该密码来解密字符串。

Now using the key create a cypher and use that to decrypt the string.

您可以找到完整的代码此处。

这篇关于如何使用SJCL解密AES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆