openssl_encrypt模式,密钥大小,IV大小,块大小

时间:2017-05-09 17:40:53

标签: php openssl

使用mcrypt我有一个方便的cypher变量参考:

<?php

error_reporting(E_ERROR | E_PARSE);
$algorithms = mcrypt_list_algorithms();
$modes = mcrypt_list_modes();
echo '<table><th>Algorithm</th><th>Mode</th><th>Key Size</th><th>IV Size</th><th>Block Size</th>';
foreach ($algorithms as $algo) 
    {
    foreach ($modes as $mode) 
        {
        $key_size = mcrypt_get_key_size($algo,$mode);
        $iv_size = mcrypt_get_iv_size($algo,$mode);
        $block_size = mcrypt_get_block_size($algo,$mode);
        if($key_size != null) {echo '<tr><td>'.$algo.'</td><td>'.$mode.'</td><td>'.$key_size.'</td><td>'.$iv_size.'</td><td>'.$block_size.'</td></tr>';}
        }
    }
echo '</table>';

使用openssl,我看到的唯一功能是iv size

使用着名开发商Rube Goldberg开创的方法,我想出了:

<?php
$methods = openssl_get_cipher_methods();
$data = openssl_random_pseudo_bytes(1024);

echo '
    <style>
    table {border-collapse:collapse;}
    td {border:1px solid; width:100px; padding:0px 5px 0px 5px;text-align:center;}
    td.algo {text-align:left; width:200px;}
    </style>';
echo '<h3>ALL CYPHERS</h3><table><th>Algorithm</th><th>Key Size</th><th>IV Size</th>';
foreach($methods as $algo)
    {
    $key = openssl_random_pseudo_bytes(256);
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($algo));
    do
        {
        $encrypted = openssl_encrypt($data,$algo,$key,OPENSSL_RAW_DATA,$iv);
        $key = substr($key,0,strlen($key)-1);
        $decrypted = openssl_decrypt($encrypted,$algo,$key,OPENSSL_RAW_DATA,$iv);
        }
    while($decrypted == $data);
    if(strlen($key)+1 == 256) {$longkey[] = $algo;}
    echo '<tr><td class="algo">'.$algo.'</td><td>'.(strlen($key)+1).'</td><td>'.strlen($iv).'</td></tr>';
    }
echo '</table>';
echo '<h3>LONG KEY CYPHERS</h3><table><th>Algorithm</th><th>Key Size</th><th>IV Size</th>';
foreach($longkey as $algo)
    {
    $key = openssl_random_pseudo_bytes(1024);
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($algo));
    do
        {
        $encrypted = openssl_encrypt($data,$algo,$key,OPENSSL_RAW_DATA,$iv);
        $key = substr($key,0,strlen($key)-1);
        $decrypted = openssl_decrypt($encrypted,$algo,$key,OPENSSL_RAW_DATA,$iv);
        }
    while($decrypted == $data);
    echo '<tr><td class="algo">'.$algo.'</td><td>'.(strlen($key)+1).'</td><td>'.strlen($iv).'</td></tr>';
    }
echo '</table>';
  1. 必须有更明智的方法来做到这一点
  2. 我仍然没有块大小
  3. 显然,非常长的密钥(gcm,ede)不是同步算法,因为以下失败: if(openssl_decrypt($ encrypted,$ algo,$ key,OPENSSL_RAW_DATA,$ iv)!= $ data)
  4. 有什么建议吗?

0 个答案:

没有答案