【问题标题】:Why is my AWS KMS decrypt not giving me back the string I just encrypted?为什么我的 AWS KMS 解密没有返回我刚刚加密的字符串?
【发布时间】:2018-11-19 16:30:33
【问题描述】:

这是我的代码:

const AWS = require('aws-sdk');
const btoa = require('btoa');

let kms = new AWS.KMS({
    accessKeyId: 'redacted',
    secretAccessKey: 'redacted',
    region: 'us-east-1'
});

let params = {
    KeyId: 'redacted',
    Plaintext: 'abcde'
 };

let encrypted = kms.encrypt(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else {
        let x = {
            CiphertextBlob: data.CiphertextBlob
        };

        kms.decrypt(x, function(err, data) {
            if (err) console.log(err, err.stack); // an error occurred
            else     console.log(btoa(data.Plaintext));           // successful response
        });
    }
 });

我只是想加密字符串abcde 然后解密它,但这不起作用。有一段时间我得到InvalidCipherException,但现在console.log(btoa(data.Plaintext)); 的输出是YWJjZGU=

我无法理解我在这里做错了什么,但我怀疑它与 base64 编码有关。我在这段代码上尝试了很多变体,但无法推断出问题所在。有人看到我在这里做错了吗?

【问题讨论】:

    标签: node.js amazon-web-services encryption base64 aws-kms


    【解决方案1】:

    我在使用 btoa 时应该使用 atob。我发誓我已经尝试过了,但谁能说。

    由于我认为atob 是 ASCII 到二进制,我无法解释我是如何使用一个应该提供二进制的函数来获取纯文本的,但是……它起作用了,所以。

    编辑:将其视为 ASCII 到二进制是一种误导。它更像是“原始内容”的“传输格式”,无论内容是什么。

    KMS.decrypt 方法想要一个二进制字符串,也就是这里的“原始内容”。 KMS.encrypt 对加密字符串进行 base64 编码以进行传输,KMS.decrypt 希望在作为参数给出之前将其编码为该格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-20
      • 2015-12-24
      • 2021-09-28
      • 2020-08-27
      • 2022-11-29
      • 1970-01-01
      • 2020-12-04
      • 2019-07-24
      相关资源
      最近更新 更多