This article mainly introduces a set of PHP reversible encryption and decryption algorithm instance code. if you need it, you can refer to the md5, sha1, and other methods for most password encryption. This can effectively prevent data leaks, but these methods only apply to data encryption that does not require restoration.
For information to be restored, a reversible encryption and decryption algorithm is required.
The following PHP functions implement encryption and decryption:
The encryption algorithm is as follows:
The code is as follows:
Function encrypt ($ data, $ key)
{
$ Key = md5 ($ key );
$ X = 0;
$ Len = strlen ($ data );
$ L = strlen ($ key );
For ($ I = 0; $ I <$ len; $ I ++)
{
If ($ x = $ l)
{
$ X = 0;
}
$ Char. = $ key {$ x };
$ X ++;
}
For ($ I = 0; $ I <$ len; $ I ++)
{
$ Str. = chr (ord ($ data {$ I}) + (ord ($ char {$ I}) % 256 );
}
Return base64_encode ($ str );
}
The decryption algorithm is as follows:
The code is as follows:
Function decrypt ($ data, $ key)
{
$ Key = md5 ($ key );
$ X = 0;
$ Data = base64_decode ($ data );
$ Len = strlen ($ data );
$ L = strlen ($ key );
For ($ I = 0; $ I <$ len; $ I ++)
{
If ($ x = $ l)
{
$ X = 0;
}
$ Char. = substr ($ key, $ x, 1 );
$ X ++;
}
For ($ I = 0; $ I <$ len; $ I ++)
{
If (ord (substr ($ data, $ I, 1) <ord (substr ($ char, $ I, 1 )))
{
$ Str. = chr (ord (substr ($ data, $ I, 1) + 256)-ord (substr ($ char, $ I, 1 )));
}
Else
{
$ Str. = chr (ord (substr ($ data, $ I, 1)-ord (substr ($ char, $ I, 1 )));
}
}
Return $ str;
}
An encryption key ($ key) is required for the above encryption and decryption process ).
The code is as follows:
$ Data = 'php encryption and decryption algorithmic '; // encrypted information
$ Key = '000000'; // key
$ Encrypt = encrypt ($ data, $ key );
$ Decrypt = decrypt ($ encrypt, $ key );
Echo $ encrypt, "\ n", $ decrypt;
The output is similar to the following:
GniCSOzZG + HnS9zcFea7SefNGhXF
PHP encryption and decryption algorithm
From the above results, we can see that this is a set of reversible encryption and decryption algorithms that can be used to encrypt part of the data to be restored.