Search This Blog

Oct 6, 2011

RC4 Encryption Sample

A file sharing is a daily common task which makes the dissemination of information accurately and on time. This activity can be seen particularly in online businesses which help them to maximize their production through fast and accurate information sharing.

Sales reports, plans and other information are shared over businesses, either business to business entity or exclusive only for the company. However, security still is the common issue. In every sharing activity, there were tendencies that information can be tapped by other person and used the data for their own.

Critical information such as credit cards are need to be encrypted so even if it is get by other people it is not easily be decrypted and use in fraud.

There are several encryption are know in the web but I will show you today about the RC4 Encryption.

R4 Encryption also known as ARC4 is the most widely used stream cipher which designed by Ron Rivest. It generates a pseudorandom stream of bits. As with any stream cipher, these can be used for encryption by combining it with the plaintext using bit-wise exclusive-or; decryption is performed the same way (since exclusive-or is a symmetric operation). (This is similar to the Vernam cipher except that generated pseudorandom bits, rather than a prepared stream, are used.) To generate the keystream, the cipher makes use of a secret internal state which consists of two parts:

1. A permutation of all 256 possible bytes (denoted "S" below).
2. Two 8-bit index-pointers (denoted "i" and "j").

The permutation is initialized with a variable length key, typically between 40 and 256 bits, using the key-scheduling algorithm (KSA). Once this has been completed, the stream of bits is generated using the pseudo-random generation algorithm (PRGA). – Wikipedia

RC4 Encryption basically use hash key which use to get the encryption output of your data. See the example below how rc4 work.

<?php
   
  /* this is the function where encryption made
  * @params $key is the hash key value 
  * @params $pt is the data that you want to encrypt 
  */
 
    function _rc4($key, $pt) {
        $s = array();
        for ($i = 0; $i < 256; $i++) {
            $s[$i] = $i;
        }
        $j = 0;
        $x;
        for ($i = 0; $i < 256; $i++) {
            $j = ($j + $s[$i] + ord($key[$i % strlen($key)])) % 256;
            $x = $s[$i];
            $s[$i] = $s[$j];
            $s[$j] = $x;
        }
        $i = 0;
        $j = 0;
        $ct = '';
        $y;
        for ($y = 0; $y < strlen($pt); $y++) {
            $i = ($i + 1) % 256;
            $j = ($j + $s[$i]) % 256;
            $x = $s[$i];
            $s[$i] = $s[$j];
            $s[$j] = $x;
            $ct .= $pt[$y] ^ chr($s[($s[$i] + $s[$j]) % 256]);
        }
        
        return $ct;
    }
?>
To encrypt data, simply call this function passing the data that you want to decrypt
<?php
 
  /* this is the function where encryption made
  * @params $key is the hash key value 
  * @params $ct is the data that you want to encrypt 
  */

    function rc4Encrypt($ct) {

        // this is your key variable. This is the hash key which encryption is based on 
        $key ='pAiZ+j&Q4RrfJH+0Xjvf^@DpI3#EJwsx-]VT)6Whyk(o';	
       
        return urlencode(base64_encode(_rc4($key, $ct)));
    }

?>
To decrypt data, simply call this function passing the encrypted data as paramater.
<?php
 
  /* this is the function where encryption made
  * @params $key is the hash key value 
  * @params $ct is the data that you want to encrypt 
  */

    
    function rc4Decrypt($ct) {
       
        		$key ='pAiZ+j&Q4RrfJH+0Xjvf^@DpI3#EJwsx-]VT)6Whyk(o';	
        
        return _rc4($key, base64_decode(urldecode($ct)));
    }
?>

So, for example you want to encrypt the "Encrypt this" using the hash key above is:
 
hBKPieTvzKP%2Fs71d
Hope this post help you in certain way.

3 comments:

Lalaine Mateo said...

PANSALT, the low-sodium salt from Finland, is now available in the Philippines. PANSALT is the tastier & healthier way to salt. With PANSALT, you can avoid and better manage hypertension and its complications such as heart attack, stroke, kidney disease, among others. PANSALT has also been approved as an organic food ingredient by the European Commission. Make that switch to PANSALT NOW!!! For more information, visit www.pansalt.ph or contact (02) 531-4881/ 0917-811-SALT (7258).

KOL Limited said...

You have a great blog. I like your post it's amazing! It too interesting!! Thanks! Web development london

Websitedesigninguk said...

Your example really nice ! i would be try it ! London web design