PHP encryption and decryption string Summary

Source: Internet
Author: User

PHP encryption and decryption string Summary

PHP encryption and decryption functions, one is Discuz! The authcode encryption function (with detailed decomposition), one is the encrypt () function. You need to use PHP to encrypt specific information, that is, to generate an encrypted string through the encryption algorithm. The encrypted string can be decrypted through the decryption algorithm, this allows the program to process the decrypted information.

In projects, we sometimes need to use PHP to encrypt specific information, that is, to generate an encrypted string through the encryption algorithm. The encrypted string can be decrypted through the decryption algorithm, this allows the program to process the decrypted information.

The most common applications are user logon and some API data exchange scenarios.

I have collected some classic PHP encryption and decryption function code and shared it with you. Encryption and decryption principles are generally based on a certain number of encryption and decryption algorithms, add the key to the algorithm, and finally obtain the encryption and decryption results.

1. Very powerful authcode encryption function, Discuz! Classic code (detailed description ):

?

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

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

// The length of the dynamic key. Different ciphertext values are generated for the same plaintext based on the dynamic key.

$ Ckey_length = 4;

 

// Key

$ Key = md5 ($ key? $ Key: $ GLOBALS ['discuz _ auth_key ']);

 

// Key a is used for encryption and decryption.

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

// Key B is used for data integrity verification.

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

// Key c is used to change the generated ciphertext

$ Keyc = $ ckey_length? ($ Operation = 'decode '? Substr ($ string, 0, $ ckey_length ):

Substr (md5 (microtime (),-$ ckey_length )):'';

// Key used for calculation

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

$ Key_length = strlen ($ cryptkey );

// Plaintext. The first 10 digits are used to save the timestamp, verify the data validity during decryption, and the 10 to 26 digits are used to save $ keyb (Key B ),

// This key is used to verify data integrity during decryption.

// If it is decoded, it starts from the $ ckey_length bit, because the $ ckey_length bit before the ciphertext stores the dynamic key to ensure correct decryption.

$ 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 ();

// Generate a key book

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

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

}

// Use a fixed algorithm to disrupt the key book and increase randomness. It seems complicated. In fact, it does not increase the ciphertext strength.

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;

}

// Core encryption/Decryption part

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;

// The keys obtained from the key book are different or converted into characters.

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

}

If ($ operation = 'decode '){

// Verify the data validity. See the unencrypted plaintext format.

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 {

// Save the dynamic key in the ciphertext, Which is why different ciphertext can be decrypted in the same plain text.

// Because the encrypted ciphertext may be special characters, the copying process may be lost, so it is base64-encoded.

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

}

}

$ String: string, plaintext or ciphertext in the authcode ($ string, $ operation, $ key, $ expiry) function; $ operation: DECODE indicates decryption; others indicate encryption; $ key: key; $ expiry: ciphertext validity period.

Usage:

?

1

2

3

4

5

$ Str = 'abcdef ';

$ Key = 'www .helloweba.com ';

Echo authcode ($ str, 'encoding', $ key, 0); // Encryption

$ Str = '56f4yer1di2wtzwmqsfpps9hwyojnfp2mpc8sohrrxo7bok ';

Echo authcode ($ str, 'decode', $ key, 0); // decrypt

2. encryption and decryption function encrypt ():

?

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

Function encrypt ($ string, $ operation, $ key = ''){

$ Key = md5 ($ key );

$ Key_length = strlen ($ key );

$ String = $ operation = 'd '? Base64_decode ($ string): substr (md5 ($ string. $ key), 0, 8). $ string;

$ String_length = strlen ($ string );

$ Rndkey = $ box = array ();

$ Result = '';

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

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

$ Box [$ I] = $ I;

}

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 = 'D '){

If (substr ($ result,) = substr (md5 (substr ($ result, 8). $ key )){

Return substr ($ result, 8 );

} Else {

Return '';

}

} Else {

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

}

}

$ String in the encrypt ($ string, $ operation, $ key) function: the string to be encrypted and decrypted; $ operation: determines whether the string is encrypted or decrypted. E Indicates encryption, and D indicates decryption; $ key: The key.

Usage:

?

1

2

3

4

5

$ Str = 'abc ';

$ Key = 'www .helloweba.com ';

$ Token = encrypt ($ str, 'E', $ key );

Echo 'encryption: '. encrypt ($ str, 'E', $ key );

Echo 'decryption: '. encrypt ($ str, 'D', $ key );

The above is all the content of this article. I hope you will like it.

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.