CFLib.org – Common Function Library Project

RC4(strPwd, plaintxt)

Last updated January 12, 2006
Download UDF

author

Michael Krock                                     Michael Krock

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

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:

<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:

<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 (&#109;&#105;&#99;&#104;&#97;&#101;&#108;&#46;&#107;&#114;&#111;&#99;&#107;&#64;&#97;&#118;&#118;&#46;&#99;&#111;&#109;) 
 * @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>

Search CFLib.org


Latest Additions

Jose Diaz-Salcedo Jose Diaz-Salcedo added
cfRssFeed
2 day(s) ago

Raymond Compton Raymond Compton added
structBlend
23 day(s) ago

Duncan Duncan added
IsZIPUK
23 day(s) ago

Todd Sharp Todd Sharp added
getTagContentAll
29 day(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Created by Raymond Camden / Design by Justin Johnson