php写的AES加密解密类分享

2025-05-29 0 33

今天写了一个phpAES加密类。适用于Yii的扩展。
如果不用在Yii框架中,把代码中Yii::app()->params['encryptKey'] 换成你对应的默认key就可以了。
类代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100
<?php

/**

* php AES加解密

* 如果要与java共用,则密钥长度应该为16位长度

* 因为java只支持128位加密,所以php也用128位加密,可以与java互转。

* 同时AES的标准也是128位。只是RIJNDAEL算法可以支持128,192和256位加密。

* java 要使用AES/CBC/NoPadding标准来加解密

*

* @author Terry

*

*/

class PhpAes

{

/**

* This was AES-128 / CBC / NoPadding encrypted.

* return base64_encode string

* @author Terry

* @param string $plaintext

* @param string $key

*/

public static function AesEncrypt($plaintext,$key = null)

{

$plaintext = trim($plaintext);

if ($plaintext == '') return '';

if(!extension_loaded('mcrypt'))

throw new CException(Yii::t('yii','AesEncrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));

$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);

$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

$key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module));

/* Create the IV and determine the keysize length, use MCRYPT_RAND

* on Windows instead */

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

/* Intialize encryption */

mcrypt_generic_init($module, $key, $iv);

/* Encrypt data */

$encrypted = mcrypt_generic($module, $plaintext);

/* Terminate encryption handler */

mcrypt_generic_deinit($module);

mcrypt_module_close($module);

return base64_encode($encrypted);

}

/**

* This was AES-128 / CBC / NoPadding decrypted.

* @author Terry

* @param string $encrypted base64_encode encrypted string

* @param string $key

* @throws CException

* @return string

*/

public static function AesDecrypt($encrypted, $key = null)

{

if ($encrypted == '') return '';

if(!extension_loaded('mcrypt'))

throw new CException(Yii::t('yii','AesDecrypt requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));

$ciphertext_dec = base64_decode($encrypted);

$module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');

$key=self::substr($key===null ? Yii::app()->params['encryptKey'] : $key, 0, mcrypt_enc_get_key_size($module));

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

/* Initialize encryption module for decryption */

mcrypt_generic_init($module, $key, $iv);

/* Decrypt encrypted string */

$decrypted = mdecrypt_generic($module, $ciphertext_dec);

/* Terminate decryption handle and close module */

mcrypt_generic_deinit($module);

mcrypt_module_close($module);

return rtrim($decrypted,"\\0");

}

/**

* Returns the length of the given string.

* If available uses the multibyte string function mb_strlen.

* @param string $string the string being measured for length

* @return integer the length of the string

*/

private static function strlen($string)

{

return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);

}

/**

* Returns the portion of string specified by the start and length parameters.

* If available uses the multibyte string function mb_substr

* @param string $string the input string. Must be one character or longer.

* @param integer $start the starting position

* @param integer $length the desired portion length

* @return string the extracted part of string, or FALSE on failure or an empty string.

*/

private static function substr($string,$start,$length)

{

return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);

}

}

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 php写的AES加密解密类分享 https://www.kuaiidc.com/104407.html

相关文章

发表评论
暂无评论