Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write the Code of PHP reversible encryption and decryption algorithm

Shulou Source: shulou.com Published: 2022-06-03 07:45:46 10月04日 Update

This article focuses on "how to write PHP reversible encryption and decryption algorithm code", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn how to write PHP reversible encryption and decryption algorithm code!

For most password encryption, we can use md5, sha1 and other methods. Data leakage can be effectively prevented, but these methods are only applicable to data encryption that does not need to be restored.

For the information that needs to be restored, a reversible encryption and decryption algorithm is needed.

The following set of PHP functions is the way to achieve this encryption and decryption:

The encryption algorithm is as follows:

The copy code is as follows:

Function encrypt ($data, $key)

{

$key = md5 ($key)

$x = 0

$len = strlen ($data)

$l = strlen ($key)

For ($I = 0; $I < $len; $iTunes +)

{

If ($x = = $l)

{

$x = 0

}

$char. = $key {$x}

$xbirthday +

}

For ($I = 0; $I < $len; $iTunes +)

{

$str. = chr (ord ($data {$I}) + (ord ($char {$I})) 256)

}

Return base64_encode ($str)

}

The decryption algorithm is as follows:

The copy 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; $iTunes +)

{

If ($x = = $l)

{

$x = 0

}

$char. = substr ($key, $x, 1)

$xbirthday +

}

For ($I = 0; $I < $len; $iTunes +)

{

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

}

The above encryption and decryption process requires an encryption key (that is, the parameter $key).

The copy code is as follows:

$data = 'PHP encryption and decryption algorithm'; / / encrypted information

$key = '123 keys; / / key

$encrypt = encrypt ($data, $key)

$decrypt = decrypt ($encrypt, $key)

Echo $encrypt, "\ n", $decrypt

GniCSOzZG+HnS9zcFea7SefNGhXF

PHP encryption and decryption algorithm

At this point, I believe that everyone on "how to write PHP reversible encryption and decryption algorithm code" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Encryption code encryption and decryption algorithm method information content key data learning practical deeper effective interest function parameter most practical practical password Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux vpn Shulou Tech Info Huawei