当前位置:Gxlcms > PHP教程 > javascript-aes128加密一些相关问题

javascript-aes128加密一些相关问题

时间:2021-07-01 10:21:17 帮助过:9人阅读

class aes {

  1. <code>const KEY = "625202f9149e061d";
  2. const IV = "5efd3f6060e20330";
  3. /**</code>
    • pkcs7补码

    • @param string $string 明文

    • @param int $blocksize Blocksize , 以 byte 为单位

    • @return String
      */

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

      1. <code> $len = strlen($string); //取得字符串长度
      2. $pad = $blocksize - ($len % $blocksize); //取得补码的长度
      3. $string .= str_repeat(chr($pad), $pad); //用ASCII码为补码长度的字符, 补足最后一段
      4. return $string;</code>

      }

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

      1. <code> $base = (mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $this->addPkcs7Padding($str, 16), MCRYPT_MODE_CBC, $iv));
      2. return $this->strToHex($base);</code>

      }

    3. strToHex($string) {//字符串转十六进制

      1. <code> $hex = "";
      2. $tmp = "";
      3. for ($i = 0; $i < strlen($string); $i++) {
      4. $tmp = dechex(ord($string[$i]));
      5. $hex.= strlen($tmp) == 1 ? "0" . $tmp : $tmp;
      6. }
      7. $hex = strtoupper($hex);
      8. return $hex;</code>

      }
      aes加密 在网上找的代码有PHP 和 android 的 来位好心人讲讲$aes = new aes(); $aes->aes128cbcEncrypt('token');这个加密过程的每一步呗 或者帮忙写一份对应的js版本的 = =

    回复内容:

    class aes {

    1. <code>const KEY = "625202f9149e061d";
    2. const IV = "5efd3f6060e20330";
    3. /**</code>
    • pkcs7补码

    • @param string $string 明文

    • @param int $blocksize Blocksize , 以 byte 为单位

    • @return String
      */

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

      1. <code> $len = strlen($string); //取得字符串长度
      2. $pad = $blocksize - ($len % $blocksize); //取得补码的长度
      3. $string .= str_repeat(chr($pad), $pad); //用ASCII码为补码长度的字符, 补足最后一段
      4. return $string;</code>

      }

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

      1. <code> $base = (mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $this->addPkcs7Padding($str, 16), MCRYPT_MODE_CBC, $iv));
      2. return $this->strToHex($base);</code>

      }

    3. strToHex($string) {//字符串转十六进制

      1. <code> $hex = "";
      2. $tmp = "";
      3. for ($i = 0; $i < strlen($string); $i++) {
      4. $tmp = dechex(ord($string[$i]));
      5. $hex.= strlen($tmp) == 1 ? "0" . $tmp : $tmp;
      6. }
      7. $hex = strtoupper($hex);
      8. return $hex;</code>

      }
      aes加密 在网上找的代码有PHP 和 android 的 来位好心人讲讲$aes = new aes(); $aes->aes128cbcEncrypt('token');这个加密过程的每一步呗 或者帮忙写一份对应的js版本的 = =

    说起来挺复杂的,你看看这个链接吧。
    http://yinghuayuan8866.blog.163.com/blog/static/2245702720121225658625/

    人气教程排行