国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - PHP教程 - php寫的AES加密解密類分享

php寫的AES加密解密類分享

2020-07-09 12:37PHP教程網(wǎng) PHP教程

這篇文章主要介紹了php寫的AES加密解密類,實(shí)際是為YII框架寫的,不在YII框架時(shí)只需替換其中的兩句代碼即可使用,需要的朋友可以參考下

今天寫了一個(gè)php的AES加密類。適用于Yii的擴(kuò)展。
如果不用在Yii框架中,把代碼中Yii::app()->params['encryptKey'] 換成你對(duì)應(yīng)的默認(rèn)key就可以了。
類代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
 * php AES加解密類
 * 如果要與java共用,則密鑰長(zhǎng)度應(yīng)該為16位長(zhǎng)度
 * 因?yàn)閖ava只支持128位加密,所以php也用128位加密,可以與java互轉(zhuǎn)。
 * 同時(shí)AES的標(biāo)準(zhǔn)也是128位。只是RIJNDAEL算法可以支持128,192和256位加密。
 * java 要使用AES/CBC/NoPadding標(biāo)準(zhǔn)來(lái)加解密
 *
 * @author Terry
 *
 */
class PhpAes
{
    /**
     * This was AES-128 / CBC / NoPadding encrypted.
     * return base64_encode string
     * @author Terry
     * @param string $plaintext
     * @param string $key
     */
    public static function AesEncrypt($plaintext,$key = null)
    {
        $plaintext = trim($plaintext);
        if ($plaintext == '') return '';
        if(!extension_loaded('mcrypt'))
            throw new CException(Yii::t('yii','AesEncrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));
        $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
        $key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module));
        /* Create the IV and determine the keysize length, use MCRYPT_RAND
         * on Windows instead */
        $iv = substr(md5($key),0,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 / NoPadding decrypted.
     * @author Terry
     * @param string $encrypted     base64_encode encrypted string
     * @param 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 PHP 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_MODE_CBC, '');
        $key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module));
 
        $iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));
 
        /* Initialize encryption module for decryption */
        mcrypt_generic_init($module, $key, $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.
     * @param 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 parameters.
     * If available uses the multibyte string function mb_substr
     * @param string $string the input string. Must be one character or longer.
     * @param integer $start the starting position
     * @param 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,$start,$length)
    {
        return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
    }
}

 

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 99这里只有精品 | 91久久精品日日躁夜夜躁国产 | 亚洲在线 | 国产亚洲精品久久久久久 | 国产av毛片 | 在线观看中文字幕 | 国产精品第一页在线 | 久草免费在线 | 欧美一区二区三区四区不卡 | 91视频 - 88av | 一级片在线观看 | 九九资源站 | 精品久久久久久久久久久 | 青娱乐国产视频 | 蜜桃精品在线 | 久久国产一区二区 | 日韩精品在线一区 | 在线播放黄 | 久久久久久久国产精品 | 欧美成人精品一区二区 | 婷婷五月色综合香五月 | 99久久精品国产一区二区三区 | 亚洲成人av | 国产小视频在线播放 | 欧美黑人一级爽快片淫片高清 | 大胆裸体gogo毛片免费看 | 色视频网 | 欧美日韩视频 | 日韩精品一区二区三区在线播放 | 色综合av| 中文字幕日产乱码六区小草 | 干干干操操操 | 久久国产精品久久久久久 | 亚洲精品一区二区 | 欧美在线影院 | 久久精品一区二区三区中文字幕 | 欧洲一级毛片 | 久久精品一区二区国产 | 能在线观看的黄色网址 | 91大片在线观看 | 伊人99综合 |