使用AES进行加密


<?php
namespace app\common\lib;

/**

  • aes 加密 解密类库

  • @by singwa

  • Class Aes

  • @package app\common\lib
    */
    class Aes {

    private $key = null;

    /**
    *

    • @param $key 密钥
    • @return String
      */
      public function __construct() {
      // 需要小伙伴在配置文件app.php中定义aeskey
      $this->key = config(‘app.aes_key’);
      }

    /**

    • 加密

    • @param String input 加密的字符串

    • @param String key 解密的key

    • @return HexString
      */
      public function encrypt($input = ‘’) {
      $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
      $input = t h i s − &gt; p k c s 5 p a d ( this-&gt;pkcs5_pad( this>pkcs5pad(input, $size);
      $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, ‘’, MCRYPT_MODE_ECB, ‘’);
      i v = m c r y p t c r e a t e i v ( m c r y p t e n c g e t i v s i z e ( iv = mcrypt_create_iv(mcrypt_enc_get_iv_size( iv=mcryptcreateiv(mcryptencgetivsize(td), MCRYPT_RAND);
      mcrypt_generic_init($td, $this->key, $iv);

      d a t a = m c r y p t g e n e r i c ( data = mcrypt_generic( data=mcryptgeneric(td, i n p u t ) ; m c r y p t g e n e r i c d e i n i t ( input); mcrypt_generic_deinit( input);mcryptgenericdeinit(td);
      mcrypt_module_close($td);
      d a t a = b a s e 6 4 e n c o d e ( data = base64_encode( data=base64encode(data);

      return $data;

    }
    /**

    • 填充方式 pkcs5
    • @param String text 原始字符串
    • @param String blocksize 加密长度
    • @return String
      */
      private function pkcs5_pad($text, $blocksize) {
      $pad = b l o c k s i z e − ( s t r l e n ( blocksize - (strlen( blocksize(strlen(text) % $blocksize);
      return t e x t . s t r r e p e a t ( c h r ( text . str_repeat(chr( text.strrepeat(chr(pad), $pad);
      }

    /**

    • 解密

    • @param String input 解密的字符串

    • @param String key 解密的key

    • @return String
      */
      public function decrypt($sStr) {
      d e c r y p t e d = m c r y p t d e c r y p t ( M C R Y P T R I J N D A E L 1 28 , decrypted= mcrypt_decrypt(MCRYPT_RIJNDAEL_128, decrypted=mcryptdecrypt(MCRYPTRIJNDAEL128,this->key,base64_decode($sStr), MCRYPT_MODE_ECB);
      d e c s = s t r l e n ( dec_s = strlen( decs=strlen(decrypted);
      p a d d i n g = o r d ( padding = ord( padding=ord(decrypted[$dec_s-1]);
      d e c r y p t e d = s u b s t r ( decrypted = substr( decrypted=substr(decrypted, 0, -$padding);

      return $decrypted;
      }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值