Maison  >  Article  >  développement back-end  >  Comment convertir le cryptage Mcrypt en cryptage OpenSSL : un guide étape par étape

Comment convertir le cryptage Mcrypt en cryptage OpenSSL : un guide étape par étape

Patricia Arquette
Patricia Arquetteoriginal
2024-11-15 02:00:02659parcourir

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);

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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn