rc4_cipher 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9351ea7ecaf50b79fc388da4d7e27b64fa4ccf96
4
+ data.tar.gz: ebd1321cb80fb52630abf604b8afa297df2149d3
5
+ SHA512:
6
+ metadata.gz: 64173344b261411078313e0c64ee9eb4ebd155ed4bc02108690ca4b2ede52ffe411b322a658fc0e627c5ccadd30a9158f31f8eef529d6bf36f307cb25cd1296d
7
+ data.tar.gz: f1bbb372ad25deeb6b9a442f918feefc2e84ae26f42405be9ecd23ca8b66fbddcd2259a390746f9f13fecbd3518f6f1f80a822fdb2c82a425600434792d01b15
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # RC4
2
+
3
+ ###Install
4
+
5
+ In a terminal window simply install via the gem command
6
+
7
+ ```
8
+ $ gem install rc4_cipher
9
+ ```
10
+
11
+ another way to add it is to include it in your gemfile
12
+
13
+ ```ruby
14
+ gem 'rc4_cipher'
15
+ ```
16
+
17
+ ###Use
18
+
19
+ Require the gem in your program and create a new instance of the RC4 object, passing it a key string.
20
+
21
+ ```ruby
22
+ require 'rc4_cipher'
23
+
24
+ cipher = RC4.new("Secret")
25
+ ```
26
+
27
+ To receive bytes from the keystream access the key object and use the next function to return values
28
+
29
+ ```ruby
30
+ cipher.key.next
31
+ ```
32
+
33
+ To encryt & decrypt text, use the encrypt! and decrypt! methods
34
+
35
+ ```ruby
36
+ output = "Attack at dawn"
37
+ cipher.ecrypt!(output)
38
+
39
+ cipher2 = RC4.new("Secret")
40
+
41
+ cipher2.decrypt!(output)
42
+ ```
data/lib/rc4_cipher.rb ADDED
@@ -0,0 +1,59 @@
1
+ class RC4
2
+
3
+ attr_reader :key
4
+
5
+ def initialize(str)
6
+ @i, @j = 0 , 0
7
+
8
+ ini_state(str)
9
+ end
10
+
11
+
12
+ def encrypt!(text)
13
+ n = 0
14
+ while n < text.length
15
+
16
+ text.setbyte(n,key.next ^ text.getbyte(n))
17
+ n = n.next
18
+ end
19
+ text
20
+ end
21
+
22
+ alias_method :decrypt!, :encrypt!
23
+
24
+
25
+ private
26
+
27
+ STATE = (0..255).to_a
28
+
29
+ def ini_state(key)
30
+
31
+ @S = STATE.dup
32
+
33
+ j = 0
34
+
35
+ for i in 0..255
36
+ j = (j + @S[i] + key.getbyte(i % key.length)) % 256
37
+
38
+ @S[i],@S[j] = @S[j],@S[i]
39
+ end
40
+
41
+ @key = Enumerator.new do |k|
42
+ while true
43
+ @i = (@i + 1) % 256
44
+ @j = (@j+ @S[@i]) % 256
45
+
46
+ @S[@i], @S[@j] = @S[@j], @S[@i]
47
+
48
+ k.yield @S[(@S[@i]+@S[@j])%256]
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+
57
+
58
+
59
+
@@ -0,0 +1,60 @@
1
+ require 'rspec'
2
+ require 'rc4_cipher'
3
+
4
+ describe RC4 do
5
+
6
+ before :each do
7
+ @crypto = RC4.new("Secret")
8
+ end
9
+
10
+ describe "#new" do
11
+ it "takes a single string parameter and should return an instance of RC4" do
12
+ expect(@crypto).to be_an_instance_of RC4
13
+ end
14
+
15
+ it "raises an argument error when initialized without a key" do
16
+ expect {RC4.new}.to raise_error(ArgumentError)
17
+ end
18
+ end
19
+
20
+ describe "#key" do
21
+ it "returns numbers from the pseudorandom number generator" do
22
+ #test obtained from Wikipedia
23
+ output = ""
24
+ 8.times do
25
+ output << ("%02X" % @crypto.key.next)
26
+ end
27
+ expect(output).to eql "04D46B053CA87B59"
28
+
29
+ end
30
+ end
31
+
32
+ describe "#encrypt!" do
33
+ it "returns an encrypted text string from a string of text" do
34
+ output = "Attack at dawn"
35
+ @crypto.encrypt!(output)
36
+ hex_output = ""
37
+ for i in 0...output.length
38
+ hex_output << ("%02X" % output.getbyte(i))
39
+ end
40
+ expect(hex_output).to eql "45A01F645FC35B383552544B9BF5"
41
+
42
+ end
43
+
44
+ end
45
+
46
+ describe "#decrypt!" do
47
+ it "returns a decrypted text string from a string of encrypted text" do
48
+ output = "Attack at dawn"
49
+ @crypto.encrypt!(output)
50
+ decrypter = RC4.new("Secret")
51
+ decrypter.decrypt!(output)
52
+
53
+ expect(output).to eql "Attack at dawn"
54
+
55
+ end
56
+
57
+ end
58
+
59
+
60
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rc4_cipher
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cody Deckard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple implementation of the RC4 Cipher
14
+ email: cjdeckard@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/rc4_cipher.rb
21
+ - spec/rc4_crypt_spec.rb
22
+ homepage:
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.1.2
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 2.2.2
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: An implementation of the RC4 Cipher
46
+ test_files:
47
+ - spec/rc4_crypt_spec.rb