복붙노트

Mcrypt에서 OpenSSL으로 암호화 라이브러리 업그레이드

PHP

Mcrypt에서 OpenSSL으로 암호화 라이브러리 업그레이드

아래의 일반적으로 참조 된 라이브러리를 암호화에 사용하고 있습니다. 나는 더 이상 사용되지 않는 라이브러리를 사용하지 않도록 Mcrypt에서 OpenSSL로 업그레이드하려고합니다.

나는 이것이 가능한지 알아 내려고하고있다. 나는 이것에 대한 연구를 해봤지만, 상충되는 정보를 발견했다.

이 게시물은 Mcrypt로 암호화 된 OpenSSL을 사용하여 데이터를 해독하는 것은 불가능하다고 말합니다. https://stackoverflow.com/a/19748494/5834657

그러나이 게시물은 패딩을 사용하는 것이 가능하다고 말합니다. 내 함수가 패딩을 사용하는 것 같습니다. 이 작업을 수행하는 데 필요한 패딩 유형입니까? https://stackoverflow.com/a/31614770/5834657

<?php 

namespace Utilities\Encryption;

/**
* A class to handle secure encryption and decryption of arbitrary data
*
* Note that this is not just straight encryption.  It also has a few other
* features in it to make the encrypted data far more secure.  Note that any
* other implementations used to decrypt data will have to do the same exact
*  operations.  
*
* Security Benefits:
*
* - Uses Key stretching
* - Hides the Initialization Vector
* - Does HMAC verification of source data
*
*/

class Encryption {

/**
 * @var string $cipher The mcrypt cipher to use for this instance
 */
protected $cipher = '';

/**
 * @var int $mode The mcrypt cipher mode to use
 */
protected $mode = '';

/**
 * @var int $rounds The number of rounds to feed into PBKDF2 for key generation
 */
protected $rounds = 100;

/**
 * Constructor!
 *
 * @param string $cipher The MCRYPT_* cypher to use for this instance
 * @param int    $mode   The MCRYPT_MODE_* mode to use for this instance
 * @param int    $rounds The number of PBKDF2 rounds to do on the key
 */
public function __construct($cipher, $mode, $rounds = 100) {
    $this->cipher = $cipher;
    $this->mode = $mode;
    $this->rounds = (int) $rounds;
}

/**
 * Decrypt the data with the provided key
 *
 * @param string $data The encrypted datat to decrypt
 * @param string $key  The key to use for decryption
 * 
 * @returns string|false The returned string if decryption is successful
 *                           false if it is not
 */
public function decrypt($data, $key) {
    $salt = substr($data, 0, 128);
    $enc = substr($data, 128, -64);
    $mac = substr($data, -64);

    list ($cipherKey, $macKey, $iv) = $this->getKeys($salt, $key);

    if ($mac !== hash_hmac('sha512', $enc, $macKey, true)) {
         return false;
    }

    $dec = mcrypt_decrypt($this->cipher, $cipherKey, $enc, $this->mode, $iv);

    $data = $this->unpad($dec);

    return $data;
}

/**
 * Encrypt the supplied data using the supplied key
 * 
 * @param string $data The data to encrypt
 * @param string $key  The key to encrypt with
 *
 * @returns string The encrypted data
 */
public function encrypt($data, $key) {
    $salt = mcrypt_create_iv(128, MCRYPT_DEV_URANDOM);
    list ($cipherKey, $macKey, $iv) = $this->getKeys($salt, $key);

    $data = $this->pad($data);

    $enc = mcrypt_encrypt($this->cipher, $cipherKey, $data, $this->mode, $iv);

    $mac = hash_hmac('sha512', $enc, $macKey, true);
    return $salt . $enc . $mac;
}

/**
 * Generates a set of keys given a random salt and a master key
 *
 * @param string $salt A random string to change the keys each encryption
 * @param string $key  The supplied key to encrypt with
 *
 * @returns array An array of keys (a cipher key, a mac key, and a IV)
 */
protected function getKeys($salt, $key) {
    $ivSize = mcrypt_get_iv_size($this->cipher, $this->mode);
    $keySize = mcrypt_get_key_size($this->cipher, $this->mode);
    $length = 2 * $keySize + $ivSize;

    $key = $this->pbkdf2('sha512', $key, $salt, $this->rounds, $length);

    $cipherKey = substr($key, 0, $keySize);
    $macKey = substr($key, $keySize, $keySize);
    $iv = substr($key, 2 * $keySize);
    return array($cipherKey, $macKey, $iv);
}

/**
 * Stretch the key using the PBKDF2 algorithm
 *
 * @see http://en.wikipedia.org/wiki/PBKDF2
 *
 * @param string $algo   The algorithm to use
 * @param string $key    The key to stretch
 * @param string $salt   A random salt
 * @param int    $rounds The number of rounds to derive
 * @param int    $length The length of the output key
 *
 * @returns string The derived key.
 */
protected function pbkdf2($algo, $key, $salt, $rounds, $length) {
    $size   = strlen(hash($algo, '', true));
    $len    = ceil($length / $size);
    $result = '';
    for ($i = 1; $i <= $len; $i++) {
        $tmp = hash_hmac($algo, $salt . pack('N', $i), $key, true);
        $res = $tmp;
        for ($j = 1; $j < $rounds; $j++) {
             $tmp  = hash_hmac($algo, $tmp, $key, true);
             $res ^= $tmp;
        }
        $result .= $res;
    }
    return substr($result, 0, $length);
}

protected function pad($data) {
    $length = mcrypt_get_block_size($this->cipher, $this->mode);
    $padAmount = $length - strlen($data) % $length;
    if ($padAmount == 0) {
        $padAmount = $length;
    }
    return $data . str_repeat(chr($padAmount), $padAmount);
}

protected function unpad($data) {
    $length = mcrypt_get_block_size($this->cipher, $this->mode);
    $last = ord($data[strlen($data) - 1]);
    if ($last > $length) return false;
    if (substr($data, -1 * $last) !== str_repeat(chr($last), $last)) {
        return false;
    }
    return substr($data, 0, -1 * $last);
 }
}

