Cara Menukar Penyulitan Mcrypt kepada Penyulitan OpenSSL: Panduan Langkah demi Langkah

Patricia Arquette
Lepaskan: 2024-11-15 02:00:02
asal
662 orang telah melayarinya

How to Convert Mcrypt Encryption to OpenSSL Encryption: A Step-by-Step Guide

Converting Mcrypt to OpenSSL Encryption

Mcrypt, a legacy encryption library, has seen its retirement due to security vulnerabilities and the emergence of OpenSSL as the industry standard. While switching from Mcrypt to OpenSSL may seem daunting, it can be simplified by understanding the key differences and adopting appropriate strategies.

Cipher and Padding Disparity

Blowfish, the cipher you're using, requires data padding for encryption. OpenSSL employs PKCS#7 padding, while Mcrypt utilizes PKCS#5. PKCS#7 requires a minimum padding length of 1, while PKCS#5 accepts 0. This difference affects the encrypted results obtained from each library.

IV Usage

In ECB mode, which you're employing, an initialization vector (IV) is not required. However, this is not the case in other encryption modes. Ensure you set the IV length to 0 when using OpenSSL for ECB mode.

Encryption and Decryption

To decrypt data encrypted with Mcrypt using OpenSSL, manual padding with PKCS#7 is required prior to encryption. This ensures compatibility between the two libraries.

Example Code

The provided code demonstrates the key differences and proper padding techniques:

$key = "anotherpassword1";
$str = "does it work 12";

// Mcrypt padding
$enc = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $str."\1", MCRYPT_MODE_ECB);

// OpenSSL padding
$enc = openssl_encrypt($str, 'bf-ecb', $key, true);

// Decrypt using both methods
$dec_mcrypt = mcrypt_decrypt(MCRYPT_BLOWFISH, $key, $enc, MCRYPT_MODE_ECB);
$dec_openssl = openssl_decrypt($enc, 'bf-ecb', $key, true);

echo bin2hex($enc) . PHP_EOL;
var_dump($dec_mcrypt);
echo PHP_EOL;
var_dump($dec_openssl);
Salin selepas log masuk

This code demonstrates successful encryption and decryption using both Mcrypt and OpenSSL, applying PKCS#7 padding for compatibility.

Atas ialah kandungan terperinci Cara Menukar Penyulitan Mcrypt kepada Penyulitan OpenSSL: Panduan Langkah demi Langkah. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan