Implementing RC4 encryption and decryption in PHP

Source: Internet
Author: User

RC4 algorithm RC4 encryption algorithm is the famous RSA trio of the number one figure Ron Rivest in 1987 designed the key length of the variable stream encryption algorithm cluster. RC4 algorithm is a kind of encryption technology in the field of electronic information, used in wireless communication network, is an electronic password, only authorized (pay the corresponding fee) users can enjoy the service.

RC4, proposed in 1987, is a symmetric encryption algorithm, which means that the key used is a single key (or private key). RC4 does not group the plaintext, but instead encrypts each byte in the plaintext in turn, and decrypts every byte in the ciphertext in turn.

RC4 algorithm is characterized by simple algorithm, fast running speed, and the key length is variable, variable range of 1-256 bytes (8-2048 bits), under the premise of today's technology support, when the key length is 128 bit, the brute force method to search for the key is not feasible, So it is foreseeable that the key range of the RC4 can withstand brute force search key attacks for a considerable amount of time in the future. In fact, no effective attack method for the RC4 encryption algorithm for 128bit key length is found today

 

/* $PWD secret key;

$data the data to encrypt

*/

Public function RC4 ($PWD, $data)
{
$cipher = ";
$key [] = "";
$box [] = "";
$pwd _length = strlen ($PWD);
$data _length = strlen ($data);
for ($i = 0; $i < $i + +) {
$key [$i] = Ord ($pwd [$i% $pwd _length]);
$box [$i] = $i;
}
for ($j = $i = 0; $i < $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;
}



If you decrypt it, call the method again.


Implementing RC4 encryption and decryption in PHP

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.