Php source code analysis-DZX1.5 encryption and decryption function authcode usage-PHP Tutorial

Source: Internet
Author: User
Php source code analysis-DZX1.5 encryption and decryption function authcode usage. Php source code analysis-DZX1.5 encryption and decryption function authcode usage this article describes the php source code analysis-DZX1.5 encryption and decryption function authcode usage. Share it with you for your reference. Analysis of php source code analysis-DZX1.5 encryption and decryption function authcode usage

This article describes how to use the authcode function of DZX1.5 encryption and decryption in php source code analysis. Share it with you for your reference. The specific analysis is as follows:

?

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

$ Authkey = '';

/**

* @ Param string $ string: Enter the plaintext (or ciphertext) to be encrypted (or decrypted)

* @ Param string $ operation: 'decode' or others. by default, this parameter indicates decryption, and other parameters include encryption.

* @ Param string $ key: encryption/decryption key

* @ Param int $ expiry: validity period

*/

Function authcode ($ string, $ operation = 'Decode', $ key = '', $ expiry = 0 ){

Global $ authkey;

$ Ckey_length = 4;

$ Key = md5 ($ key! = ''? $ Key: $ authkey );

$ Keya = md5 (substr ($ key, 0, 16 ));

$ Keyb = md5 (substr ($ key, 16, 16 ));

$ Keyc = $ ckey_length? ($ Operation = 'decode '? Substr ($ string, 0, $ ckey_length): substr (md5 (microtime (),-$ ckey_length )):'';

$ Cryptkey = $ keya. md5 ($ keya. $ keyc );

$ Key_length = strlen ($ cryptkey );

$ String = $ operation = 'decode '? Base64_decode (substr ($ string, $ ckey_length): sprintf ('% 010d', $ expiry? $ Expiry + time (): 0). substr (md5 ($ string. $ keyb), 0, 16). $ string;

$ String_length = strlen ($ string );

$ Result = '';

$ Box = range (1, 0,255 );

$ Rndkey = array ();

For ($ I = 0; $ I <= 255; $ I ++ ){

$ Rndkey [$ I] = ord ($ cryptkey [$ I % $ key_length]);

}

For ($ j = $ I = 0; I I <256; $ I ++ ){

$ J = ($ j + $ box [$ I] + $ rndkey [$ I]) % 256;

$ Tmp = $ box [$ I];

$ Box [$ I] = $ box [$ j];

$ Box [$ j] = $ tmp;

}

For ($ a = $ j = $ I = 0; $ I <$ string_length; $ I ++ ){

$ A = ($ a + 1) % 256;

$ J = ($ j + $ box [$ a]) % 256;

$ Tmp = $ box [$ a];

$ Box [$ a] = $ box [$ j];

$ Box [$ j] = $ tmp;

$ Result. = chr (ord ($ string [$ I]) ^ ($ box [($ box [$ a] + $ box [$ j]) % 256]);

}

If ($ operation = 'decode '){

If (substr ($ result, 0, 10) = 0 | substr ($ result, 0, 10)-time ()> 0) & substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ keyb), 0, 16 )){

Return substr ($ result, 26 );

} Else {

Return '';

}

} Else {

Return $ keyc. str_replace ('=', '', base64_encode ($ result ));

}

}

$ Str = '000000 ';

$ Key = '000000 ';

Echo "Plaintext:". $ str;

Echo"
";

Echo "key:". $ key;

$ Encode = authcode ($ str, '11', $ key );

Echo"
";

Echo "encrypted ciphertext:". $ encode;

Echo"
";

$ Decode = authcode ($ encode, 'Decode', $ key );

Echo "decrypted plaintext:". $ decode;

/* End of php */

The running result is as follows:

Plaintext: 1234

Secret key: 1234

Encrypted ciphertext: a52f67eXZGVy0HtQBo4vOREhq3WxnL6E2zlx75JGfoJW

Decrypted plaintext: 1234

I hope this article will help you with php programming.

Examples in this article describes the usage of DZX1.5 encryption and decryption function authcode in php source code analysis. Share it with you for your reference. Specific analysis...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.