gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:45 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com