mcrypt_module_open

(PHP 4 >= 4.0.2, PHP 5, PHP 7 < 7.2.0, PECL mcrypt >= 1.0.0)

mcrypt_module_open — 알고리즘의 모듈과 사용할 모드를 엽니다.

경고 이 함수는 PHP 7.1.0에서 더 이상 사용되지 않으며 PHP 7.2.0에서 제거되었습니다. 이 함수에 의존하는 것은 매우 권장되지 않습니다.


설명

mcrypt_module_open(
    string $algorithm,
    string $algorithm_directory,
    string $mode,
    string $mode_directory
): resource
                

이 함수는 알고리즘의 모듈과 사용할 모드를 엽니다. 알고리즘의 이름은 algorithm에 지정됩니다. 예: "twofish" 또는 MCRYPT_ciphername 상수 중 하나입니다. 모듈은 mcrypt_module_close()를 호출하여 닫힙니다.


매개변수

algorithm
MCRYPT_ciphername 상수 중 하나 또는 알고리즘 이름(문자열).
algorithm_directory
algorithm_directory 매개변수는 암호화 모듈을 찾는 데 사용됩니다. 디렉토리 이름을 제공하면 사용됩니다. 빈 문자열("")로 설정하면 mcrypt.algorithms_dir php.ini 지시문에서 설정한 값이 사용됩니다. 설정되지 않은 경우 사용되는 기본 디렉토리는 libmcrypt로 컴파일된 디렉토리(보통 /usr/local/lib/libmcrypt)입니다.
mode
MCRYPT_MODE_modename 상수 중 하나 또는 "ecb", "cbc", "cfb", "ofb", "nofb" 또는 "stream" 문자열 중 하나.
mode_directory
mode_directory 매개변수는 암호화 모듈을 찾는 데 사용됩니다. 디렉토리 이름을 제공하면 사용됩니다. 빈 문자열("")로 설정하면 mcrypt.modes_dir php.ini 지시문에서 설정한 값이 사용됩니다. 설정되지 않은 경우 사용되는 기본 디렉토리는 libmcrypt로 컴파일된 디렉토리(보통 /usr/local/lib/libmcrypt)입니다.

반환 값

일반적으로 암호화 설명자를 반환하거나 오류가 발생하면 false를 반환합니다.


Examples

예제 #1 mcrypt_module_open() 예제

                  
<?php
    $td = mcrypt_module_open(MCRYPT_DES, '',
        MCRYPT_MODE_ECB, '/usr/lib/mcrypt-modes');

    $td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
?>
                  
                

위의 예에서 첫 번째 줄은 기본 디렉터리에서 DES 암호를 열고 /usr/lib/mcrypt-modes 디렉터리에서 ECB 모드를 열려고 시도합니다. 두 번째 예는 암호 및 모드의 이름으로 문자열을 사용합니다. 이는 확장이 libmcrypt 2.4.x 또는 2.5.x에 대해 링크된 경우에만 작동합니다.

예제 #2 암호화에서 mcrypt_module_open() 사용

                  
<?php
    /* Open the cipher */
    $td = mcrypt_module_open('rijndael-256', '', 'ofb', '');

    /* Create the IV and determine the keysize length, use MCRYPT_RAND
     * on Windows instead */
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
    $ks = mcrypt_enc_get_key_size($td);

    /* Create key */
    $key = substr(md5('very secret key'), 0, $ks);

    /* Intialize encryption */
    mcrypt_generic_init($td, $key, $iv);

    /* Encrypt data */
    $encrypted = mcrypt_generic($td, 'This is very important data');

    /* Terminate encryption handler */
    mcrypt_generic_deinit($td);

    /* Initialize encryption module for decryption */
    mcrypt_generic_init($td, $key, $iv);

    /* Decrypt encrypted string */
    $decrypted = mdecrypt_generic($td, $encrypted);

    /* Terminate decryption handle and close module */
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);

    /* Show string */
    echo trim($decrypted) . "\n";
?>
                  
                

기타