使用Nodejs和AWS KMS对s3中的文件进行加密和解密

时间:2022-01-20 23:03:42

I am using AWS KMS to encrypt file to s3 bucket. I'm currently doing this using the AWS console, but I'd like to do this using Nodejs.

我正在使用AWS KMS将文件加密到s3 bucket。我目前正在使用AWS控制台进行此操作,但我希望使用Nodejs进行此操作。

I just checked some of the things but I am not getting any clear idea about the encryption and decryption using nodejs for KMS.

我刚刚检查了一些东西,但是我不清楚用nodejs对KMS进行加密和解密。

1 个解决方案

#1


2  

You need to take a look at the AWS SDK for javascript. From the examples:

您需要查看AWS javascript SDK。的例子:

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

var kms = new AWS.KMS({apiVersion: '2014-11-01'});

var params = {
  KeyId: "1234abcd-12ab-34cd-56ef-1234567890ab", // The identifier of the CMK to use for encryption. You can use the key ID or Amazon Resource Name (ARN) of the CMK, or the name or ARN of an alias that refers to the CMK.
  Plaintext: <Binary String>// The data to encrypt.
 };

kms.encrypt(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    CiphertextBlob: <Binary String>, // The encrypted data (ciphertext).
    KeyId: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"// The ARN of the CMK that was used to encrypt the data.
   }
   */
});

var params = {
  CiphertextBlob: <Binary String>// The encrypted data (ciphertext).
 };

kms.decrypt(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    KeyId: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", // The Amazon Resource Name (ARN) of the CMK that was used to decrypt the data.
    Plaintext: <Binary String>// The decrypted (plaintext) data.
   }
   */
});

Here is the link for the aws-sdk package on NPM. Here is the link for the main AWS SDK for Javascript documentation page.

这是NPM的aws-sdk包的链接。下面是用于Javascript文档页面的AWS SDK的链接。

Hope this helps!

希望这可以帮助!

#1


2  

You need to take a look at the AWS SDK for javascript. From the examples:

您需要查看AWS javascript SDK。的例子:

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

var kms = new AWS.KMS({apiVersion: '2014-11-01'});

var params = {
  KeyId: "1234abcd-12ab-34cd-56ef-1234567890ab", // The identifier of the CMK to use for encryption. You can use the key ID or Amazon Resource Name (ARN) of the CMK, or the name or ARN of an alias that refers to the CMK.
  Plaintext: <Binary String>// The data to encrypt.
 };

kms.encrypt(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    CiphertextBlob: <Binary String>, // The encrypted data (ciphertext).
    KeyId: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"// The ARN of the CMK that was used to encrypt the data.
   }
   */
});

var params = {
  CiphertextBlob: <Binary String>// The encrypted data (ciphertext).
 };

kms.decrypt(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    KeyId: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", // The Amazon Resource Name (ARN) of the CMK that was used to decrypt the data.
    Plaintext: <Binary String>// The decrypted (plaintext) data.
   }
   */
});

Here is the link for the aws-sdk package on NPM. Here is the link for the main AWS SDK for Javascript documentation page.

这是NPM的aws-sdk包的链接。下面是用于Javascript文档页面的AWS SDK的链接。

Hope this helps!

希望这可以帮助!