mcrypt_generic_init

(PHP 4 >= 4.0.2, PHP 5)

mcrypt_generic_initThis function initializes all buffers needed for encryption

Description

int mcrypt_generic_init ( resource $td , string $key , string $iv )

You need to call this function before every call to mcrypt_generic() or mdecrypt_generic().

Parameters

td

The encryption descriptor.

key

The maximum length of the key should be the one obtained by calling mcrypt_enc_get_key_size() and every value smaller than this is legal.

iv

The IV should normally have the size of the algorithms block size, but you must obtain the size by calling mcrypt_enc_get_iv_size(). IV is ignored in ECB. IV MUST exist in CFB, CBC, STREAM, nOFB and OFB modes. It needs to be random and unique (but not secret). The same IV must be used for encryption/decryption. If you do not want to use it you should set it to zeros, but this is not recommended.

Return Values

The function returns a negative value on error: -3 when the key length was incorrect, -4 when there was a memory allocation problem and any other return value is an unknown error. If an error occurs a warning will be displayed accordingly. FALSE is returned if incorrect parameters were passed.

See Also

Коментарии

Автор:
The mcrypt implementation of RC2 algorithm supports an effective key length of 1024 bits only.
It is however possible to use an effective key length in 1..1024 bits, by transforming the key before use as follows:

<?php

function transformKey($key$effKeyLen)

{
   
$pitable = array(
       
0xD90x780xF90xC40x190xDD0xB50xED0x280xE90xFD0x790x4A0xA00xD80x9D,
       
0xC60x7E0x370x830x2B0x760x530x8E0x620x4C0x640x880x440x8B0xFB0xA2,
       
0x170x9A0x590xF50x870xB30x4F0x130x610x450x6D0x8D0x090x810x7D0x32,
       
0xBD0x8F0x400xEB0x860xB70x7B0x0B0xF00x950x210x220x5C0x6B0x4E0x82,
       
0x540xD60x650x930xCE0x600xB20x1C0x730x560xC00x140xA70x8C0xF10xDC,
       
0x120x750xCA0x1F0x3B0xBE0xE40xD10x420x3D0xD40x300xA30x3C0xB60x26,
       
0x6F0xBF0x0E0xDA0x460x690x070x570x270xF20x1D0x9B0xBC0x940x430x03,
       
0xF80x110xC70xF60x900xEF0x3E0xE70x060xC30xD50x2F0xC80x660x1E0xD7,
       
0x080xE80xEA0xDE0x800x520xEE0xF70x840xAA0x720xAC0x350x4D0x6A0x2A,
       
0x960x1A0xD20x710x5A0x150x490x740x4B0x9F0xD00x5E0x040x180xA40xEC,
       
0xC20xE00x410x6E0x0F0x510xCB0xCC0x240x910xAF0x500xA10xF40x700x39,
       
0x990x7C0x3A0x850x230xB80xB40x7A0xFC0x020x360x5B0x250x550x970x31,
       
0x2D0x5D0xFA0x980xE30x8A0x920xAE0x050xDF0x290x100x670x6C0xBA0xC9,
       
0xD30x000xE60xCF0xE10x9E0xA80x2C0x630x160x010x3F0x580xE20x890xA9,
       
0x0D0x380x340x1B0xAB0x330xFF0xB00xBB0x480x0C0x5F0xB90xB10xCD0x2E,
       
0xC50xF30xDB0x470xE50xA50x9C0x770x0A0xA60x200x680xFE0x7F0xC10xAD);
   
$invpitable array_flip($pitable);

   
//    Apply the regular RC2 key expansion algorithm.
   
$t strlen($key);
   
$key array_values(unpack('C*C'$key));

    for (
$i $t$i 128$i++)
       
$key[$i] = $pitable[($key[$i 1] + $key[$i $t]) & 0xFF];

   
$t8 = ($effKeyLen 7) >> 3;
   
$tm 0xFF >> ($t8 $effKeyLen);
   
$i 128 $t8;
   
$key[$i] = $pitable[$key[$i] & $tm];

    while (
$i--)
       
$key[$i] = $pitable[$key[$i 1] ^ $key[$i $t8]];

   
//    Map the first byte: this operation will be undone by
    //        mcrypt internals.
   
$key[0] = $invpitable[$key[0]];

   
//    Return the transformed key as a string.
   
array_unshift($key'C*');
    return 
call_user_func_array('pack'$key);
}

//    Usage example
$r mcrypt_module_open(MCRYPT_RC2$algoDir$mode$modeDir);
mcrypt_generic_init($rtransformKey($originalKey$effectiveKeyLength), $iv);

?>
2013-02-26 20:33:02
http://php5.kiev.ua/manual/ru/function.mcrypt-generic-init.html

    Поддержать сайт на родительском проекте КГБ