3
我正要面臨一個問題來解密python模塊中的字符串。 我在節點js文件中加密了一個字符串並存儲在數據庫中。 在Python文件中從數據庫中獲取相同的字符串並嘗試在Python文件中解密該字符串。如何解密通過加密AES算法加密的字符串
我使用下面的配置和加密庫來加密節點文件中的字符串。
/* 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;
};
this.encryptData('[email protected] '); //將返回3eaef0dd0caa0f40ff52879f67d2af150b77adb2e807cc4721cf
this.decryptData(' 3eaef0dd0caa0f40ff52879f67d2af150b77adb2e807cc4721cf'); //將返回[email protected]
我想在Python中使用相同的方法和配置,請幫助我。我是新的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("[email protected]")
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)
在此先感謝。
哪兒了'ivHex =「000102030405060708090A0B0C0D0E0F」'和'keyHex =「000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F」'Co電子的每個元素從?它們必須匹配加密值。 – zaph
當你在函數調用中嵌入函數調用,例如'secureKeyStore.decrypt(config.password)'可能看起來像「leet/1337」,但調試變得非常困難,因爲調試時無法檢查該值。不要這樣做,請使用中間臨時變量。 – zaph