Недопустимый аргумент(ы): длина входных данных должна быть кратна
размеру блока шифра
Вот пример PHP-кода
Код: Выделить всё
function encryptDomainWithToken($domain, $token)
{
// Encryption method using OpenSSL with AES-256-CBC
$method = 'AES-256-CBC';
$ivLength = openssl_cipher_iv_length($method); // IV length
$iv = openssl_random_pseudo_bytes($ivLength); // Initialization Vector
$key = hash('sha256', $token, true); // Derive encryption key from token
// Data to encrypt
$data = $domain;
// Encrypt data
$encryptedData = openssl_encrypt($data, $method, $key, 0, $iv);
// Combine IV with encrypted data and encode in base64
return base64_encode($iv . $encryptedData);
}
Код: Выделить всё
import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:encrypt/encrypt.dart';
String decryptDomainWithToken(String encrypted, String token) {
final method = AESMode.cbc;
// Derive key from token using SHA-256
final key = sha256.convert(utf8.encode(token)).bytes;
final encrypter = Encrypter(AES(Key(Uint8List.fromList(key)), mode: method));
// Decode the base64 encrypted string
final decodedData = base64.decode(encrypted);
// Determine IV length for AES-256-CBC
final ivLength = 16;
final iv = IV(Uint8List.fromList(decodedData.sublist(0, ivLength)));
final encryptedData = Encrypted(Uint8List.fromList(decodedData.sublist(ivLength)));
// Decrypt the data
return encrypter.decrypt(encryptedData, iv: iv);
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... e-of-ciphe