Python 如何解密加密AES算法加密的字符串

Python 如何解密加密AES算法加密的字符串,python,node.js,encryption,cryptography,Python,Node.js,Encryption,Cryptography,我在python模块中面临一个解密字符串的问题。 我在NodeJS文件中加密了一个字符串,并将其存储在DB中。 在Python文件中从DB获取相同的字符串,并尝试在Python文件中解密该字符串 我使用以下配置和加密库对节点文件中的字符串进行加密 /* jshint node: true */ 'use strict'; var crypto = require('crypto'); var secureKeyStore = require('./keyEncryption').keyEncry

我在python模块中面临一个解密字符串的问题。 我在NodeJS文件中加密了一个字符串,并将其存储在DB中。 在Python文件中从DB获取相同的字符串,并尝试在Python文件中解密该字符串

我使用以下配置和加密库对节点文件中的字符串进行加密

/* jshint node: true */
'use strict';
var crypto = require('crypto');
var secureKeyStore = require('./keyEncryption').keyEncryption;

exports.encryptData = function (data) {
  var cipher = crypto.createCipher(secureKeyStore.decrypt('aes-256-ctr'), secureKeyStore.decrypt('1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8'));
  var crypted = cipher.update(data, 'utf8', 'hex');
  crypted += cipher.final('hex');
  return crypted;
};

exports.decryptData = function (data) {
  var decipher = crypto.createDecipher('aes-256-ctr'), secureKeyStore.decrypt('1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8'));
  var dec = decipher.update(data, 'hex', 'utf8');
  dec += decipher.final('utf8');
  return dec;
};
此.encryptData('abc@test.com');//将返回3eaef0dd0caa0f40ff52879f67d2af150b77adb2e807cc4721cf

此.decrypteddata('3eaef0dd0caa0f40ff52879f67d2af150b77adb2e807cc4721cf')//会回来的abc@test.com

我想在Python中使用相同的方法和配置任何人请帮助我。我是python新手,但仍然尝试了一些东西,但未能实现我想要的

 import sys
    import chilkat

    crypt = chilkat.CkCrypt2()

    #  AES is also known as Rijndael.
    crypt.put_CryptAlgorithm("aes")

    #  CipherMode may be "ctr", "cfb", "ecb" or "cbc"
    crypt.put_CipherMode("ctr")

    #  KeyLength may be 128, 192, 256
    crypt.put_KeyLength(256)

    #  Counter mode emits the exact number of bytes input, and therefore
    #  padding is not used.  The PaddingScheme property does not apply with CTR mode.

    #  EncodingMode specifies the encoding of the output for
    #  encryption, and the input for decryption.
    #  It may be "hex", "url", "base64", "quoted-printable", or many other choices.
    crypt.put_EncodingMode("hex")

    #  An initialization vector (nonce) is required if using CTR mode.
    #  The length of the IV is equal to the algorithm's block size.
    #  It is NOT equal to the length of the key.
    ivHex = "000102030405060708090A0B0C0D0E0F"
    crypt.SetEncodedIV(ivHex,"hex")

    #  The secret key must equal the size of the key.  For
    #  256-bit encryption, the binary secret key is 32 bytes.
    #  For 128-bit encryption, the binary secret key is 16 bytes.
    keyHex = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F"
    crypt.SetEncodedKey(keyHex,"hex")

    #  Encrypt a string...
    #  The input string is 44 ANSI characters (i.e. 44 bytes), so
    #  the output should be 48 bytes (a multiple of 16).
    #  Because the output is a hex string, it should
    #  be 96 characters long (2 chars per byte).
    encStr = crypt.encryptStringENC("abc@test.com")
    print('coming from here encryptStringENC',encStr)

    decrypt = chilkat.CkCrypt2()

    decrypt.put_CryptAlgorithm("aes")
    decrypt.put_CipherMode("ctr")
    decrypt.put_KeyLength(256)
    decrypt.put_EncodingMode("hex")
    decrypt.SetEncodedIV(ivHex,"hex")
    decrypt.SetEncodedKey(keyHex,"hex")


    decStr = decrypt.decryptStringENC(encStr)
    print('decrypted data',decStr)

提前感谢。

为了解决这个问题,我在NodeJS中创建了一个脚本。它将获取所有加密的用户id并返回解密的表单

var crypto = require('cryptojs');

function addDecryptedUserId(data) {
  if (data.length) {
    data.forEach(function (element) {
      if (element.userId) {       
        element.decryptedUserId =  crypto.decryptData(element.userId);
      }
    });
   return data;
  } else {
    return [];
  }
}

上述函数将获取所有元素,并向每个名为“decryptedUserId”的元素添加一个新属性,其中did
ivHex=“000102030405060708090A0B0C0D0E0F”和
keyHex=“000102030405060708090A0C0D0F1011112131415161718191A1B1C1D1E1F”
coe from?它们必须与加密值匹配。当您在函数调用中嵌入函数调用时,例如
secureKeyStore.decrypt(config.password)
可能看起来像是“leet/1337”,但调试变得非常困难,因为无法检查值以进行调试。不要这样做,使用中间临时变量。