mcrypt_encrypt Encrypts plaintext with given parameters


mcrypt_encrypt

(PHP 4 >= 4.0.2, PHP 5)

mcrypt_encryptEncrypts plaintext with given parameters

说明

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

Encrypts the data and returns it.

参数

cipher

One of the MCRYPT_ciphername constants, or the name of the algorithm as string.

key

The key with which the data will be encrypted. If it's smaller than the required keysize, it is padded with ''. It is better not to use ASCII strings for keys.

It is recommended to use the mhash functions to create a key from a string.

data

The data that will be encrypted with the given cipher and mode. If the size of the data is not n * blocksize, the data will be padded with ''.

The returned crypttext can be larger than the size of the data that was given by data.

mode

One of the MCRYPT_MODE_modename constants, or one of the following strings: "ecb", "cbc", "cfb", "ofb", "nofb" or "stream".

iv

Used for the initialization in CBC, CFB, OFB modes, and in some algorithms in STREAM mode. If you do not supply an IV, while it is needed for an algorithm, the function issues a warning and uses an IV with all its bytes set to ''.

返回值

Returns the encrypted data, as a string.

范例

Example #1 mcrypt_encrypt() Example

<?php
    $iv_size 
mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256MCRYPT_MODE_ECB);
    
$iv mcrypt_create_iv($iv_sizeMCRYPT_RAND);
    
$key "This is a very secret key";
    
$text "Meet me at 11 o'clock behind the monument.";
    echo 
strlen($text) . " ";

    
$crypttext mcrypt_encrypt(MCRYPT_RIJNDAEL_256$key$textMCRYPT_MODE_ECB$iv);
    echo 
strlen($crypttext) . " ";
?>

以上例程会输出:

42
64

See also mcrypt_module_open() for a more advanced API and an example.


«  mcrypt_ecb
» mcrypt_enc_get_algorithms_name
快速导航

Copyright © 2016 phpStudy | 皖ICP备18014864号-4