php中加密解密DES类的简单使用方法示例

2025-05-29 0 38

本文实例讲述了php加密解密des类的简单使用方法。分享给大家供大家参考,具体如下:

在平时的开发工作中,我们经常会对关键字符进行加密,可能为了安全 也可能为了规范,所以要正确使用des加密解密

php中加密解密DES类的简单使用方法示例

代码1:

?

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
class des

{

var $key; // 密钥

var $iv; // 偏移量

function __construct( $key, $iv=0 ) {

$this->key = $key;

if( $iv == 0 ) {

$this->iv = $key;

} else {

$this->iv = $iv;

// 创建初始向量, 并且检测密钥长度, windows 平台请使用 mcrypt_rand

// mcrypt_create_iv ( mcrypt_get_block_size (mcrypt_des, mcrypt_mode_cbc), mcrypt_dev_random );

}

}

function encrypt($str) {

//加密,返回大写十六进制字符串

$size = mcrypt_get_block_size ( mcrypt_des, mcrypt_mode_cbc );

$str = $this->pkcs5pad ( $str, $size );

// bin2hex 把 ascii 字符的字符串转换为十六进制值

return strtoupper( bin2hex( mcrypt_cbc(mcrypt_des, $this->key, $str, mcrypt_encrypt, $this->iv ) ) );

}

function decrypt($str) {

//解密

$strbin = $this->hex2bin( strtolower( $str ) );

$str = mcrypt_cbc( mcrypt_des, $this->key, $strbin, mcrypt_decrypt, $this->iv );

$str = $this->pkcs5unpad( $str );

return $str;

}

function hex2bin($hexdata) {

$bindata = "";

for($i = 0; $i < strlen ( $hexdata ); $i += 2) {

$bindata .= chr ( hexdec ( substr ( $hexdata, $i, 2 ) ) );

}

return $bindata;

}

function pkcs5pad($text, $blocksize) {

$pad = $blocksize - (strlen ( $text ) % $blocksize);

return $text . str_repeat ( chr ( $pad ), $pad );

}

function pkcs5unpad($text) {

$pad = ord ( $text {strlen ( $text ) - 1} );

if ($pad > strlen ( $text ))

return false;

if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad)

return false;

return substr ( $text, 0, - 1 * $pad );

}

}

deprecated: methods with the same name as their class will not be constructors in a future version of php; des5 has a deprecated constructor in d:\\phpstudy_pro\\www\\des\\des5.php on line 2

fatal error: uncaught error: call to undefined function mcrypt_get_block_size() in d:\\phpstudy_pro\\www\\des\\des5.php:19 stack trace: #0 d:\\phpstudy_pro\\www\\des\\1.php(10): des5->encrypt('podsmia') #1 {main} thrown in d:\\phpstudy_pro\\www\\des\\des5.php on line 19

  • mcrypt_cbc 以 cbc 模式加解密数据, 在php 5.5.0+被弃用, php 7.0.0被移除
  • mcrypt_encrypt / mcrypt_decrypt 使用给定参数加密 / 解密, 在php 7.1.0+被弃用, 在php 7.2.0+被移除

代码2:

?

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
class des7

{

//要改的加密, 使用 openssl

public function desencrypt($str,$key) {

$iv = $key;

$data = openssl_encrypt($str,"des-cbc",$key,openssl_raw_data,$iv);

$data = strtolower(bin2hex($data));

return $data;

}

//要改的解密

public function desdecrypt($str,$key) {

$iv = $key;

return openssl_decrypt (hex2bin($str), 'des-cbc', $key, openssl_raw_data,$iv);

}

}

希望本文所述对大家PHP程序设计有所帮助。

原文链接:https://blog.csdn.net/qq_36261130/article/details/104015050

收藏 (0) 打赏

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

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

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

快网idc优惠网 建站教程 php中加密解密DES类的简单使用方法示例 https://www.kuaiidc.com/91225.html

相关文章

发表评论
暂无评论