A couple of years ago, I published a routine to perform RC4 encryption in Synergy/DE, translated from the original VBScript of Mike Shaffer with his permission. Lots of people searching for the RC4 algorithm have landed on that post, most of them not knowing the Synergy/DE language. A couple of days ago, a reader going by the alias “crazysoccer” asked if I could provide an example in C.
The download below contains a straight C version of the famous “EnDeCrypt” function, along with a simple “main” test program. The flavor of C I used is vendor and platform neutral. It only presumes the availability of stdio and malloc. Naturally, you could tweak this code to use whatever I/O and memory allocation mechanisms your development framework prefers.
I’ve been asked before about the legality of publishing cryptographic algorithms on the web. According to Wikipedia (which is not a lawyer, nor do I think it has ever played one on TV), rules for exporting cryptography have been relaxed in recent years, including “the effective elimination of export controls on … open source software containing cryptography.” Since I’m making this freely available to all, and I welcome any contributions to improving the code, I’ll consider this “open source”. Besides, I wasn’t the first one to publish this algorithm on the web, and if the Bureau of Industry and Security wants to come after me, it’ll have to go get 4GuysFromRolla, too. But if you plan to use this algorithm in software that will be exported from the US or other nations with similar rules, you might want to consult your lawyer on any restrictions you need to follow.
UPDATE: Changed the API to include a specified length, since the encrypted data can contain a null.