Php implements rc4 encryption algorithm code

Source: Internet
Author: User
The decryption method of this algorithm is to re-encrypt it once to restore it. if you need it, refer to the following code:
Copy codeThe code is as follows:
/*
* Rc4 encryption algorithm
* $ Pwd key
* $ Data the data to be encrypted
*/
Function rc4 ($ pwd, $ data) // $ pwd key $ data requires an encrypted string
{
$ Key [] = "";
$ Box [] = "";
$ Pwd_length = strlen ($ pwd );
$ Data_length = strlen ($ data );
For ($ I = 0; I I <256; $ I ++)
{
$ Key [$ I] = ord ($ pwd [$ I % $ pwd_length]);
$ Box [$ I] = $ I;
}
For ($ j = $ I = 0; I I <256; $ I ++)
{
$ J = ($ j + $ box [$ I] + $ key [$ I]) % 256;
$ Tmp = $ box [$ I];
$ Box [$ I] = $ box [$ j];
$ Box [$ j] = $ tmp;
}
For ($ a = $ j = $ I = 0; $ I <$ data_length; $ I ++)
{
$ A = ($ a + 1) % 256;
$ J = ($ j + $ box [$ a]) % 256;
$ Tmp = $ box [$ a];
$ Box [$ a] = $ box [$ j];
$ Box [$ j] = $ tmp;
$ K = $ box [($ box [$ a] + $ box [$ j]) % 256)];
$ Cipher. = chr (ord ($ data [$ I]) ^ $ k );
}

Return $ cipher;
}

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.