node.js - 如何使用 AWS KMS 加密和解密字符串?

标签 node.js amazon-web-services amazon-kms

我正在尝试使用 AWS KMS 加密和解密一个简单的字符串,
我正在使用 AWS Javascript SDK 来执行此操作,
我能够对字符串进行加密和解密,因为没有错误,
但是 KMS 解密方法的输出不会产生我试图加密的原始字符串。

这是我的工作代码-

var AWS = require('aws-sdk');
const util = require('util');

AWS.config.update({region:'us-east-1'});
var kms = new AWS.KMS({apiVersion: '2014-11-01'});

let test = async () => {

    try {
        let data = `test`;

        var encryptionParams = {
            KeyId: "someKMSKeyId",
            Plaintext: data
        };

        let kmsEncrypt = util.promisify(kms.encrypt).bind(kms);
        let encryptedData = await kmsEncrypt(encryptionParams);

        //encryptedData contained 2 parts, CiphertextBlob and KeyId
        console.log('encryptedData => \n', encryptedData);
        console.log('\nencryptedData.CiphertextBlob => \n', encryptedData.CiphertextBlob);
        console.log('\nencryptedData.KeyId => \n', encryptedData.KeyId);

        var decryptionParams = {
            CiphertextBlob : encryptedData.CiphertextBlob
        };

        let kmsDecrypt = util.promisify(kms.decrypt).bind(kms);
        let decryptedData = await kmsDecrypt(decryptionParams);

        //ndecryptedData contained 2 parts, Plaintext and KeyId
        console.log('\ndecryptedData => \n', decryptedData);
        console.log('\ndecryptedData.Plaintext => \n', decryptedData.Plaintext);
        console.log('\ndecryptedData.KeyId => \n', decryptedData.KeyId);
    } catch (error) {
        console.log('\nerror => \n',error);
    }
}

test();

我期待 decryptedData.Plaintext 的输出进行测试
但输出类似于 - <Buffer 74 65 73 74> ,
我在这里做错了什么?

引用-
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/KMS.html#endpoint-property

最佳答案

感谢 kdgregory 的提示,我能够通过使用 base64 将纯文本解码为字符串来解决此问题,
以下是使用 AWS KMS 进行加密和解密的最终工作代码 -

var AWS = require('aws-sdk');
const util = require('util');

AWS.config.update({region:'us-east-1'});
var kms = new AWS.KMS({apiVersion: '2014-11-01'});

let test = async () => {

    try {
        let data = 'test';

        var encryptionParams = {
            KeyId: "kmsKeyId",
            Plaintext: data
        };

        let kmsEncrypt = util.promisify(kms.encrypt).bind(kms);
        let encryptedData = await kmsEncrypt(encryptionParams);

        //encryptedData contained 2 parts, CiphertextBlob and KeyId
        console.log('encryptedData => \n', encryptedData);
        console.log('\nencryptedData.CiphertextBlob => \n', encryptedData.CiphertextBlob);
        console.log('\nencryptedData.KeyId => \n', encryptedData.KeyId);

        let buff = Buffer.from(encryptedData.CiphertextBlob);
        let encryptedBase64data = buff.toString('base64');
        console.log("\nencryptedBase64data => \n", encryptedBase64data);

        var decryptionParams = {
            CiphertextBlob : encryptedData.CiphertextBlob
        };

        let kmsDecrypt = util.promisify(kms.decrypt).bind(kms);
        let decryptedData = await kmsDecrypt(decryptionParams);

        //ndecryptedData contained 2 parts, Plaintext and KeyId
        console.log('\ndecryptedData => \n', decryptedData);
        console.log('\ndecryptedData.Plaintext => \n', decryptedData.Plaintext);
        console.log('\ndecryptedData.KeyId => \n', decryptedData.KeyId);

        let buff2 = Buffer.from(decryptedData.Plaintext, 'base64');  
        let originalText = buff2.toString('ascii');
        console.log('\noriginalText => \n', originalText);
    } catch (error) {
        console.log('\nerror => \n',error);
    }
}

test();

关于node.js - 如何使用 AWS KMS 加密和解密字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56890832/

相关文章:

mysql - 连接 Erlang 和 mysql odbc 时出错

amazon-web-services - AWS 拥有的 CMK 与 AWS 管理的 CMK

node.js - npm 测试 CircleCI 上的意外行为

javascript - Node.js 服务器压力测试

node.js - 有没有办法让 webpack 不显示它正在编译的所有 block ?

ios - AWS CLI SNS 向我的 iOS 应用程序推送通知有效,但我如何播放不同的声音或更改角标(Badge)编号?

http - 只有 AWS ELB 支持在堆栈中支持 http/2 是否足够

node.js - 无法重新启动 redis-server.service : Unit redis-server. 找不到服务

amazon-s3 - 使用pull方法的跨账户codepipeline

node.js - 在 NodeJs 中使用 AWS KMS 解密文本