zoukankan      html  css  js  c++  java
  • Aes CBC加密

     1 <?php
     2 namespace appcomponents;
     3 use yii;
     4 class Aes
     5 {
     6     /**
     7      * This was AES-128 / CBC / PKCS5Padding
     8      * return base64_encode string
     9      * @param string $plaintext
    10      * @param string $key
    11      * @return string
    12      */
    13     public static function AesEncrypt($plaintext,$key = null)
    14     {
    15         $plaintext = trim($plaintext);
    16         if ($plaintext == '') return '';
    17         $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    18 
    19         //PKCS5Padding
    20         $padding = $size - strlen($plaintext) % $size;
    21         // 添加Padding
    22         $plaintext .= str_repeat(chr($padding), $padding);
    23 
    24 
    25         $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    26         $key=self::substr($key, 0, mcrypt_enc_get_key_size($module));
    27         $iv = $key;
    28         // str_repeat($key, $size);
    29         /* Intialize encryption */
    30         mcrypt_generic_init($module, $key, $iv);
    31 
    32 
    33         /* Encrypt data */
    34         $encrypted = mcrypt_generic($module, $plaintext);
    35         /* Terminate encryption handler */
    36         mcrypt_generic_deinit($module);
    37         mcrypt_module_close($module);
    38         return base64_encode($encrypted);
    39     }
    40     /**
    41      * Returns the length of the given string.
    42      * If available uses the multibyte string function mb_strlen.
    43      * @param string $string the string being measured for length
    44      * @return integer the length of the string
    45      */
    46     private static function strlen($string)
    47     {
    48         return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
    49     }
    50 
    51 
    52     /**
    53      * Returns the portion of string specified by the start and length parameters.
    54      * If available uses the multibyte string function mb_substr
    55      * @param string $string the input string. Must be one character or longer.
    56      * @param integer $start the starting position
    57      * @param integer $length the desired portion length
    58      * @return string the extracted part of string, or FALSE on failure or an empty string.
    59      */
    60     private static function substr($string,$start,$length)
    61     {
    62         return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
    63     }
    64     /**
    65      * This was AES-128 / CBC / PKCS5Padding
    66      * @param string $encrypted     base64_encode encrypted string
    67      * @param string $key
    68      * @throws CException
    69      * @return string
    70      */
    71     public static function AesDecrypt($encrypted, $key = null)
    72     {
    73         if ($encrypted == '') return '';
    74         $ciphertext_dec = base64_decode($encrypted);
    75         $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    76         $key=self::substr($key, 0, mcrypt_enc_get_key_size($module));
    77         $iv = $key;
    78         // str_repeat($key, 16);    //解密的初始化向量要和加密时一样。
    79         /* Initialize encryption module for decryption */
    80         mcrypt_generic_init($module, $key, $iv);
    81 
    82 
    83         /* Decrypt encrypted string */
    84         $decrypted = mdecrypt_generic($module, $ciphertext_dec);
    85 
    86         /* Terminate decryption handle and close module */
    87         mcrypt_generic_deinit($module);
    88         mcrypt_module_close($module);
    89         // echo $decrypted;die;
    90         //$a = rtrim($decrypted,"");
    91         return rtrim($decrypted,"");
    92     }
    93 }
  • 相关阅读:
    WebApp匯入資料之對話框實現
    JSON學習
    [JSON]jQuery的parseJSON()函數使用
    DataTable.Merge()一次應用
    WebApp匯入資料之From Excel
    含有HTML碼的欄位寫入資料庫處理方法
    访问Webservice错误
    ASP.NET整合應用實例後端Services合作
    强直性脊柱炎活动指数(ASDAS)在日常诊疗种评估生物制剂治疗患者的应用-来自葡萄牙登记系统风湿病患者
    脊柱炎症水平在疾病活动度高低不等的脊柱关节炎患者中表现相似-来自一项群体研究结果
  • 原文地址:https://www.cnblogs.com/lixiansen/p/9579165.html
Copyright © 2011-2022 走看看