Аналоговый код Python для крипто-пакета npm (ENCODE/DECODE) [закрыто]Python

Программы на Python
Anonymous
Аналоговый код Python для крипто-пакета npm (ENCODE/DECODE) [закрыто]

Сообщение Anonymous »

Это код node.js, и он отлично работает. Я хочу использовать одну и ту же БД для сеансов с сервером Node.js и с сервером Python.
В запросе регистрации мне нужен рабочий код копии в варианте Python...

Код: Выделить всё

const crypto = require('crypto');

const algorithm = 'aes-256-ctr';
const ENCRYPTION_KEY = Buffer.from('*******', 'base64');
const IV_LENGTH = 16;

class CryptoHandler {

constructor() {
}

encrypt(text) {
let iv = crypto.randomBytes(IV_LENGTH);
let cipher = crypto.createCipheriv(algorithm, Buffer.from(ENCRYPTION_KEY, 'hex'), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}

decrypt(text) {
let textParts = text.split(':');
let iv = Buffer.from(textParts.shift(), 'hex');
let encryptedText = Buffer.from(textParts.join(':'), 'hex');
let decipher = crypto.createDecipheriv(algorithm, Buffer.from(ENCRYPTION_KEY, 'hex'), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}

}

module.exports = CryptoHandler;
ОБНОВЛЕНИЕ
Переведенный код:

Код: Выделить всё

class CryptoHandler:

def __init__(self):
pass

def encrypt(self, text):
encryption_key = base64.b64decode('FoCKvdLslUuB4y3EZlKate7XGottHski1LmyqJHvUhs=')
iv = Random.new().read(AES.block_size)
cipher = AES.new(encryption_key, AES.MODE_CTR, iv)
encrypted = cipher.encrypt(text.encode())
return iv.hex() + ':' + encrypted.hex()

def decrypt(self, text):
encryption_key = base64.b64decode('FoCKvdLslUuB4y3EZlKate7XGottHski1LmyqJHvUhs=')
iv, encrypted = text.split(':')
iv = bytes.fromhex(iv)
encrypted = bytes.fromhex(encrypted)
cipher = AES.new(encryption_key, AES.MODE_CTR, iv)
decrypted = cipher.decrypt(encrypted)
return decrypted.decode('utf-8')

Журнал ошибок в строке cipher = AES.new(encryption_key, AES.MODE_CTR, iv) :

Код: Выделить всё

An exception occurred :: Too many arguments for this mode

Любые предложения!


Подробнее здесь: https://stackoverflow.com/questions/786 ... ode-decode

Вернуться в «Python»