Does is matter from which region I'm calling AWS KMS encrypt decrypt API?

Viewed 16

Does it matter from where I am calling the API, for example, the region of my KMS is the US but currently I'm calling it from the EU. Does it somehow affect decryption, coz I'm encrypting data fine but decryption gives random output? Other than region I'm not sure if something else is causing the issue, please have a look and any answer is appreciated.

Here is my code for reference:

        import { DecryptCommandInput, KMS } from "@aws-sdk/client-kms";
        import util from 'util'
        import { kmsConfig } from "./constants";
        
        export const region = kmsConfig.region;
        
        export const kms = new KMS({
          region: region,
          apiVersion: "2014-11-01",
          credentials: {
            accessKeyId: kmsConfig.accesKeyId,
            secretAccessKey: kmsConfig.secretAccessKey,
          },
          // important for react-native
          endpoint: {
            hostname: "kms." + region +".amazonaws.com",
            path: "",
            protocol: "https",
          }
        });
        
        export async function kmsEncryption(data) {
          // a client can be shared by different commands.
        
          try {
            let encryptionParams = {
              KeyId: kmsConfig.arn,
              Plaintext: data,
            };
        
            let kmsEncrypt = util.promisify(kms.encrypt).bind(kms);
        
            let encryptedData = await kmsEncrypt(encryptionParams);
        
            //encryptedData contained 2 parts, CiphertextBlob and KeyId
            console.log("Encrypted");
            return encryptedData;
            
          } catch (error) {
            console.log("\nerror => \n", error);
          }
        
        }
        
        
        export const kmsDecryption = async (encryptedData: any) => {
          try {
            let buff = Buffer.from(encryptedData.CiphertextBlob);
            let encryptedBase64data = buff.toString("base64");
            console.log("\nencryptedBase64data => \n", encryptedBase64data);
        
            let decryptionParams:DecryptCommandInput = {
              CiphertextBlob: encryptedData.CiphertextBlob,
            };
        
            let kmsDecrypt = util.promisify(kms.decrypt).bind(kms);
            let decryptedData = await kmsDecrypt(decryptionParams);
        
            // decryptedData 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();
            console.log("\noriginalText => \n", originalText);
            return originalText;
          } catch (error) {
            console.log("\ndecrypt error => \n", error);
          }
        }
    
    let encode = await kmsEncryption("helloword1234");
    let decode = await kmsDecryption(encode);

0 Answers
Related