Chinaunix首页 | 论坛 | 博客
  • 博客访问: 446813
  • 博文数量: 141
  • 博客积分: 211
  • 博客等级: 入伍新兵
  • 技术积分: 1049
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-17 16:25
个人简介

如此经年,望尽千帆。

文章分类

全部博文(141)

文章存档

2014年(73)

2013年(65)

2012年(3)

我的朋友

分类: PHP

2014-06-20 11:49:45

php写的AES加密解密类,实际是为YII框架写的,不在YII框架时只需替换其中的两句代码即可使用。
今天写了一个php的AES加密类。适用于Yii的扩展。
如果不用在Yii框架中,把代码中Yii::app()->params['encryptKey'] 换成你对应的默认key就可以了。

您可能感兴趣的文章:

    类代码:

    1. <?php
    2. /**
    3.  * php AES加解密类
    4.  * 如果要与java共用,则密钥长度应该为16位长度
    5.  * 因为java只支持128位加密,所以php也用128位加密,可以与java互转。
    6.  * 同时AES的标准也是128位。只是RIJNDAEL算法可以支持128,192和256位加密。
    7.  * java 要使用AES/CBC/NoPadding标准来加解密
    8.  *
    9.  * @author Terry
    10.  *
    11.  */
    12. class PhpAes
    13. {
    14. /**
    15. * This was AES-128 / CBC / NoPadding encrypted.
    16. * return base64_encode string
    17. * @author Terry
    18. * @param string $plaintext
    19. * @param string $key
    20. */
    21. public static function AesEncrypt($plaintext,$key = null)
    22. {
    23. $plaintext = trim($plaintext);
    24. if ($plaintext == '') return '';
    25. if(!extension_loaded('mcrypt'))
    26. throw new CException(Yii::t('yii','AesEncrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));
    27. $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    28. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    29. $key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module));
    30. /* Create the IV and determine the keysize length, use MCRYPT_RAND
    31. * on Windows instead */
    32. $iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));
    33. /* Intialize encryption */
    34. mcrypt_generic_init($module, $key, $iv);


    35. /* Encrypt data */
    36. $encrypted = mcrypt_generic($module, $plaintext);


    37. /* Terminate encryption handler */
    38. mcrypt_generic_deinit($module);
    39. mcrypt_module_close($module);
    40. return base64_encode($encrypted);
    41. }


    42. /**
    43. * This was AES-128 / CBC / NoPadding decrypted.
    44. * @author Terry
    45. * @param string $encrypted     base64_encode encrypted string
    46. * @param string $key
    47. * @throws CException
    48. * @return string
    49. */
    50. public static function AesDecrypt($encrypted, $key = null)
    51. {
    52. if ($encrypted == '') return '';
    53. if(!extension_loaded('mcrypt'))
    54. throw new CException(Yii::t('yii','AesDecrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));


    55. $ciphertext_dec = base64_decode($encrypted);
    56. $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
    57. $key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module));


    58. $iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));


    59. /* Initialize encryption module for decryption */
    60. mcrypt_generic_init($module, $key, $iv);

    61. ()
    62. /* Decrypt encrypted string */
    63. $decrypted = mdecrypt_generic($module, $ciphertext_dec);


    64. /* Terminate decryption handle and close module */
    65. mcrypt_generic_deinit($module);
    66. mcrypt_module_close($module);
    67. return rtrim($decrypted,"\0");
    68. }


    69. /**
    70. * Returns the length of the given string.
    71. * If available uses the multibyte string function mb_strlen.
    72. * @param string $string the string being measured for length
    73. * @return integer the length of the string
    74. */
    75. private static function strlen($string)
    76. {
    77. return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
    78. }


    79. /**
    80. * Returns the portion of string specified by the start and length parameters.
    81. * If available uses the multibyte string function mb_substr
    82. * @param string $string the input string. Must be one character or longer.
    83. * @param integer $start the starting position
    84. * @param integer $length the desired portion length
    85. * @return string the extracted part of string, or FALSE on failure or an empty string.
    86. */
    87. private static function substr($string,$start,$length)
    88. {
    89. return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
    90. }
    91. }


    阅读(1424) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~