업데이트 : 나는 라이브러리를 OpenSSL로 변환하여 OpenSSL을 사용하여 Mcrypt로 암호화 된 문자열의 암호를 해독하려고 노력해 왔습니다.

그런 다음 위 코드를 사용하여 키를 사용하여 문자열을 암호화하고 아래 코드와 같은 키를 사용하여 해당 값의 암호를 해독하려고합니다. 그러나 나는 그냥 빈 응답을 얻고있다. 내가 코멘트하면 :

$ data = $ this-> unpad ($ dec)

해독 함수에서 문자열을 얻지 만 캐릭터가 뒤죽박죽입니다 (처음 암호화 될 때처럼 보입니다).

<?php 

namespace Utilities\Encryption;

/**
* A class to handle secure encryption and decryption of arbitrary data
*
* Note that this is not just straight encryption.  It also has a few other
*  features in it to make the encrypted data far more secure.  Note that any
*  other implementations used to decrypt data will have to do the same exact
*  operations.  
*
* Security Benefits:
*
* - Uses Key stretching
* - Hides the Initialization Vector
* - Does HMAC verification of source data
*
*/
class EncryptionOpenSsl {

/**
 * @var string $cipher The mcrypt cipher to use for this instance
 */
protected $cipher = '';

/**
 * @var int $mode The mcrypt cipher mode to use
 */
protected $mode = '';

/**
 * @var int $rounds The number of rounds to feed into PBKDF2 for key generation
 */
protected $rounds = 100;

/**
 * Constructor!
 *
 * @param string $cipher The MCRYPT_* cypher to use for this instance
 * @param int    $mode   The MCRYPT_MODE_* mode to use for this instance
 * @param int    $rounds The number of PBKDF2 rounds to do on the key
 */
public function __construct($cipher, $rounds = 100) {
    $this->cipher = $cipher;
    // $this->mode = MCRYPT_MODE_CBC;
    $this->rounds = (int) $rounds;
}

/**
 * Decrypt the data with the provided key
 *
 * @param string $data The encrypted datat to decrypt
 * @param string $key  The key to use for decryption
 * 
 * @returns string|false The returned string if decryption is successful
 *                           false if it is not
 */
public function decrypt($data, $key) {
    $salt = substr($data, 0, 128);
    $enc = substr($data, 128, -64);
    $mac = substr($data, -64);

    list ($cipherKey, $macKey, $iv) = $this->getKeys($salt, $key);

    if ($mac !== hash_hmac('sha512', $enc, $macKey, true)) {
         return false;
    }
    $dec = openssl_decrypt($enc, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
    // $dec = mcrypt_decrypt($this->cipher, $cipherKey, $enc, $this->mode, $iv);

    $data = $this->unpad($dec);

    return $data;
}

/**
 * Encrypt the supplied data using the supplied key
 * 
 * @param string $data The data to encrypt
 * @param string $key  The key to encrypt with
 *
 * @returns string The encrypted data
 */
public function encrypt($data, $key) {
    $salt = mcrypt_create_iv(128, MCRYPT_DEV_URANDOM);
    list ($cipherKey, $macKey, $iv) = $this->getKeys($salt, $key);

    $data = $this->pad($data);

    $enc = mcrypt_encrypt($this->cipher, $cipherKey, $data, $this->mode, $iv);

    $mac = hash_hmac('sha512', $enc, $macKey, true);
    return $salt . $enc . $mac;
}

/**
 * Generates a set of keys given a random salt and a master key
 *
 * @param string $salt A random string to change the keys each encryption
 * @param string $key  The supplied key to encrypt with
 *
 * @returns array An array of keys (a cipher key, a mac key, and a IV)
 */
protected function getKeys($salt, $key) {
    $ivSize = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
    $keySize = mcrypt_get_key_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
    $length = 2 * $keySize + $ivSize;

    $key = $this->pbkdf2('sha512', $key, $salt, $this->rounds, $length);

    $cipherKey = substr($key, 0, $keySize);
    $macKey = substr($key, $keySize, $keySize);
    $iv = substr($key, 2 * $keySize);
    return array($cipherKey, $macKey, $iv);
}

/**
 * Stretch the key using the PBKDF2 algorithm
 *
 * @see http://en.wikipedia.org/wiki/PBKDF2
 *
 * @param string $algo   The algorithm to use
 * @param string $key    The key to stretch
 * @param string $salt   A random salt
 * @param int    $rounds The number of rounds to derive
 * @param int    $length The length of the output key
 *
 * @returns string The derived key.
 */
protected function pbkdf2($algo, $key, $salt, $rounds, $length) {
    $size   = strlen(hash($algo, '', true));
    $len    = ceil($length / $size);
    $result = '';
    for ($i = 1; $i <= $len; $i++) {
        $tmp = hash_hmac($algo, $salt . pack('N', $i), $key, true);
        $res = $tmp;
        for ($j = 1; $j < $rounds; $j++) {
             $tmp  = hash_hmac($algo, $tmp, $key, true);
             $res ^= $tmp;
        }
        $result .= $res;
    }
    return substr($result, 0, $length);
}

protected function pad($data) {
    $length = mcrypt_get_block_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
    $padAmount = $length - strlen($data) % $length;
    if ($padAmount == 0) {
        $padAmount = $length;
    }
    return $data . str_repeat(chr($padAmount), $padAmount);
}

protected function unpad($data) {
    $length = mcrypt_get_block_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
    $last = ord($data[strlen($data) - 1]);
    if ($last > $length) return false;
    if (substr($data, -1 * $last) !== str_repeat(chr($last), $last)) {
        return false;
    }
    return substr($data, 0, -1 * $last);
 }
}

해결법

