javascript - aes128加密一些相關問題

來源:互聯網
上載者:User
class aes {

const KEY = "625202f9149e061d";const IV = "5efd3f6060e20330";/**
    • pkcs7補碼

    • @param string $string 明文

    • @param int $blocksize Blocksize , 以 byte 為單位

    • @return String
      */

    1. function addPkcs7Padding($string, $blocksize = 32) {

         $len = strlen($string); //取得字串長度   $pad = $blocksize - ($len % $blocksize); //取得補碼的長度   $string .= str_repeat(chr($pad), $pad); //用ASCII碼為補碼長度的字元, 補足最後一段   return $string;

      }

    2. aes128cbcEncrypt($str, $iv = self::IV, $key = self::KEY) { // $this->addPkcs7Padding($str,16)

         $base = (mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $this->addPkcs7Padding($str, 16), MCRYPT_MODE_CBC, $iv));   return $this->strToHex($base);

      }

    3. strToHex($string) {//字串轉十六進位

         $hex = "";   $tmp = "";   for ($i = 0; $i < strlen($string); $i++) {       $tmp = dechex(ord($string[$i]));       $hex.= strlen($tmp) == 1 ? "0" . $tmp : $tmp;   }   $hex = strtoupper($hex);   return $hex;

      }
      aes加密 在網上找的代碼有PHP 和 android 的 來位好心人講講$aes = new aes(); $aes->aes128cbcEncrypt('token');這個加密過程的每一步唄 或者幫忙寫一份對應的js版本的 = =

    回複內容:

    class aes {

    const KEY = "625202f9149e061d";const IV = "5efd3f6060e20330";/**
    • pkcs7補碼

    • @param string $string 明文

    • @param int $blocksize Blocksize , 以 byte 為單位

    • @return String
      */

    1. function addPkcs7Padding($string, $blocksize = 32) {

         $len = strlen($string); //取得字串長度   $pad = $blocksize - ($len % $blocksize); //取得補碼的長度   $string .= str_repeat(chr($pad), $pad); //用ASCII碼為補碼長度的字元, 補足最後一段   return $string;

      }

    2. aes128cbcEncrypt($str, $iv = self::IV, $key = self::KEY) { // $this->addPkcs7Padding($str,16)

         $base = (mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $this->addPkcs7Padding($str, 16), MCRYPT_MODE_CBC, $iv));   return $this->strToHex($base);

      }

    3. strToHex($string) {//字串轉十六進位

         $hex = "";   $tmp = "";   for ($i = 0; $i < strlen($string); $i++) {       $tmp = dechex(ord($string[$i]));       $hex.= strlen($tmp) == 1 ? "0" . $tmp : $tmp;   }   $hex = strtoupper($hex);   return $hex;

      }
      aes加密 在網上找的代碼有PHP 和 android 的 來位好心人講講$aes = new aes(); $aes->aes128cbcEncrypt('token');這個加密過程的每一步唄 或者幫忙寫一份對應的js版本的 = =

    說起來挺複雜的,你看看這個連結吧。
    http://yinghuayuan8866.blog.163.com/blog/static/2245702720121225658625/

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.