rc4
RC4 is a very fast and very effective variable-key-size stream cipher. This is a self-inverse algorithm meaning that if you encrypt the plaintext with a given key, applying the same key to the ciphertext will reproduce the original plaintext. RC4 was developed in 1987 by Ron Rivest and is a trademark of RSA Data Security, Inc. It was a trade secret until a compatible algorithm was anonymously posted on the internet in 1994. The common generic names for the algorithm are ARC4 and ARCFOUR.
/*
** Usage:
** rc4(str,key)
**
** Arguments:
** str plaintext or ciphertext, string
** key encryption key, string
**
** Returns:
** the given string encrypted/decrypted with the given key
**
** Notes:
** RC4 is a trademark owned by RSA Data Security, Inc.
** ARC4 and ARCFOUR are generic names for the algorithm.
**
** GMLscripts.com
*/
{
var str,key,out,len,i,S,j,temp,pos,t;
str = argument0;
key = argument1;
out = "";
len = string_length(key);
for (i=0; i<256; i+=1) S[i] = i;
j = 0;
for (i=0; i<256; i+=1) {
j = (j + S[i] + ord(string_char_at(key,(i mod len)+1))) mod 256;
temp = S[i];
S[i] = S[j];
S[j] = temp;
}
i = 0;
j = 0;
for (pos=0; pos<string_length(str); pos+=1) {
i = (i + 1) mod 256;
j = (j + S[i]) mod 256;
temp = S[i];
S[i] = S[j];
S[j] = temp;
t = (S[i] + S[j]) mod 256;
out += chr(ord(string_char_at(str,pos+1)) ^ S[t]);
}
return out;
}
** Usage:
** rc4(str,key)
**
** Arguments:
** str plaintext or ciphertext, string
** key encryption key, string
**
** Returns:
** the given string encrypted/decrypted with the given key
**
** Notes:
** RC4 is a trademark owned by RSA Data Security, Inc.
** ARC4 and ARCFOUR are generic names for the algorithm.
**
** GMLscripts.com
*/
{
var str,key,out,len,i,S,j,temp,pos,t;
str = argument0;
key = argument1;
out = "";
len = string_length(key);
for (i=0; i<256; i+=1) S[i] = i;
j = 0;
for (i=0; i<256; i+=1) {
j = (j + S[i] + ord(string_char_at(key,(i mod len)+1))) mod 256;
temp = S[i];
S[i] = S[j];
S[j] = temp;
}
i = 0;
j = 0;
for (pos=0; pos<string_length(str); pos+=1) {
i = (i + 1) mod 256;
j = (j + S[i]) mod 256;
temp = S[i];
S[i] = S[j];
S[j] = temp;
t = (S[i] + S[j]) mod 256;
out += chr(ord(string_char_at(str,pos+1)) ^ S[t]);
}
return out;
}
[Please Login]
Projects: 0
Contributor: xot
comments powered by Disqus

Related: