enygma 1.0.0 → 1.0.2
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 +4 -4
- data/README.md +8 -31
- data/enygma.gemspec +6 -6
- data/lib/enygma.rb +3 -3
- data/lib/enygma/cracker.rb +24 -19
- data/lib/enygma/decryptor.rb +28 -18
- data/lib/enygma/encryptor.rb +23 -15
- data/lib/enygma/helpers/filer.rb +5 -3
- data/lib/enygma/helpers/key_gen.rb +26 -12
- data/lib/enygma/helpers/rotator.rb +7 -3
- data/lib/enygma/mixins/character_mapper.rb +1 -1
- data/lib/enygma/version.rb +1 -1
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81fe400dc15ce3b9e134048aa3a474a1ceee4c51
|
4
|
+
data.tar.gz: 78d8f268727f989a103f719fabb4bfd9311330f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1431acd831b3be2ae33209bebb22d1a1853ea49ec0a286c5b7d7629f7d6080620923130876b18bbe20c3c6b69317f59ce632ed1378994ec0b18f91f114854af4
|
7
|
+
data.tar.gz: 9a2e9bc051d0d0dd217a33355991f078633b297db827cfaa9e3bb31aa53ae5c3e7a6ca598bfb296e3af1e42c984d99af4f80d0044861f2a375f62b15828b8211
|
data/README.md
CHANGED
@@ -1,29 +1,6 @@
|
|
1
|
-
# Enygma [](https://coveralls.io/github/andela-echigbo/enygma?branch=master) [](https://circleci.com/gh/andela-echigbo/enygma)
|
1
|
+
# Enygma [](https://coveralls.io/github/andela-echigbo/enygma?branch=master) [](https://circleci.com/gh/andela-echigbo/enygma) [](https://codeclimate.com/github/andela-echigbo/enygma)
|
2
2
|
|
3
|
-
Enygma is an
|
4
|
-
|
5
|
-
## How the encryption works
|
6
|
-
|
7
|
-
The encryption is based on rotation of letters using a key and the date of the encryption. The character map is made up of lower case alphabets (a - z), numbers(0 - 9), space character, comma, and period character.
|
8
|
-
|
9
|
-
### The Key
|
10
|
-
|
11
|
-
* Each message uses a unique encryption key
|
12
|
-
* The key is five digits, like 41521
|
13
|
-
* The first two digits of the key are the "A" rotation (41)
|
14
|
-
* The second and third digits of the key are the "B" rotation (15)
|
15
|
-
* The third and fourth digits of the key are the "C" rotation (52)
|
16
|
-
* The fourth and fifth digits of the key are the "D" rotation (21)
|
17
|
-
|
18
|
-
### The Offset
|
19
|
-
|
20
|
-
* The date of message transmission is also factored into the encryption
|
21
|
-
* Consider the date in the format DDMMYY, like 020315
|
22
|
-
* Square the numeric form (412699225) and find the last four digits (9225)
|
23
|
-
* The first digit is the "A offset" (9)
|
24
|
-
* The second digit is the "B offset" (2)
|
25
|
-
* The third digit is the "C offset" (2)
|
26
|
-
* The fourth digit is the "D offset" (5)
|
3
|
+
Enygma is an encryption engine built (as a gem) with ruby programming language. It follows the principles of [Enigma Encryption Machine](https://en.wikipedia.org/wiki/Enigma_machine) to encrypt and decrypt files. The gem also offers the feature of cracking a file.
|
27
4
|
|
28
5
|
## Installation
|
29
6
|
|
@@ -48,14 +25,14 @@ This gem provides you with three command line actions, `encrypt`, `decrypt`, and
|
|
48
25
|
|
49
26
|
When you have installed the enygma gem, you can encrypt a file by changing to the directory that contantains the file, and run any of the following commands
|
50
27
|
|
51
|
-
|
28
|
+
#### Encryption
|
52
29
|
$ encrypt <filename> [<destination-filename>]
|
53
30
|
|
54
|
-
|
31
|
+
#### Decryption
|
55
32
|
$ decrypt <cypher-filename> [<plain-filename>] <key> <date>
|
56
33
|
|
57
|
-
|
58
|
-
$crack <cypher-filename> [<plain-filename>] <date>
|
34
|
+
#### Cracking
|
35
|
+
$ crack <cypher-filename> [<plain-filename>] <date>
|
59
36
|
|
60
37
|
## Options
|
61
38
|
`<filename>` The file path of the of the file you want to encrypt. This can be the absolute filepath if you are in a different directory.
|
@@ -92,7 +69,7 @@ To crack a file named `file.encrypted.txt`
|
|
92
69
|
|
93
70
|
$ crack file.encrypted.txt 231190
|
94
71
|
|
95
|
-
=>#Created file.
|
72
|
+
=>#Created file.decrypted.txt with key 12345 and date 231190
|
96
73
|
|
97
74
|
|
98
75
|
## Contributing
|
@@ -107,7 +84,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/andela
|
|
107
84
|
6. Wait
|
108
85
|
|
109
86
|
## Limitations
|
110
|
-
* The character set of this gem is limited
|
87
|
+
* The character set of this gem is limited; lower case alphabets, numbers, space, comma and period characters.
|
111
88
|
* The application generates the the key for the encryption, and does not allow user to choose their prefered digit combinations
|
112
89
|
|
113
90
|
## Improvement
|
data/enygma.gemspec
CHANGED
@@ -9,21 +9,21 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Emmanuel Chigbo"]
|
10
10
|
spec.email = ["emmanuel.chigbo@andela.com"]
|
11
11
|
|
12
|
-
spec.summary = "A file encryption gem"
|
13
|
-
spec.description = "This gem uses Enigma algorithm to
|
14
|
-
|
15
|
-
|
12
|
+
spec.summary = "A file encryption and decryption gem"
|
13
|
+
spec.description = "This gem uses Enigma algorithm to encrypt and decrypt"\
|
14
|
+
"a file. It also provides you with the ability to crack a file that was"\
|
15
|
+
" encrypted with 'enygma'"
|
16
16
|
spec.homepage = "https://github.com/andela-echigbo/enygma"
|
17
17
|
spec.license = "MIT"
|
18
18
|
|
19
19
|
spec.files = `git ls-files -z`.split("\x0")
|
20
20
|
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
21
|
spec.bindir = "bin"
|
22
|
-
spec.executables = %w(encrypt decrypt crack)
|
22
|
+
spec.executables = %w(encrypt decrypt crack setup console)
|
23
23
|
spec.require_paths = ["lib"]
|
24
24
|
|
25
25
|
spec.add_development_dependency 'bundler', '~> 1.10', '>= 1.10.6'
|
26
26
|
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
-
spec.add_development_dependency 'rspec'
|
27
|
+
spec.add_development_dependency 'rspec'
|
28
28
|
spec.add_development_dependency "coveralls", "0.8.2"
|
29
29
|
end
|
data/lib/enygma.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require "enygma/version"
|
2
|
-
require "enygma/encryptor"
|
3
|
-
require "enygma/decryptor"
|
4
1
|
require "enygma/cracker"
|
2
|
+
require "enygma/decryptor"
|
3
|
+
require "enygma/encryptor"
|
5
4
|
require "enygma/helpers/filer"
|
6
5
|
require "enygma/helpers/key_gen"
|
7
6
|
require "enygma/helpers/offset"
|
8
7
|
require "enygma/helpers/rotator"
|
9
8
|
require "enygma/mixins/character_mapper"
|
10
9
|
require "enygma/mixins/confirmation"
|
10
|
+
require "enygma/version"
|
data/lib/enygma/cracker.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
require 'enygma/
|
2
|
-
require 'enygma/helpers/offset'
|
1
|
+
require 'enygma/decryptor'
|
3
2
|
require 'enygma/helpers/filer'
|
4
3
|
require 'enygma/helpers/key_gen'
|
5
|
-
require 'enygma/
|
4
|
+
require 'enygma/helpers/offset'
|
5
|
+
require 'enygma/mixins/character_mapper'
|
6
6
|
|
7
7
|
module Enygma
|
8
8
|
class Cracker
|
9
9
|
PLAIN_LAST_7_CHARACTERS = "..end.."
|
10
|
+
|
10
11
|
def initialize(cypher_filename, encryption_date, plain_filename = nil)
|
11
12
|
@cypher_filename = cypher_filename
|
12
13
|
@plain_filename = plain_filename
|
@@ -16,30 +17,34 @@ module Enygma
|
|
16
17
|
end
|
17
18
|
|
18
19
|
def crack
|
19
|
-
|
20
|
-
cypher_last_4_characters =
|
21
|
-
cypher_last_4_characters.rotate!(4 - (
|
22
|
-
plain_last_4_characters = PLAIN_LAST_7_CHARACTERS.split(
|
23
|
-
plain_last_4_characters.rotate!(4 - (
|
24
|
-
offset_characters = @offset.split(
|
25
|
-
|
26
|
-
|
27
|
-
|
20
|
+
cypher_characters = Filer.read(@cypher_filename)
|
21
|
+
cypher_last_4_characters = cypher_characters.last(4)
|
22
|
+
cypher_last_4_characters.rotate!(4 - (cypher_characters.size % 4))
|
23
|
+
plain_last_4_characters = PLAIN_LAST_7_CHARACTERS.split("").last(4)
|
24
|
+
plain_last_4_characters.rotate!(4 - (cypher_characters.size % 4))
|
25
|
+
offset_characters = @offset.split("")
|
26
|
+
differences = get_differences(
|
27
|
+
cypher_last_4_characters,
|
28
|
+
plain_last_4_characters,
|
29
|
+
offset_characters
|
28
30
|
)
|
29
|
-
|
31
|
+
|
32
|
+
key = KeyGen.get_key(differences)
|
30
33
|
Decryptor.new(@cypher_filename, key, @encryption_date, @plain_filename).
|
31
|
-
|
34
|
+
decrypt
|
32
35
|
end
|
33
36
|
|
34
|
-
def
|
35
|
-
|
37
|
+
def get_differences(cypher_array, plain_array, offset_array)
|
38
|
+
differences = []
|
36
39
|
4.times do |i|
|
37
40
|
diff = Enygma::CHARACTER_MAP.index(cypher_array[i]) -
|
38
|
-
|
41
|
+
Enygma::CHARACTER_MAP.index(plain_array[i])
|
42
|
+
|
39
43
|
diff -= offset_array[i].to_i
|
40
|
-
|
44
|
+
differences[i] = diff.to_s.rjust(2, '0')
|
41
45
|
end
|
42
|
-
|
46
|
+
|
47
|
+
differences
|
43
48
|
end
|
44
49
|
end
|
45
50
|
end
|
data/lib/enygma/decryptor.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require 'enygma/mixins/character_mapper'
|
2
|
-
require 'enygma/helpers/offset'
|
3
1
|
require 'enygma/helpers/filer'
|
2
|
+
require 'enygma/helpers/offset'
|
4
3
|
require 'enygma/helpers/rotator'
|
4
|
+
require 'enygma/mixins/character_mapper'
|
5
5
|
require 'enygma/mixins/confirmation'
|
6
6
|
|
7
7
|
module Enygma
|
@@ -9,8 +9,12 @@ module Enygma
|
|
9
9
|
include Confirmation
|
10
10
|
attr_reader :plain_filename, :cypher_filename, :encryption_key,
|
11
11
|
:encryption_date, :decrypted
|
12
|
-
|
13
|
-
|
12
|
+
|
13
|
+
def initialize(
|
14
|
+
cypher_filename,
|
15
|
+
encryption_key,
|
16
|
+
encryption_date,
|
17
|
+
plain_filename = nil
|
14
18
|
)
|
15
19
|
@cypher_filename = cypher_filename
|
16
20
|
@plain_filename = plain_filename
|
@@ -23,30 +27,36 @@ module Enygma
|
|
23
27
|
def decrypt
|
24
28
|
begin
|
25
29
|
cypher_characters = Filer.read(@cypher_filename)
|
26
|
-
cypher_characters.each_slice(4)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
cypher_characters.each_slice(4) { |batch| decrypt_batch(batch) }
|
31
|
+
@plain_filename = Filer.write(
|
32
|
+
@plain_filename,
|
33
|
+
@decrypted,
|
34
|
+
@cypher_filename,
|
35
|
+
"decrypted"
|
31
36
|
)
|
32
|
-
|
33
|
-
|
37
|
+
|
38
|
+
show_confirmation_message(
|
39
|
+
@plain_filename,
|
40
|
+
@encryption_key,
|
41
|
+
@encryption_date
|
34
42
|
)
|
35
43
|
rescue StandardError => e
|
36
|
-
puts "Could not open the file you supplied.
|
37
|
-
Make sure you are correctly typing the correct path #{e.message}"
|
44
|
+
puts "Could not open the file you supplied."\
|
45
|
+
"Make sure you are correctly typing the correct path #{e.message}"
|
38
46
|
end
|
39
47
|
end
|
40
48
|
|
41
49
|
def decrypt_batch(batch)
|
42
50
|
key_characters = @encryption_key.split("")
|
43
|
-
offset_characters = @offset.split(
|
51
|
+
offset_characters = @offset.split("")
|
44
52
|
batch.each_with_index do |_value, index|
|
45
|
-
new_index = Rotator.rotate(
|
46
|
-
|
47
|
-
|
48
|
-
|
53
|
+
new_index = Rotator.rotate(
|
54
|
+
key_characters[index],
|
55
|
+
key_characters[index + 1],
|
56
|
+
offset_characters[index],
|
57
|
+
batch[index]
|
49
58
|
) { |x, y| x - y }
|
59
|
+
|
50
60
|
@decrypted += Enygma::CHARACTER_MAP[new_index]
|
51
61
|
end
|
52
62
|
end
|
data/lib/enygma/encryptor.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
require 'enygma/mixins/character_mapper'
|
2
|
-
require 'enygma/helpers/offset'
|
3
1
|
require 'enygma/helpers/filer'
|
2
|
+
require 'enygma/helpers/offset'
|
4
3
|
require 'enygma/helpers/rotator'
|
4
|
+
require 'enygma/mixins/character_mapper'
|
5
5
|
require 'enygma/mixins/confirmation'
|
6
6
|
|
7
7
|
module Enygma
|
@@ -9,10 +9,11 @@ module Enygma
|
|
9
9
|
include Confirmation
|
10
10
|
attr_reader :filename, :cypher_filename, :offset, :encryption_key,
|
11
11
|
:encryption_date, :encrypted
|
12
|
+
|
12
13
|
def initialize(filename, cypher_filename = nil)
|
13
14
|
@filename = filename
|
14
15
|
@cypher_filename = cypher_filename
|
15
|
-
@encryption_key = rand(10**5).to_s.rjust(5,'0')
|
16
|
+
@encryption_key = rand(10**5).to_s.rjust(5, '0')
|
16
17
|
@encryption_date = Time.now.strftime("%d%m%y")
|
17
18
|
@offset = Offset.get_offset(@encryption_date)
|
18
19
|
@encrypted = ""
|
@@ -21,18 +22,22 @@ module Enygma
|
|
21
22
|
def encrypt
|
22
23
|
begin
|
23
24
|
plain_characters = Filer.read(@filename)
|
24
|
-
plain_characters.each_slice(4)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
plain_characters.each_slice(4) { |batch| encrypt_batch(batch) }
|
26
|
+
@cypher_filename = Filer.write(
|
27
|
+
@cypher_filename,
|
28
|
+
@encrypted,
|
29
|
+
@filename,
|
30
|
+
"encrypted"
|
29
31
|
)
|
30
|
-
|
31
|
-
|
32
|
+
|
33
|
+
show_confirmation_message(
|
34
|
+
@cypher_filename,
|
35
|
+
@encryption_key,
|
36
|
+
@encryption_date
|
32
37
|
)
|
33
38
|
rescue StandardError => e
|
34
|
-
puts "Could not open the file you supplied.
|
35
|
-
Make sure you are correctly typing the correct path. #{e.message}"
|
39
|
+
puts "Could not open the file you supplied."\
|
40
|
+
"Make sure you are correctly typing the correct path. #{e.message}"
|
36
41
|
end
|
37
42
|
end
|
38
43
|
|
@@ -40,10 +45,13 @@ Make sure you are correctly typing the correct path. #{e.message}"
|
|
40
45
|
key_characters = @encryption_key.split('')
|
41
46
|
offset_characters = @offset.split('')
|
42
47
|
batch.each_with_index do |_value, index|
|
43
|
-
new_index = Rotator.rotate(
|
44
|
-
|
45
|
-
|
48
|
+
new_index = Rotator.rotate(
|
49
|
+
key_characters[index],
|
50
|
+
key_characters[index + 1],
|
51
|
+
offset_characters[index],
|
52
|
+
batch[index]
|
46
53
|
) { |x, y| x + y }
|
54
|
+
|
47
55
|
@encrypted += Enygma::CHARACTER_MAP[new_index]
|
48
56
|
end
|
49
57
|
end
|
data/lib/enygma/helpers/filer.rb
CHANGED
@@ -5,12 +5,14 @@ module Enygma
|
|
5
5
|
content.split('')
|
6
6
|
end
|
7
7
|
|
8
|
+
# TODO: use options params instead
|
8
9
|
def self.write(dest_file, content, source_file = '', action = '')
|
9
10
|
unless dest_file
|
10
|
-
|
11
|
-
|
12
|
-
dest_file =
|
11
|
+
name_split = source_file.split('.')
|
12
|
+
name_split.delete("encrypted")
|
13
|
+
dest_file = name_split.insert(-2, action).join('.')
|
13
14
|
end
|
15
|
+
|
14
16
|
File.open(dest_file, "w") { |file| file.write(content) }
|
15
17
|
dest_file
|
16
18
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require "enygma/mixins/character_mapper"
|
2
|
+
|
2
3
|
module Enygma
|
3
4
|
class KeyGen
|
4
5
|
def self.get_key(differences)
|
@@ -7,21 +8,34 @@ module Enygma
|
|
7
8
|
break if index == differences.size - 1
|
8
9
|
value1 = value
|
9
10
|
value2 = differences[index + 1]
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
11
|
+
|
12
|
+
compare(index, value1, value2, tracker, differences)
|
13
|
+
end
|
14
|
+
|
15
|
+
"%d%d%d%d" % [
|
16
|
+
differences[0],
|
17
|
+
differences[1][-1],
|
18
|
+
differences[2][-1],
|
19
|
+
differences[3][-1]
|
20
|
+
]
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def self.compare(index, value1, value2, tracker, differences)
|
25
|
+
until value1.to_i >= 100 || tracker[index]
|
26
|
+
until value2.to_i >= 100 || tracker[index]
|
27
|
+
if value1[1] == value2[0]
|
28
|
+
differences[index] = value1
|
29
|
+
differences[index + 1] = value2
|
30
|
+
tracker[index] = true
|
31
|
+
else
|
32
|
+
value2 = (value2.to_i + Enygma::CHARACTER_MAP.size).to_s
|
19
33
|
end
|
20
|
-
value2 = differences[index + 1]
|
21
|
-
value1 = (value1.to_i + Enygma::CHARACTER_MAP.size).to_s
|
22
34
|
end
|
35
|
+
|
36
|
+
value2 = differences[index + 1]
|
37
|
+
value1 = (value1.to_i + Enygma::CHARACTER_MAP.size).to_s
|
23
38
|
end
|
24
|
-
"#{differences[0]}#{differences[1][-1]}#{differences[2][-1]}#{differences[3][-1]}"
|
25
39
|
end
|
26
40
|
end
|
27
41
|
end
|
@@ -1,12 +1,16 @@
|
|
1
1
|
require "enygma/mixins/character_mapper"
|
2
|
+
|
2
3
|
module Enygma
|
3
4
|
class Rotator
|
4
|
-
def self.rotate(
|
5
|
-
|
5
|
+
def self.rotate(
|
6
|
+
key_character_1,
|
7
|
+
key_character_2,
|
8
|
+
offset_character,
|
9
|
+
character
|
6
10
|
)
|
7
11
|
character_index = Enygma::CHARACTER_MAP.index(character)
|
8
12
|
total_shift = (key_character_1 + key_character_2).to_i +
|
9
|
-
|
13
|
+
offset_character.to_i
|
10
14
|
rotation = yield(character_index, total_shift)
|
11
15
|
rotation % Enygma::CHARACTER_MAP.size
|
12
16
|
end
|
data/lib/enygma/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enygma
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emmanuel Chigbo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -48,14 +48,14 @@ dependencies:
|
|
48
48
|
name: rspec
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- - "
|
51
|
+
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: '0'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
|
-
- - "
|
58
|
+
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0'
|
61
61
|
- !ruby/object:Gem::Dependency
|
@@ -72,16 +72,16 @@ dependencies:
|
|
72
72
|
- - '='
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: 0.8.2
|
75
|
-
description:
|
76
|
-
|
77
|
-
encrypt and decrypt a file. It also provides you
|
78
|
-
with the ability to crack a file that was encrypted with 'enygma'
|
75
|
+
description: This gem uses Enigma algorithm to encrypt and decrypta file. It also
|
76
|
+
provides you with the ability to crack a file that was encrypted with 'enygma'
|
79
77
|
email:
|
80
78
|
- emmanuel.chigbo@andela.com
|
81
79
|
executables:
|
82
80
|
- encrypt
|
83
81
|
- decrypt
|
84
82
|
- crack
|
83
|
+
- setup
|
84
|
+
- console
|
85
85
|
extensions: []
|
86
86
|
extra_rdoc_files: []
|
87
87
|
files:
|
@@ -137,5 +137,5 @@ rubyforge_project:
|
|
137
137
|
rubygems_version: 2.4.8
|
138
138
|
signing_key:
|
139
139
|
specification_version: 4
|
140
|
-
summary: A file encryption gem
|
140
|
+
summary: A file encryption and decryption gem
|
141
141
|
test_files: []
|