# Arcfour encryption algorithm

**URL:** <https://www.ruby-forum.com/t/arcfour-encryption-algorithm/147555>\
**Category:** Ruby\
**Created:** [September 3, 2008, 3:55am UTC](https://www.ruby-forum.com/t/arcfour-encryption-algorithm/147555 "2008-09-03T03:55:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CiriusMex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@CiriusMex](https://www.ruby-forum.com/u/CiriusMex)\
**Post date:** [September 3, 2008, 3:55am UTC](https://www.ruby-forum.com/t/arcfour-encryption-algorithm/147555/1 "2008-09-03T03:55:00Z")

</div>

Hi folks,

I’m looking for a way to use the arc4 encryption algorithm with  
RubyOnRails. I have some automatic mails in which there’s links to my  
website functions. In these links I want to encode various datas (such  
as login, password, identification number…). I did it with Chilkat  
and works great but this plugin can only be used with Windows so now  
that I deployed my WebSite on linux, well the function using Chilkat  
does’t work any more…

Thanks for your help and suggestions (I’ve been looking for another  
solution all the day testing various encryption algorithm but can’t  
find anything as good as arc4).

---

<div class="post-metadata">

**Author:** ![CiriusMex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@CiriusMex](https://www.ruby-forum.com/u/CiriusMex)\
**Post date:** [September 3, 2008, 10:00am UTC](https://www.ruby-forum.com/t/arcfour-encryption-algorithm/147555/2 "2008-09-03T10:00:41Z")

</div>

Here you have simple implentation in Python, it should be easy to  
translate that into ruby

> **[RC4](https://en.wikipedia.org/wiki/RC4_(cipher))**
>
> In cryptography, RC4 (Rivest Cipher 4, also known as ARC4 or ARCFOUR, meaning Alleged RC4, see below) is a stream cipher. While it is remarkable for its simplicity and speed in software, multiple vulnerabilities have been discovered in RC4, rendering it insecure. It is especially vulnerable when the beginning of the output keystream is not discarded, or when nonrandom or related keys are used. Particularly problematic uses of RC4 have led to very insecure protocols such as WEP.
> As of 2015\[upda...

m.

---

<div class="post-metadata">

**Author:** ![CiriusMex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@CiriusMex](https://www.ruby-forum.com/u/CiriusMex)\
**Post date:** [September 4, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/arcfour-encryption-algorithm/147555/3 "2008-09-04T19:40:36Z")

</div>

On 3 sep, 02:59, edek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Here you have simple implentation in Python, it should be easy to  
> translate that into ruby
> 
> [RC4 - Wikipedia](http://en.wikipedia.org/wiki/RC4_(cipher))
> 
> m.

I’m gonna have a look at it, thanks 😉

---

<div class="post-metadata">

**Author:** ![CiriusMex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/90ced4/32.png) [@CiriusMex](https://www.ruby-forum.com/u/CiriusMex)\
**Post date:** [September 5, 2008, 2:45am UTC](https://www.ruby-forum.com/t/arcfour-encryption-algorithm/147555/4 "2008-09-05T02:45:05Z")

</div>

I’ve been using this implementation for a while. Unfortunately, the  
file doesn’t have any author info in it, and I can’t find it in Google  
any longer, so I’m not sure who deserves credit for it.

I also make this patch to it:

class ARC4

def setup(key)  
#@init\_s = Array.new(256) { |i| i }  
@init\_s = ‘’  
0.upto(255) { |i| @init\_s \<\< i }  
j = 0  
for i in 0 … 255  
j = (j + @init\_s[i] + key[i % key.size]) & 0xff  
@init\_s[i], @init\_s[j] = @init\_s[j], @init\_s[i]  
end  
reset  
end

def reset  
@s = @init\_s.dup  
@i = 0  
@j = 0  
end

end

Cheers,  
Jim