  1. ==============================

    1.귀하의 암호 해독 루틴에 대한이 코드는 나를 위해 작동합니다 :

    귀하의 암호 해독 루틴에 대한이 코드는 나를 위해 작동합니다 :

    public function decrypt($data, $key) {
        $salt = substr($data, 0, 128);
        $enc = substr($data, 128, -64);
        $mac = substr($data, -64);
    
        list ($cipherKey, $macKey, $iv) = $this->getKeys($salt, $key);
    
        if ($mac !== hash_hmac('sha512', $enc, $macKey, true)) {
             return false;
        }
    
        $dec = openssl_decrypt($enc, $this->cipher, $cipherKey, OPENSSL_RAW_DATA, $iv);
    
        return $dec;
    }
    

    테스트:

    $keys = [
        'this is a secret key.',
        'G906m70p(IhzA5T&5x7(w0%a631)u)%D6E79cIYJQ!iP2U(xT13q6)tJ6gZ3D2wi&0")7cP5',
        chr(6) . chr(200) . chr(16) . 'my key ' . chr(3) . chr(4) . chr(192) . chr(254) . ' zyx0987!!',
        'and finally one more key to test with here:',
    ];
    
    
    $data = [
        'A',
        'This is a test',
        'now test encrypting something a little bit longer with 1234567890.',
        '$length = mcrypt_get_block_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC); $last = ord($data[strlen($data) - 1]);',
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sit amet pharetra urna. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Ut fringilla, quam sed eleifend eleifend, justo turpis consectetur tellus, quis tristique eros erat at nibh. Nunc dictum neque vel diam molestie fermentum. Pellentesque dignissim dui quis tortor eleifend, ut maximus elit egestas. Donec posuere odio et auctor porta. Quisque placerat condimentum maximus. Curabitur luctus dolor eget sem luctus, in dignissim tortor venenatis. Mauris eget nulla nisl.',
    ];
    
    $failures = 0;
    
    foreach ($data as $datum) {
        foreach ($keys as $key) {
            $enc = new Encryption(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
    
            $encrypted = $enc->encrypt($datum, $key);
    
            $dec = new EncryptionOpenSsl('bf-cbc');
    
            $decrypted = $dec->decrypt($encrypted, $key);
    
            if (strcmp($datum, $decrypted) !== 0) {
                echo "Encryption with key '$key' of '$datum' failed.  '$decrypted' != '$datum'<br><br>\n\n";
                $failures++;
            }
        }
    }
    
    if ($failures) {
        echo "$failures tests failed.<br>\n";
    } else {
        echo "ALL OKAY<br>\n";
    }
    

    당신이 잘 작동하는지 확인할 수 있다면 답을 정리하고 최종 작업 코드를 추가 할 수 있습니다.

  2. from https://stackoverflow.com/questions/43329513/upgrading-my-encryption-library-from-mcrypt-to-openssl by cc-by-sa and MIT license