微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php写的AES加密解密类分享

今天写了一个PHP的AES加密类。适用于Yii的扩展。 如果不用在Yii框架中,把代码中Yii::app()->params['encryptKey'] 换成你对应的认key就可以了。 类代码:

rush:PHP;"> params['encryptKey'] : $key,mcrypt_enc_get_key_size($module)); /* Create the IV and determine the keysize length,use MCRYPT_RAND * on Windows instead */ $iv = substr(md5($key),mcrypt_enc_get_iv_size($module)); /* Intialize encryption */ mcrypt_generic_init($module,$key,$iv);
    /* Encrypt data */
    $encrypted = mcrypt_generic($module,$plaintext);

    /* Terminate encryption handler */
    mcrypt_generic_deinit($module);
    mcrypt_module_close($module);
    return base64_encode($encrypted);
}

/**
 * This was AES-128 / CBC / <a href="https://www.jb51.cc/tag/nop/" target="_blank" class="keywords">nop</a>adding decrypted.
 * @author Terry
 * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m string $encrypted     base64_encode encrypted string
 * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m string $key
 * @throws CException
 * @return string
 */
public static function AesDecrypt($encrypted,$key = null)
{
    if ($encrypted == '') return '';
    if(!extension_loaded('mcrypt'))
        throw new CException(Yii::t('yii','AesDecrypt requires <a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a> mcrypt extension to be loaded in order to use data encryption feature.'));

    $ciphertext_dec = base64_decode($encrypted);
    $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128,mcrypt_enc_get_key_size($module));

    $iv = substr(md5($key),mcrypt_enc_get_iv_size($module));

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

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

    /* Terminate decryption handle and close module */
    mcrypt_generic_deinit($module);
    mcrypt_module_close($module);
    return rtrim($decrypted,"\0");
}

/**
 * Returns the length of the given string.
 * If available uses the multibyte string function mb_strlen.
 * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m string $string the string being measured for length
 * @return integer the length of the string
 */
private static function strlen($string)
{
    return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
}

/**
 * Returns the portion of string specified by the start and length p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>meters.
 * If available uses the multibyte string function mb_substr
 * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m string $string the input string. Must be one ch<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>cter or longer.
 * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m integer <a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a> the starting position
 * @p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>m integer $length the desired portion length
 * @return string the extracted part of string,or FALSE on failure or an empty string.
 */
private static function substr($string,<a href="https://www.jb51.cc/tag/start/" target="_blank" class="keywords">$start</a>,$length)
{
    return extension_loaded('mbstring') ? mb_substr($string,$length,'8bit') : substr($string,$length);
}

}

原文地址:https://www.jb51.cc/php/24398.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