码迷,mamicode.com
首页 > 其他好文 > 详细

加密解密类

时间:2016-07-06 13:20:38      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

<?php
    /**
     * Aes
     *
     * @version
     * @copyright
     */
    class Aes {

    public function encode($value,$key) {
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $plaintext_utf8 = $value;
        //md5做效验值
        $md5_key = substr(md5($plaintext_utf8),8,16);
        $plaintext_utf8 = $md5_key . $plaintext_utf8;
        $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext_utf8, MCRYPT_MODE_CBC, $iv);
        # prepend the IV for it to be available for decryption
        $ciphertext = $iv . $ciphertext;
        # encode the resulting cipher text so it can be represented by a string
        $ciphertext_base64 = base64_encode($ciphertext);
        return  $ciphertext_base64;
    }

    public function decode($value,$key) {
        $ciphertext_dec = base64_decode($value);
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        # retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
        $iv_dec = substr($ciphertext_dec, 0, $iv_size);
        # retrieves the cipher text (everything except the $iv_size in the front)
        $ciphertext_dec = substr($ciphertext_dec, $iv_size);
        # may remove 00h valued characters from end of plain text
        $plaintext = @mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);
        $plaintext = rtrim($plaintext, "\0");
        //md5效验
        $md5_key   = substr($plaintext,0,16);
        $plaintext = substr($plaintext,16);
        if($md5_key === substr(md5($plaintext),8,16))
            return $plaintext;
        else
            return false;
    }
}

 

加密解密类

标签:

原文地址:http://www.cnblogs.com/pengcz/p/5646541.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!