# 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.

WARNING: This script is NOT SUITABLE FOR GM:STUDIO. Because GM:Studio strings are null terminated, there is a very high chance that the encrypted strings generated by this script will be truncated resulting in data loss. The script must be modified to perform its actions using a different data type. Buffers are the recommended type for arbitrary binary data such as that produced by this script.

- rc4(str,key)
- Returns the given string encrypted/decrypted with the RC4 algorithm using the given key.

`COPY````
/// rc4(str,key)
//
// Returns the given string encrypted/decrypted with the RC4 algorithm
// using the given key. RC4 is a trademark of RSA Data Security, Inc.
//
// str plaintext or ciphertext, string
// key encryption key, string
//
/// GMLscripts.com/license
{
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;
}
```

Contributors: xot

GitHub: View · Commits · Blame · Raw