mcrypt_encrypt

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

mcrypt_encrypt使锟矫革拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

Warning

This function has been DEPRECATED as of PHP 7.1.0. Relying on this function is highly discouraged.

说锟斤拷

mcrypt_encrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] ) : string

锟斤拷锟斤拷锟斤拷锟捷诧拷锟斤拷锟斤拷锟斤拷锟侥★拷

锟斤拷锟斤拷

cipher

MCRYPT_ciphername 锟斤拷锟斤拷锟叫碉拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷值锟斤拷锟姐法锟斤拷锟狡★拷

key

锟斤拷锟斤拷锟斤拷钥锟斤拷 锟斤拷锟斤拷锟皆匡拷锟斤拷炔锟斤拷歉锟斤拷惴拷锟斤拷芄锟街э拷值锟斤拷锟叫э拷锟斤拷龋锟斤拷锟斤拷锟斤拷锟斤拷岱拷锟斤拷锟斤拷娌拷锟斤拷锟� FALSE

data

使锟矫革拷锟斤拷锟斤拷 cipher 锟斤拷 mode 锟斤拷锟杰碉拷锟斤拷锟捷★拷 锟斤拷锟斤拷锟斤拷莩锟斤拷炔锟斤拷锟� n*锟斤拷锟斤拷锟叫★拷锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 '\0' 锟斤拷锟诫。

锟斤拷锟截碉拷锟斤拷锟侥筹拷锟饺匡拷锟杰憋拷 data 锟斤拷锟斤拷

mode

MCRYPT_MODE_modename 锟斤拷锟斤拷锟叫碉拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟叫碉拷一锟斤拷锟斤拷"ecb"锟斤拷"cbc"锟斤拷"cfb"锟斤拷"ofb"锟斤拷"nofb" 锟斤拷 "stream"锟斤拷

iv

Used for the initialization in CBC, CFB, OFB modes, and in some algorithms in STREAM mode. If the provided IV size is not supported by the chaining mode or no IV was provided, but the chaining mode requires one, the function will emit a warning and return FALSE.

锟斤拷锟斤拷值

锟斤拷锟街凤拷锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷锟侥o拷 锟斤拷锟斤拷锟斤拷失锟斤拷时锟斤拷锟斤拷 FALSE锟斤拷

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.6.0 锟斤拷锟劫斤拷锟斤拷锟斤拷效锟斤拷锟饺碉拷 key and iv 锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫э拷锟斤拷锟� mcrypt_decrypt() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷娌拷曳锟斤拷锟� FALSE锟斤拷 之前锟芥本锟叫o拷锟斤拷锟节筹拷锟饺诧拷锟斤拷锟斤拷锟皆匡拷统锟绞硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 '\0' 使锟斤拷锏斤拷锟叫э拷锟斤拷取锟�

锟斤拷锟斤拷

Example #1 mcrypt_encrypt() 锟斤拷锟斤拷

<?php
    
# --- 锟斤拷锟斤拷 ---

    # 锟斤拷钥应锟斤拷锟斤拷锟斤拷锟斤拷亩锟斤拷锟斤拷锟斤拷锟斤拷荩锟�
    # 锟斤拷始使锟斤拷 scrypt, bcrypt 锟斤拷 PBKDF2 锟斤拷一锟斤拷锟街凤拷锟斤拷转锟斤拷锟斤拷一锟斤拷锟斤拷钥
    # 锟斤拷钥锟斤拷 16 锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷式
    
$key pack('H*'"bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");
    
    
# 锟斤拷示 AES-128, 192, 256 锟斤拷应锟斤拷锟斤拷钥锟斤拷锟饺o拷
    #16锟斤拷24锟斤拷32 锟街节★拷
    
$key_size =  strlen($key);
    echo 
"Key size: " $key_size "\n";
    
    
$plaintext "This string was AES-256 / CBC / ZeroBytePadding encrypted.";

    
# 为 CBC 模式锟斤拷锟斤拷锟斤拷锟斤拷某锟绞硷拷锟斤拷锟�
    
$iv_size mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128MCRYPT_MODE_CBC);
    
$iv mcrypt_create_iv($iv_sizeMCRYPT_RAND);
    

    
# 锟斤拷锟斤拷锟斤拷 AES 锟斤拷锟捷碉拷锟斤拷锟侥o拷Rijndael 锟斤拷锟斤拷锟叫� = 128锟斤拷
    # 锟斤拷锟斤拷锟斤拷锟节憋拷锟斤拷锟斤拷锟斤拷锟诫不锟斤拷锟斤拷 00h 锟斤拷尾锟斤拷
    # 锟斤拷锟斤拷为默锟斤拷锟斤拷使锟斤拷 0 锟斤拷锟斤拷锟斤拷锟斤拷锟捷o拷
    
$ciphertext mcrypt_encrypt(MCRYPT_RIJNDAEL_128$key,
                                 
$plaintextMCRYPT_MODE_CBC$iv);

    
# 锟斤拷锟斤拷始锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷之锟斤拷锟皆癸拷锟斤拷锟斤拷时使锟斤拷
    
$ciphertext $iv $ciphertext;
    
    
# 锟斤拷锟斤拷锟侥斤拷锟斤拷 base64 锟斤拷锟斤拷
    
$ciphertext_base64 base64_encode($ciphertext);

    echo  
$ciphertext_base64 "\n";

    
# === 锟斤拷锟斤拷 ===

    # 锟斤拷锟侥诧拷未锟斤拷锟斤拷锟斤拷锟斤拷锟皆和匡拷锟脚度憋拷锟斤拷锟斤拷
    # 锟斤拷锟皆匡拷锟斤拷锟斤拷锟斤拷 Padding Oracle 锟斤拷锟斤拷锟斤拷
    
    # --- 锟斤拷锟斤拷 ---
    
    
$ciphertext_dec base64_decode($ciphertext_base64);
    
    
# 锟斤拷始锟斤拷锟斤拷锟斤拷小锟斤拷锟斤拷锟斤拷通锟斤拷 mcrypt_get_iv_size() 锟斤拷锟斤拷锟�
    
$iv_dec substr($ciphertext_dec0$iv_size);
    
    
# 锟斤拷取锟斤拷锟斤拷始锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
    
$ciphertext_dec substr($ciphertext_dec$iv_size);

    
# 锟斤拷锟斤拷锟斤拷要锟斤拷锟斤拷锟斤拷末尾锟狡筹拷 0
    
$plaintext_dec mcrypt_decrypt(MCRYPT_RIJNDAEL_128$key,
                                    
$ciphertext_decMCRYPT_MODE_CBC$iv_dec);
    
    echo  
$plaintext_dec "\n";
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

Key size: 32
ENJW8mS2KaJoNB5E5CoSAAu0xARgsR1bdzFWpEn+poYw45q+73az5kYi4j+0haevext1dGrcW8Qi59txfCBV8BBj3bzRP3dFCp3CPQSJ8eU=
This string was AES-256 / CBC / ZeroBytePadding encrypted.

锟轿硷拷

  • mcrypt_decrypt() - 使锟矫革拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
  • mcrypt_module_open() - 锟斤拷锟姐法锟斤拷模式锟斤拷应锟斤拷模锟斤拷