Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report