encode() public method

对需要加密的明文进行填充补位
public encode ( $text ) : 补齐明文字符串
$text 需要进行填充补位操作的明文
return 补齐明文字符串
Example #1
0
 /**
  * 对明文进行加密
  * @param string $text 需要加密的明文
  * @return string 加密后的密文
  */
 public function encrypt($text, $appid)
 {
     try {
         //获得16位随机字符串,填充到明文之前
         $random = $this->getRandomStr();
         $text = $random . pack("N", strlen($text)) . $text . $appid;
         // 网络字节序
         $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
         $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
         $iv = substr($this->key, 0, 16);
         //使用自定义的填充方式对明文进行补位填充
         $pkc_encoder = new PKCS7Encoder();
         $text = $pkc_encoder->encode($text);
         mcrypt_generic_init($module, $this->key, $iv);
         //加密
         $encrypted = mcrypt_generic($module, $text);
         mcrypt_generic_deinit($module);
         mcrypt_module_close($module);
         //print(base64_encode($encrypted));
         //使用BASE64对加密后的字符串进行编码
         return array(ErrorCode::$OK, base64_encode($encrypted));
     } catch (Exception $e) {
         print $e;
         return array(ErrorCode::$EncryptAESError, null);
     }
 }
Example #2
0
 /**
  * 对明文进行加密
  *
  * @param string $text 需要加密的明文
  * @param string $corp_id
  * @return string 加密后的密文
  */
 public function encrypt($text, $corp_id)
 {
     try {
         //获得16位随机字符串,填充到明文之前
         $random = $this->getRandomStr();
         $text = $random . pack('N', strlen($text)) . $text . $corp_id;
         //使用自定义的填充方式对明文进行补位填充
         $text = PKCS7Encoder::encode($text);
         $iv = substr($this->_key, 0, self::INIT_VECTOR_SIZE);
         $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->_key, $text, MCRYPT_MODE_CBC, $iv);
         return base64_encode($encrypted);
     } catch (\Exception $e) {
         return false;
     }
 }
Example #3
0
 public function encrypt($text, $appid)
 {
     try {
         $random = $this->getRandomStr();
         $text = $random . pack('N', strlen($text)) . $text . $appid;
         $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
         $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
         $iv = substr($this->key, 0, 16);
         $pkc_encoder = new PKCS7Encoder();
         $text = $pkc_encoder->encode($text);
         mcrypt_generic_init($module, $this->key, $iv);
         $encrypted = mcrypt_generic($module, $text);
         mcrypt_generic_deinit($module);
         mcrypt_module_close($module);
         return array(ErrorCode::$OK, base64_encode($encrypted));
     } catch (Exception $e) {
         print $e;
         return array(ErrorCode::$EncryptAESError, NULL);
     }
 }
Example #4
0
 /**
  * 对明文进行加密
  * @param string $text 需要加密的明文
  * @return string 加密后的密文
  */
 public function encrypt($text, $no)
 {
     try {
         //获得16位随机字符串,填充到明文之前
         $random = $this->getRandomStr(16);
         $text = $random . pack("N", strlen($text)) . $text . $no;
         // 网络字节序
         $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
         $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
         $iv = substr($this->key, 0, 16);
         //使用自定义的填充方式对明文进行补位填充
         $pkc_encoder = new PKCS7Encoder();
         $text = $pkc_encoder->encode($text);
         mcrypt_generic_init($module, $this->key, $iv);
         //加密
         $encrypted = mcrypt_generic($module, $text);
         mcrypt_generic_deinit($module);
         mcrypt_module_close($module);
         $base64_encrypted = base64_encode($encrypted);
         return $base64_encrypted;
     } catch (Exception $e) {
         return null;
     }
 }
Example #5
0
 /**
  * 对明文进行加密
  * @param string $text 需要加密的明文
  * @return string 加密后的密文
  */
 public function encrypt($text, $corpid)
 {
     try {
         //获得16位随机字符串,填充到明文之前
         $random = $this->getRandomStr();
         $text = $random . pack("N", strlen($text)) . $text . $corpid;
         // 网络字节序
         $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
         $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
         $iv = substr($this->key, 0, 16);
         //使用自定义的填充方式对明文进行补位填充
         $pkc_encoder = new PKCS7Encoder();
         $text = $pkc_encoder->encode($text);
         mcrypt_generic_init($module, $this->key, $iv);
         //加密
         $encrypted = mcrypt_generic($module, $text);
         mcrypt_generic_deinit($module);
         mcrypt_module_close($module);
         //print(base64_encode($encrypted));
         //使用BASE64对加密后的字符串进行编码
         return base64_encode($encrypted);
     } catch (Exception $e) {
         $this->err = new PrpcryptError($e->getCode(), $e->getMessage(), $e->getLine(), $e->getFile(), $e->getTrace());
         return null;
     }
 }