使用 Web Crypto API 的 AES 加密和解密

问题描述

我在 Github 中找到了两个使用 Web Crypto API 加密和解密数据(字符串)的代码,问题是我不是密码学专家,所以我想知道您对这些代码的安全性的看法?它们是否足够安全以依赖它们获取易受攻击的数据?

加密:

async function encryptMessage(key) {
    let encoded = getMessageEncoding();
    // The iv must never be reused with a given key.
    iv = window.crypto.getRandomValues(new Uint8Array(12));
    ciphertext = await window.crypto.subtle.encrypt(
        {
            name: "AES-GCM",iv: iv
        },key,encoded
    )
    .then(function (encrypted) {
        console.log(new Uint8Array(encrypted));
    })
    .catch(function (err) {
        console.error(err);
    });
}

解密:

async function decryptMessage(key) {
    let encoded = getMessageEncoding();
    let decrypted = await window.crypto.subtle.decrypt(
        {
            name: "AES-GCM",ciphertext
    )
    .then(function (decrypted) {
        console.log(new Uint8Array(encrypted));
    })
    .catch(function (err) {
        console.error(err);
    });
}

如果您可以对代码进行任何改进以提升其安全性,我将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)