crypt19-rb 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ module Crypt
2
+ module StringXor
3
+
4
+ def ^(a_string)
5
+ a = self.unpack('C'*(self.length))
6
+ b = a_string.unpack('C'*(a_string.length))
7
+ if (b.length < a.length)
8
+ (a.length - b.length).times { b << 0 }
9
+ end
10
+ xor = ""
11
+ 0.upto(a.length-1) { |pos|
12
+ x = a[pos] ^ b[pos]
13
+ xor << x.chr()
14
+ }
15
+ return(xor)
16
+ end
17
+
18
+ end
19
+ end
20
+
21
+ class String
22
+ include Crypt::StringXor
23
+ end
@@ -0,0 +1,3 @@
1
+ module Crypt
2
+ VERSION = "1.2.1"
3
+ end
@@ -0,0 +1,84 @@
1
+ require 'test/unit'
2
+ require 'crypt/blowfish'
3
+ require 'crypt/cbc'
4
+ require 'fileutils'
5
+
6
+ class TestBlowfish < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @bf = Crypt::Blowfish.new("Who is John Galt?") # Schneier's test key
10
+ end
11
+
12
+ def test_block_size
13
+ assert_equal(8, @bf.block_size(), "Wrong block size")
14
+ end
15
+
16
+ def test_initialize
17
+ assert_raise(RuntimeError) {
18
+ b0 = Crypt::Blowfish.new("")
19
+ }
20
+ assert_nothing_raised() {
21
+ b1 = Crypt::Blowfish.new("1")
22
+ }
23
+ assert_nothing_raised() {
24
+ b56 = Crypt::Blowfish.new("1"*56)
25
+ }
26
+ assert_raise(RuntimeError) {
27
+ b57 = Crypt::Blowfish.new("1"*57)
28
+ }
29
+ end
30
+
31
+ def test_pair
32
+ bf = Crypt::Blowfish.new("Who is John Galt?")
33
+ orig_l, orig_r = [0xfedcba98, 0x76543210]
34
+ l, r = bf.encrypt_pair(orig_l, orig_r)
35
+ assert_equal(0xcc91732b, l)
36
+ assert_equal(0x8022f684, r)
37
+ l, r = bf.decrypt_pair(l, r)
38
+ assert_equal(orig_l, l)
39
+ assert_equal(orig_r, r)
40
+ end
41
+
42
+ def test_block
43
+ bf = Crypt::Blowfish.new("Who is John Galt?")
44
+ block = "norandom"
45
+ encrypted_block = bf.encrypt_block(block)
46
+ assert_equal("\236\353k\321&Q\"\220", encrypted_block)
47
+ decrypted_block = bf.decrypt_block(encrypted_block)
48
+ assert_equal(block, decrypted_block)
49
+ end
50
+
51
+ def test_string
52
+ length = 30 + rand(26)
53
+ userkey = ""
54
+ length.times { userkey << rand(256).chr }
55
+ bf = Crypt::Blowfish.new(userkey)
56
+ string = "This is a string which is not a multiple of 8 characters long"
57
+ encrypted_string = bf.encrypt_string(string)
58
+ decrypted_string = bf.decrypt_string(encrypted_string)
59
+ assert_equal(string, decrypted_string)
60
+ secondstring = "This is another string to check repetitive use."
61
+ encrypted_string = bf.encrypt_string(secondstring)
62
+ decrypted_string = bf.decrypt_string(encrypted_string)
63
+ assert_equal(secondstring, decrypted_string)
64
+
65
+ end
66
+
67
+ def test_file
68
+ plain_text = "This is a multi-line string\nwhich is not a multiple of 8 \ncharacters long."
69
+ plain_file = File.new('plain.txt', 'wb+')
70
+ plain_file.puts(plain_text)
71
+ plain_file.close()
72
+ bf = Crypt::Blowfish.new("Who is John Galt?")
73
+ bf.encrypt_file('plain.txt', 'crypt.txt')
74
+ bf.decrypt_file('crypt.txt', 'decrypt.txt')
75
+ decrypt_file = File.new('decrypt.txt', 'rb')
76
+ decrypt_text = decrypt_file.readlines().join('').chomp()
77
+ decrypt_file.close()
78
+ assert_equal(plain_text, decrypt_text)
79
+ FileUtils.rm('plain.txt')
80
+ FileUtils.rm('crypt.txt')
81
+ FileUtils.rm('decrypt.txt')
82
+ end
83
+
84
+ end
@@ -0,0 +1,69 @@
1
+ require 'test/unit'
2
+ require 'crypt/gost'
3
+ require 'fileutils'
4
+
5
+ class TestGost < Test::Unit::TestCase
6
+
7
+ def test_init
8
+ assert_nothing_raised(RuntimeError) {
9
+ gost = Crypt::Gost.new("Whatever happened to Yuri Gagarin?")
10
+ }
11
+ assert_nothing_raised(RuntimeError) {
12
+ gost = Crypt::Gost.new("Whatever happened to Yuri?")
13
+ }
14
+ end
15
+
16
+ def test_block_size
17
+ gost = Crypt::Gost.new("Whatever happened to Yuri?")
18
+ assert_equal(8, gost.block_size(), "Wrong block size")
19
+ end
20
+
21
+ def test_pair
22
+ gost = Crypt::Gost.new("Whatever happened to Yuri?")
23
+ orig_l, orig_r = [0xfedcba98, 0x76543210]
24
+ l, r = gost.encrypt_pair(orig_l, orig_r)
25
+ assert_equal(0xaefaf8f4, l)
26
+ assert_equal(0xe24891b0, r)
27
+ l, r = gost.decrypt_pair(l, r)
28
+ assert_equal(orig_l, l)
29
+ assert_equal(orig_r, r)
30
+ end
31
+
32
+ def test_block
33
+ gost = Crypt::Gost.new("Whatever happened to Yuri?")
34
+ block = "norandom"
35
+ encrypted_block = gost.encrypt_block(block)
36
+ assert_equal(".Vy\377\005\e3`", encrypted_block)
37
+ decrypted_block = gost.decrypt_block(encrypted_block)
38
+ assert_equal(block, decrypted_block)
39
+ end
40
+
41
+ def test_string
42
+ length = 25 + rand(12)
43
+ userkey = ""
44
+ length.times { userkey << rand(256).chr }
45
+ gost = Crypt::Gost.new(userkey)
46
+ string = "This is a string which is not a multiple of 8 characters long"
47
+ encrypted_string = gost.encrypt_string(string)
48
+ decrypted_string = gost.decrypt_string(encrypted_string)
49
+ assert_equal(string, decrypted_string)
50
+ end
51
+
52
+ def test_file
53
+ plain_text = "This is a multi-line string\nwhich is not a multiple of 8 \ncharacters long."
54
+ plain_file = File.new('plain.txt', 'wb+')
55
+ plain_file.puts(plain_text)
56
+ plain_file.close()
57
+ gost = Crypt::Gost.new("Whatever happened to Yuri?")
58
+ gost.encrypt_file('plain.txt', 'crypt.txt')
59
+ gost.decrypt_file('crypt.txt', 'decrypt.txt')
60
+ decrypt_file = File.new('decrypt.txt', 'rb')
61
+ decrypt_text = decrypt_file.readlines().join('').chomp()
62
+ decrypt_file.close()
63
+ assert_equal(plain_text, decrypt_text)
64
+ FileUtils.rm('plain.txt')
65
+ FileUtils.rm('crypt.txt')
66
+ FileUtils.rm('decrypt.txt')
67
+ end
68
+
69
+ end
@@ -0,0 +1,70 @@
1
+ require 'test/unit'
2
+ require 'crypt/idea'
3
+ require 'fileutils'
4
+
5
+ class TestIdea < Test::Unit::TestCase
6
+
7
+ def test_init
8
+ assert_nothing_raised(RuntimeError) {
9
+ idea_en = Crypt::IDEA.new("Who was John Galt and where's my breakfast?", Crypt::IDEA::ENCRYPT)
10
+ }
11
+ end
12
+
13
+ def test_block_size
14
+ idea_en = Crypt::IDEA.new("Who is John Galt", Crypt::IDEA::ENCRYPT)
15
+ assert_equal(8, idea_en.block_size(), "Wrong block size")
16
+ end
17
+
18
+ def test_pair
19
+ idea_en = Crypt::IDEA.new("Who is John Galt", Crypt::IDEA::ENCRYPT)
20
+ orig_l, orig_r = [0xfedcba98, 0x76543210]
21
+ l, r = idea_en.crypt_pair(orig_l, orig_r)
22
+ assert_equal(0x05627e79, l)
23
+ assert_equal(0x69476521, r)
24
+ idea_de = Crypt::IDEA.new("Who is John Galt", Crypt::IDEA::DECRYPT)
25
+ l, r = idea_de.crypt_pair(l, r)
26
+ assert_equal(orig_l, l)
27
+ assert_equal(orig_r, r)
28
+ end
29
+
30
+ def test_block
31
+ idea_en = Crypt::IDEA.new("Who is John Galt", Crypt::IDEA::ENCRYPT)
32
+ block = "norandom"
33
+ encrypted_block = idea_en.encrypt_block(block)
34
+ assert_equal("\235\003\326u\001\330\361\t", encrypted_block)
35
+ idea_de = Crypt::IDEA.new("Who is John Galt", Crypt::IDEA::DECRYPT)
36
+ decrypted_block = idea_de.decrypt_block(encrypted_block)
37
+ assert_equal(block, decrypted_block)
38
+ end
39
+
40
+ def test_string
41
+ length = 25 + rand(12)
42
+ userkey = ""
43
+ length.times { userkey << rand(256).chr }
44
+ idea_en = Crypt::IDEA.new(userkey, Crypt::IDEA::ENCRYPT)
45
+ string = "This is a string which is not a multiple of 8 characters long"
46
+ encrypted_string = idea_en.encrypt_string(string)
47
+ idea_de = Crypt::IDEA.new(userkey, Crypt::IDEA::DECRYPT)
48
+ decryptedString = idea_de.decrypt_string(encrypted_string)
49
+ assert_equal(string, decryptedString)
50
+ end
51
+
52
+ def test_file
53
+ plain_text = "This is a multi-line string\nwhich is not a multiple of 8 \ncharacters long."
54
+ plain_file = File.new('plain.txt', 'wb+')
55
+ plain_file.puts(plain_text)
56
+ plain_file.close()
57
+ idea_en = Crypt::IDEA.new("Who is John Galt", Crypt::IDEA::ENCRYPT)
58
+ idea_en.encrypt_file('plain.txt', 'crypt.txt')
59
+ idea_de = Crypt::IDEA.new("Who is John Galt", Crypt::IDEA::DECRYPT)
60
+ idea_de.decrypt_file('crypt.txt', 'decrypt.txt')
61
+ decrypt_file = File.new('decrypt.txt', 'rb')
62
+ decrypt_text = decrypt_file.readlines().join('').chomp()
63
+ decrypt_file.close()
64
+ assert_equal(plain_text, decrypt_text)
65
+ FileUtils.rm('plain.txt')
66
+ FileUtils.rm('crypt.txt')
67
+ FileUtils.rm('decrypt.txt')
68
+ end
69
+
70
+ end
@@ -0,0 +1,72 @@
1
+ require 'test/unit'
2
+ require 'crypt/rc6'
3
+ require 'fileutils'
4
+
5
+ class TestRC6 < Test::Unit::TestCase
6
+
7
+ def test_block_size
8
+ assert_equal(16, Crypt::RC6.new([0]*32).block_size(), "Wrong block size")
9
+ end
10
+
11
+ def test_initialize
12
+ ["","1", "qwe"].each do |key|
13
+ assert_raise(RuntimeError) {
14
+ Crypt::RC6.new(key)
15
+ }
16
+ end
17
+
18
+ ["1234567890123456","123456789012345678901234"].each do |key|
19
+ assert_nothing_raised() {
20
+ Crypt::RC6.new(key)
21
+ }
22
+ end
23
+ end
24
+
25
+ def test_vectors
26
+ vectors = [
27
+ [[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
29
+ [0x00000000, 0x00000000, 0x00000000, 0x00000000],
30
+ [0x36a5c38f, 0x78f7b156, 0x4edf29c1, 0x1ea44898] ],
31
+
32
+ [[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
33
+ 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78],
34
+ [0x35241302, 0x79685746, 0xbdac9b8a, 0xf1e0dfce],
35
+ [0x2f194e52, 0x23c61547, 0x36f6511f, 0x183fa47e]],
36
+
37
+ [[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
40
+ [0x00000000, 0x00000000, 0x00000000, 0x00000000],
41
+ [0xcb1bd66c, 0x38300b19, 0x163f8a4e, 0x82ae9086] ],
42
+
43
+ [[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
44
+ 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
45
+ 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0],
46
+ [0x35241302, 0x79685746, 0xbdac9b8a, 0xf1e0dfce],
47
+ [0xd0298368, 0x0405e519, 0x2ae9521e, 0xd49152f9]],
48
+
49
+ [[ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
53
+ [0x00000000, 0x00000000, 0x00000000, 0x00000000],
54
+ [0x05bd5f8f, 0xa85fd110, 0xda3ffa93, 0xc27e856e] ],
55
+
56
+ [[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
57
+ 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
58
+ 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0,
59
+ 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe],
60
+ [0x35241302, 0x79685746, 0xbdac9b8a, 0xf1e0dfce],
61
+ [0x161824c8, 0x89e4d7f0, 0xa116ad20, 0x485d4e67]],
62
+ ]
63
+
64
+ vectors.each_with_index do |vector, i|
65
+ rc6 = Crypt::RC6.new(vector[0])
66
+ assert_equal(rc6.encrypt_block(vector[1].pack('N*')), vector[2].pack('N*'))
67
+ assert_equal(rc6.decrypt_block(vector[2].pack('N*')), vector[1].pack('N*'))
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -0,0 +1,61 @@
1
+ require 'test/unit'
2
+ require 'crypt/rijndael'
3
+ require 'fileutils'
4
+
5
+ class TestRijndael < Test::Unit::TestCase
6
+
7
+ def test_init
8
+ assert_raise(RuntimeError) {
9
+ rijndael = Crypt::Rijndael.new("Who is this John Galt guy, anyway?", 64)
10
+ }
11
+ assert_raise(RuntimeError) {
12
+ rijndael = Crypt::Rijndael.new("Who is this John Galt guy, anyway?", 128, 64)
13
+ }
14
+ end
15
+
16
+ def test_block_size
17
+ rijndael = Crypt::Rijndael.new("Who is this John Galt guy, anyway?", 128, 256)
18
+ assert_equal(32, rijndael.block_size)
19
+ rijndael = Crypt::Rijndael.new("Who is this John Galt guy, anyway?", 256, 128)
20
+ assert_equal(16, rijndael.block_size)
21
+ end
22
+
23
+ def test_block
24
+ rijndael = Crypt::Rijndael.new("Who is this John Galt guy, anyway?", 128, 128)
25
+ block = "\341q\214NIj\023u\343\330s\323\354$g\277"
26
+ encrypted_block = rijndael.encrypt_block(block)
27
+ assert_equal("\024\246^\332T\323x`\323yB\352\2159\212R", encrypted_block)
28
+ decrypted_block = rijndael.decrypt_block(encrypted_block)
29
+ assert_equal(block, decrypted_block)
30
+ rijndael = Crypt::Rijndael.new("Who is this John Galt guy, anyway?", 128, 256)
31
+ assert_raise(RuntimeError) {
32
+ encrypted_block = rijndael.encrypt_block(block)
33
+ }
34
+ end
35
+
36
+ def test_string
37
+ rijndael = Crypt::Rijndael.new("Who is this John Galt guy, anyway?")
38
+ string = "This is a string which is not a multiple of 8 characters long"
39
+ encrypted_string = rijndael.encrypt_string(string)
40
+ decrypted_string = rijndael.decrypt_string(encrypted_string)
41
+ assert_equal(string, decrypted_string)
42
+ end
43
+
44
+ def test_file
45
+ plain_text = "This is a multi-line string\nwhich is not a multiple of 8 \ncharacters long."
46
+ plain_file = File.new('plain.txt', 'wb+')
47
+ plain_file.puts(plain_text)
48
+ plain_file.close()
49
+ rijndael = Crypt::Rijndael.new("Who is this John Galt guy, anyway?")
50
+ rijndael.encrypt_file('plain.txt', 'crypt.txt')
51
+ rijndael.decrypt_file('crypt.txt', 'decrypt.txt')
52
+ decrypt_file = File.new('decrypt.txt', 'rb')
53
+ decrypt_text = decrypt_file.readlines().join('').chomp()
54
+ decrypt_file.close()
55
+ assert_equal(plain_text, decrypt_text)
56
+ FileUtils.rm('plain.txt')
57
+ FileUtils.rm('crypt.txt')
58
+ FileUtils.rm('decrypt.txt')
59
+ end
60
+
61
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crypt19-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Rudenberg
8
+ - Richard Kernahan
9
+ - Maximilian Haack
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-02-26 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 10.0.3
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ version: 10.0.3
29
+ description: Crypt is a pure-ruby implementation of a number of popular encryption
30
+ algorithms. Block cyphers currently include Blowfish, GOST, IDEA, Rijndael (AES),
31
+ and RC6. Cypher Block Chaining (CBC) has been implemented.
32
+ email:
33
+ - mxhaack@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .travis.yml
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE
42
+ - README.md
43
+ - Rakefile
44
+ - VERSION
45
+ - crypt19.gemspec
46
+ - lib/crypt/blowfish-tables.rb
47
+ - lib/crypt/blowfish.rb
48
+ - lib/crypt/bytes-compat.rb
49
+ - lib/crypt/cbc.rb
50
+ - lib/crypt/gost.rb
51
+ - lib/crypt/idea.rb
52
+ - lib/crypt/noise.rb
53
+ - lib/crypt/rc6.rb
54
+ - lib/crypt/rijndael-tables.rb
55
+ - lib/crypt/rijndael.rb
56
+ - lib/crypt/stringxor.rb
57
+ - lib/crypt/version.rb
58
+ - test/blowfish_test.rb
59
+ - test/gost_test.rb
60
+ - test/idea_test.rb
61
+ - test/rc6_test.rb
62
+ - test/rijndael_test.rb
63
+ homepage: https://github.com/coffeejunk/crypt19
64
+ licenses: []
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.0.0
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Crypt is a pure-ruby implementation of a number of popular encryption algorithms.
86
+ test_files:
87
+ - test/blowfish_test.rb
88
+ - test/gost_test.rb
89
+ - test/idea_test.rb
90
+ - test/rc6_test.rb
91
+ - test/rijndael_test.rb