`函数solveFunction(加密文本){ console.log("解密开始...");
if (!encryptedText || encryptedText.trim() === "") {
console.error("unavaible");
}
try {
const decryptedBytes = CryptoJS.AES.decrypt(encryptedText, secretKey);
const decryptedText = decryptedBytes.toString(CryptoJS.enc.Utf8);
if (!decryptedText) {
console.warn("failed!");
return "error: failed";
}
return decryptedText;
} catch (error) {
console.error("error:", error.message);
return "error";
}
}` 我正在 python 中运行 js 代码,但效率不高。我想在 python 中编写相同的代码,但我无法运行它,因为我的密码长度是 13 个字节。
如何将此代码转换为Python?
你可以尝试一下
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
import base64
def decrypt_data(encrypted_text, secret_key):
if not encrypted_text or encrypted_text.strip() == "":
print("unavailable")
return "error: unavailable"
try:
secret_key_bytes = secret_key.encode('utf-8')
encrypted_text_bytes = base64.b64decode(encrypted_text)
cipher = AES.new(secret_key_bytes, AES.MODE_ECB)
decrypted_bytes = unpad(cipher.decrypt(encrypted_text_bytes), AES.block_size)
decrypted_text = decrypted_bytes.decode('utf-8')
if not decrypted_text:
print("failed!")
return "error: failed"
return decrypted_text
except Exception as error:
print("error:", str(error))
return "error"
# Example usage
encrypted_text = "Here goes your encrypted text"
secret_key = "Your secret key"
print(decrypt_data(encrypted_text, secret_key))