Home  >  Article  >  Backend Development  >  javascript - Some issues related to aes128 encryption

javascript - Some issues related to aes128 encryption

WBOY
WBOYOriginal
2016-08-04 09:21:431272browse

class aes {

<code>const KEY = "625202f9149e061d";
const IV = "5efd3f6060e20330";

/**</code>
    • pkcs7 complement code

    • @param string $string plain text

    • @param int $blocksize Blocksize , in byte

    • @return String
      */

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

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

      }

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

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

      }

    3. strToHex($string) {//Convert string to hexadecimal

      <code>   $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;</code>

      }
      Aes encryption codes found online include PHP and android. Can a kind person tell me about each step of this encryption process? $aes = new aes(); $aes->aes128cbcEncrypt('token'); or help. Write a corresponding js version = =

    Reply content:

    class aes {

    <code>const KEY = "625202f9149e061d";
    const IV = "5efd3f6060e20330";
    
    /**</code>
    • pkcs7 complement code

    • @param string $string plain text

    • @param int $blocksize Blocksize , in byte

    • @return String
      */

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

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

      }

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

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

      }

    3. strToHex($string) {//Convert string to hexadecimal

      <code>   $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;</code>

      }
      Aes encryption codes found online include PHP and android. Can a kind person tell me about each step of this encryption process? $aes = new aes(); $aes->aes128cbcEncrypt('token'); or help. Write a corresponding js version = =

    It sounds quite complicated, just take a look at this link.
    http://yinghuayuan8866.blog.163.com/blog/static/2245702720121225658625/

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn