CFLib.org – Common Function Library Project

RC4(strPwd, plaintxt)

Last updated January 12, 2006

Version: 1 | Requires: ColdFusion 5 | Library: SecurityLib

 
Rated 6 time(s). Average Rating: 3.2

Description:
A function does RC4 encryption by using a key and the string. Formula based upon script from Joshua Olson. Original article may be found here: http://www.4guysfromrolla.com/webtech/010100-1.shtml

Return Values:
Returns a string.

Example:

view plain print about
<cfoutput>
#RC4("chickens","TextToSecure")#
</cfoutput>

Parameters:

Name Description Required
strPwd The key to use for encryption. Yes
plaintxt Text to encrypt. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * A function does RC4 encryption by using a key and the string.
 * Original source: http://www.4guysfromrolla.com/webtech/010100-1.shtml
 * 
 * @param strPwd      The key to use for encryption. (Required)
 * @param plaintxt      Text to encrypt. (Required)
 * @return Returns a string. 
 * @author Michael Krock (michael.krock@avv.com) 
 * @version 1, January 12, 2006 
 */

function RC4(strPwd,plaintxt) {
    var sbox = ArrayNew(1);
    var key = ArrayNew(1);
    var tempSwap = 0;
    var a = 0;
    var b = 0;
    var intLength = len(strPwd);
    var temp = 0;
    var i = 0;
    var j = 0;
    var k = 0;
    var cipherby = 0;
    var cipher = "";
    
    for(a=0; a lte 255; a=a+1) {    
        key[a + 1] = asc(mid(strPwd,(a MOD intLength)+1,1));
        sbox[a + 1] = a;
    }

    for(a=0; a lte 255; a=a+1) {    
        b = (b + sbox[a + 1] + key[a + 1]) Mod 256;        
        tempSwap = sbox[a + 1];
        sbox[a + 1] = sbox[b + 1];
        sbox[b + 1] = tempSwap;    
    }

    for(a=1; a lte len(plaintxt); a=a+1) {    
        i = (i + 1) mod 256;
        j = (j + sbox[i + 1]) Mod 256;        
        temp = sbox[i + 1];
        sbox[i + 1] = sbox[j + 1];
        sbox[j + 1] = temp;
        k = sbox[((sbox[i + 1] + sbox[j + 1]) mod 256) + 1];        
        cipherby = BitXor(asc(mid(plaintxt, a, 1)), k);
        cipher = cipher & chr(cipherby);          
    }
    return cipher;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
4 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson