diff --git a/backend/core/Security/EncryptionHelper.php b/backend/core/Security/EncryptionHelper.php index d410f8f1..dd2ac923 100644 --- a/backend/core/Security/EncryptionHelper.php +++ b/backend/core/Security/EncryptionHelper.php @@ -59,12 +59,26 @@ class EncryptionHelper return $plain !== false ? $plain : false; } - // وإلا استخدم CBC القديم + // وإلا استخدم CBC القديم (دعم IV العشوائي المسبوق و IV الثابت) $decoded = base64_decode($cipherText, true); if ($decoded === false) return false; + // محاولة أولى: IV عشوائي مسبوق (أول 16 بايت) + if (strlen($decoded) >= 32) { + $iv = substr($decoded, 0, 16); + $payload = substr($decoded, 16); + $decrypted = openssl_decrypt($payload, self::ALGO_CBC, $this->key, OPENSSL_RAW_DATA, $iv); + if ($decrypted !== false && strlen($decrypted) > 0) { + $pad = ord($decrypted[strlen($decrypted) - 1]); + if ($pad >= 1 && $pad <= 16) { + return substr($decrypted, 0, -$pad); + } + } + } + + // محاولة ثانية: IV ثابت $decrypted = openssl_decrypt($decoded, self::ALGO_CBC, $this->key, OPENSSL_RAW_DATA, $this->cbcIv); - if ($decrypted === false) return false; + if ($decrypted === false || strlen($decrypted) === 0) return false; $pad = ord($decrypted[strlen($decrypted) - 1]); if ($pad < 1 || $pad > 16) return false;