enygma 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 +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +115 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/crack +23 -0
- data/bin/decrypt +23 -0
- data/bin/encrypt +19 -0
- data/bin/setup +7 -0
- data/circle.yml +7 -0
- data/default.txt +1 -0
- data/default_enc.txt +1 -0
- data/enygma.gemspec +29 -0
- data/lib/enygma.rb +10 -0
- data/lib/enygma/cracker.rb +45 -0
- data/lib/enygma/decryptor.rb +54 -0
- data/lib/enygma/encryptor.rb +51 -0
- data/lib/enygma/helpers/filer.rb +18 -0
- data/lib/enygma/helpers/help.rb +48 -0
- data/lib/enygma/helpers/key_gen.rb +27 -0
- data/lib/enygma/helpers/offset.rb +8 -0
- data/lib/enygma/helpers/rotator.rb +14 -0
- data/lib/enygma/mixins/character_mapper.rb +3 -0
- data/lib/enygma/mixins/confirmation.rb +7 -0
- data/lib/enygma/test_file.txt +1 -0
- data/lib/enygma/version.rb +3 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2f76a0055e8c3902ff594eef74db248e3841263a
|
4
|
+
data.tar.gz: d60c6eecd0b61b3dc531f4d7d5edfc72404fd6aa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 89bc34b5057e4889970a789d7ee6a918db8002133ce84e149f84cadde8c98b28625546a0230fb109b058e3f164e4d418a845dc41af020efb07d633a9f35050ce
|
7
|
+
data.tar.gz: 1fce60030010026281f58c97e2683a53a8cb6353641decf7779e48f71275089e6ccc599cbecb371d882d54f649cd46e5c534e15e40d74dcffda057208d554537
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
repo_token: 0Bfmfua5syue0U4FdULPzfiggPNtEXJRD
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Emmanuel Chigbo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
# Enygma [](https://coveralls.io/github/andela-echigbo/enygma?branch=master) [](https://circleci.com/gh/andela-echigbo/enygma)
|
2
|
+
|
3
|
+
Enygma is an encryptor 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 crack a file.
|
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)
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
To install as a gem and run as a termina/command line program, run the following command in you terminal(command promt for Windows)
|
31
|
+
|
32
|
+
$ gem install enygma
|
33
|
+
|
34
|
+
If you want to use 'Enygma' in your ruby application, add this line to your application's Gemfile:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
gem 'enygma'
|
38
|
+
```
|
39
|
+
|
40
|
+
And then execute:
|
41
|
+
|
42
|
+
$ bundle
|
43
|
+
|
44
|
+
|
45
|
+
## Usage
|
46
|
+
|
47
|
+
This gem provides you with three command line actions, `encrypt`, `decrypt`, and `crack`
|
48
|
+
|
49
|
+
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
|
+
|
51
|
+
### Encryption
|
52
|
+
$ encrypt <filename> [<destination-filename>]
|
53
|
+
|
54
|
+
### Decryption
|
55
|
+
$ decrypt <cypher-filename> [<plain-filename>] <key> <date>
|
56
|
+
|
57
|
+
### Cracking
|
58
|
+
$crack <cypher-filename> [<plain-filename>] <date>
|
59
|
+
|
60
|
+
## Options
|
61
|
+
`<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.
|
62
|
+
|
63
|
+
`<destination-filename>` This is optional. If provided, is tThe filename where the encrypted text will the saved. Otherwise, the filename is deduce from the source `filename`
|
64
|
+
|
65
|
+
`<cypher-filename>` The filename/filepath to the encrypted file you want decrypt.
|
66
|
+
|
67
|
+
`<plain-filename>` The destination filename to save the plain text. Optional. If not provided, the plain text is stored is a file with name coined from the `cypher-filename`
|
68
|
+
|
69
|
+
`<key>` A randomly generated unique 5 digits number used to encrypt the file. The key is displayed to you on a confirmation message when you encrypted the file.
|
70
|
+
|
71
|
+
`<date>` The date the file was encrypted. This will also be displayed in a confirmation message in the terminal in the format, `ddmmyy`
|
72
|
+
|
73
|
+
## Example Usage
|
74
|
+
|
75
|
+
To encrypt a file name `file.txt`, and in whose containing I have changed to
|
76
|
+
|
77
|
+
$ encrypt file.txt
|
78
|
+
|
79
|
+
=>#Created file.encrypted.txt with the key 12345 and date 231190
|
80
|
+
|
81
|
+
$ encrypt file.txt encrypted.txt
|
82
|
+
|
83
|
+
=>#Created encrypted.txt with the key 12345 and date 231190
|
84
|
+
|
85
|
+
To decrypt a file named `file.encrypted.txt`
|
86
|
+
|
87
|
+
$ decrypt file.encrypted.txt 12345 231190
|
88
|
+
|
89
|
+
=>#Created file.decrypted.txt with key 12345 and date 231190
|
90
|
+
|
91
|
+
To crack a file named `file.encrypted.txt`
|
92
|
+
|
93
|
+
$ crack file.encrypted.txt 231190
|
94
|
+
|
95
|
+
=>#Created file.cracked.txt with key 12345 and date 231190
|
96
|
+
|
97
|
+
|
98
|
+
## Contributing
|
99
|
+
|
100
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/andela-echigbo/enygma. This project is intended to be a safe and welcoming space for collaboration. To contribute to this work:
|
101
|
+
|
102
|
+
1. Fork it ( https://github.com/[andela-echigbo]/enygma/fork )
|
103
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
104
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
105
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
106
|
+
5. Create a new Pull Request
|
107
|
+
6. Wait
|
108
|
+
|
109
|
+
## Limitations
|
110
|
+
* The character set of this gem is limited. Lower case alphabets, numbers, space, comma and period characters.
|
111
|
+
* The application generates the the key for the encryption, and does not allow user to choose their prefered digit combinations
|
112
|
+
|
113
|
+
## Improvement
|
114
|
+
* The character set suported by the gem will be increased
|
115
|
+
* The application should able to allow users choose their encryption key upon encrypting a file
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "enygma"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/crack
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'enygma/cracker'
|
4
|
+
require 'enygma/helpers/help'
|
5
|
+
|
6
|
+
if ARGV[0] && (ARGV[0] == 'help' || ARGV[0] == '--help' || ARGV[0] == '-h')
|
7
|
+
Enygma::Help.crack
|
8
|
+
elsif ARGV.length < 2 || ARGV.length > 3
|
9
|
+
puts "Incorrect number of argument. Run with --help for explanation"
|
10
|
+
#Enygma::Help.crack
|
11
|
+
elsif !File.file?(ARGV[0])
|
12
|
+
puts "The input file path does not exist. Run with --help for explanation"
|
13
|
+
#Enygma::Help.crack
|
14
|
+
elsif ARGV.length == 3 && File.file?(ARGV[1])
|
15
|
+
puts "The output file path cannot be created, or file already exist. Run with --help for explanation"
|
16
|
+
#Enygma::Help.crack
|
17
|
+
else
|
18
|
+
if ARGV.length == 3
|
19
|
+
Enygma::Cracker.new(ARGV[0], ARGV[2], ARGV[1]).crack
|
20
|
+
else
|
21
|
+
Enygma::Cracker.new(ARGV[0], ARGV[1]).crack
|
22
|
+
end
|
23
|
+
end
|
data/bin/decrypt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'enygma/decryptor'
|
4
|
+
require 'enygma/helpers/help'
|
5
|
+
|
6
|
+
if ARGV[0] && (ARGV[0] == 'help' || ARGV[0] == '--help' || ARGV[0] == '-h')
|
7
|
+
Enygma::Help.decrypt
|
8
|
+
elsif ARGV.length < 3 || ARGV.length > 4
|
9
|
+
puts "Incorrect number of argument. Run with --help for explanation"
|
10
|
+
#Enygma::Help.decrypt
|
11
|
+
elsif !File.file?(ARGV[0])
|
12
|
+
puts "The input file path does not exist. Run with --help for explanation"
|
13
|
+
#Enygma::Help.decrypt
|
14
|
+
elsif ARGV.length == 4 && File.file?(ARGV[1])
|
15
|
+
puts "The output file path cannot be created, or file already exist. Run with --help for explanation"
|
16
|
+
#Enygma::Help.decrypt
|
17
|
+
else
|
18
|
+
if ARGV.length == 4
|
19
|
+
Enygma::Decryptor.new(ARGV[0], ARGV[2], ARGV[3], ARGV[1]).decrypt
|
20
|
+
else
|
21
|
+
Enygma::Decryptor.new(ARGV[0], ARGV[1], ARGV[2]).decrypt
|
22
|
+
end
|
23
|
+
end
|
data/bin/encrypt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'enygma/encryptor'
|
4
|
+
require 'enygma/helpers/help'
|
5
|
+
|
6
|
+
if ARGV[0] && (ARGV[0] == 'help' || ARGV[0] == '--help' || ARGV[0] == '-h')
|
7
|
+
Enygma::Help.encrypt
|
8
|
+
elsif ARGV.length < 1 || ARGV.length > 2
|
9
|
+
puts "Incorrect number of argument. Run with --help for explanation"
|
10
|
+
#Enygma::Help.encrypt
|
11
|
+
elsif !File.file?(ARGV[0])
|
12
|
+
puts "The input file path does not exist. Run with --help for explanation"
|
13
|
+
#Enygma::Help.encrypt
|
14
|
+
elsif ARGV[1] && File.file?(ARGV[1])
|
15
|
+
puts "The output file path cannot be created, or file already exist. Run with --help for explanation"
|
16
|
+
#Enygma::Help.encrypt
|
17
|
+
else
|
18
|
+
Enygma::Encryptor.new(ARGV[0], ARGV[1]).encrypt
|
19
|
+
end
|
data/bin/setup
ADDED
data/circle.yml
ADDED
data/default.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
some default content
|
data/default_enc.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ar94sg15vx8gsf.abh g
|
data/enygma.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enygma/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "enygma"
|
8
|
+
spec.version = Enygma::VERSION
|
9
|
+
spec.authors = ["Emmanuel Chigbo"]
|
10
|
+
spec.email = ["emmanuel.chigbo@andela.com"]
|
11
|
+
|
12
|
+
spec.summary = "A file encryption gem"
|
13
|
+
spec.description = "This gem uses Enigma algorithm to
|
14
|
+
encrypt and decrypt a file. It also provides you
|
15
|
+
with the ability to crack a file that was encrypted with 'enygma'"
|
16
|
+
spec.homepage = "https://github.com/andela-echigbo/enygma"
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0")
|
20
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
spec.bindir = "bin"
|
22
|
+
spec.executables = %w(encrypt decrypt crack)
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.10', '>= 1.10.6'
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency 'rspec', '~> 0'
|
28
|
+
spec.add_development_dependency "coveralls", "0.8.2"
|
29
|
+
end
|
data/lib/enygma.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require "enygma/version"
|
2
|
+
require "enygma/encryptor"
|
3
|
+
require "enygma/decryptor"
|
4
|
+
require "enygma/cracker"
|
5
|
+
require "enygma/helpers/filer"
|
6
|
+
require "enygma/helpers/key_gen"
|
7
|
+
require "enygma/helpers/offset"
|
8
|
+
require "enygma/helpers/rotator"
|
9
|
+
require "enygma/mixins/character_mapper"
|
10
|
+
require "enygma/mixins/confirmation"
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'enygma/mixins/character_mapper'
|
2
|
+
require 'enygma/helpers/offset'
|
3
|
+
require 'enygma/helpers/filer'
|
4
|
+
require 'enygma/helpers/key_gen'
|
5
|
+
require 'enygma/decryptor'
|
6
|
+
|
7
|
+
module Enygma
|
8
|
+
class Cracker
|
9
|
+
PLAIN_LAST_7_CHARACTERS = "..end.."
|
10
|
+
def initialize(cypher_filename, encryption_date, plain_filename = nil)
|
11
|
+
@cypher_filename = cypher_filename
|
12
|
+
@plain_filename = plain_filename
|
13
|
+
@encryption_date = encryption_date
|
14
|
+
@offset = Offset.get_offset(@encryption_date)
|
15
|
+
@decrypted = ""
|
16
|
+
end
|
17
|
+
|
18
|
+
def crack
|
19
|
+
character_array = Filer.read(@cypher_filename)
|
20
|
+
cypher_last_4_characters = character_array.last(4)
|
21
|
+
cypher_last_4_characters.rotate!(4 - (character_array.size % 4))
|
22
|
+
plain_last_4_characters = PLAIN_LAST_7_CHARACTERS.split('').last(4)
|
23
|
+
plain_last_4_characters.rotate!(4 - (character_array.size % 4))
|
24
|
+
offset_characters = @offset.split('')
|
25
|
+
diff_array = get_difference_array(cypher_last_4_characters,
|
26
|
+
plain_last_4_characters,
|
27
|
+
offset_characters
|
28
|
+
)
|
29
|
+
key = KeyGen.get_key(diff_array)
|
30
|
+
Decryptor.new(@cypher_filename, key, @encryption_date, @plain_filename).
|
31
|
+
decrypt
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_difference_array(cypher_array, plain_array, offset_array)
|
35
|
+
difference = []
|
36
|
+
4.times do |i|
|
37
|
+
diff = Enygma::CHARACTER_MAP.index(cypher_array[i]) -
|
38
|
+
Enygma::CHARACTER_MAP.index(plain_array[i])
|
39
|
+
diff -= offset_array[i].to_i
|
40
|
+
difference[i] = diff.to_s.rjust(2, '0')
|
41
|
+
end
|
42
|
+
difference
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'enygma/mixins/character_mapper'
|
2
|
+
require 'enygma/helpers/offset'
|
3
|
+
require 'enygma/helpers/filer'
|
4
|
+
require 'enygma/helpers/rotator'
|
5
|
+
require 'enygma/mixins/confirmation'
|
6
|
+
|
7
|
+
module Enygma
|
8
|
+
class Decryptor
|
9
|
+
include Confirmation
|
10
|
+
attr_reader :plain_filename, :cypher_filename, :encryption_key,
|
11
|
+
:encryption_date, :decrypted
|
12
|
+
def initialize(cypher_filename, encryption_key, encryption_date,
|
13
|
+
plain_filename = nil
|
14
|
+
)
|
15
|
+
@cypher_filename = cypher_filename
|
16
|
+
@plain_filename = plain_filename
|
17
|
+
@encryption_key = encryption_key
|
18
|
+
@encryption_date = encryption_date
|
19
|
+
@offset = Offset.get_offset(@encryption_date)
|
20
|
+
@decrypted = ""
|
21
|
+
end
|
22
|
+
|
23
|
+
def decrypt
|
24
|
+
begin
|
25
|
+
cypher_characters = Filer.read(@cypher_filename)
|
26
|
+
cypher_characters.each_slice(4) do |batch|
|
27
|
+
decrypt_batch(batch)
|
28
|
+
end
|
29
|
+
@plain_filename = Filer.write(@plain_filename, @decrypted,
|
30
|
+
@cypher_filename, "decrypted"
|
31
|
+
)
|
32
|
+
show_confirmation_message(@plain_filename, @encryption_key,
|
33
|
+
@encryption_date
|
34
|
+
)
|
35
|
+
rescue StandardError => e
|
36
|
+
puts "Could not open the file you supplied.
|
37
|
+
Make sure you are correctly typing the correct path #{e.message}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def decrypt_batch(batch)
|
42
|
+
key_characters = @encryption_key.split("")
|
43
|
+
offset_characters = @offset.split('')
|
44
|
+
batch.each_with_index do |_value, index|
|
45
|
+
new_index = Rotator.rotate(key_characters[index],
|
46
|
+
key_characters[index + 1],
|
47
|
+
offset_characters[index],
|
48
|
+
batch[index]
|
49
|
+
) { |x, y| x - y }
|
50
|
+
@decrypted += Enygma::CHARACTER_MAP[new_index]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'enygma/mixins/character_mapper'
|
2
|
+
require 'enygma/helpers/offset'
|
3
|
+
require 'enygma/helpers/filer'
|
4
|
+
require 'enygma/helpers/rotator'
|
5
|
+
require 'enygma/mixins/confirmation'
|
6
|
+
|
7
|
+
module Enygma
|
8
|
+
class Encryptor
|
9
|
+
include Confirmation
|
10
|
+
attr_reader :filename, :cypher_filename, :offset, :encryption_key,
|
11
|
+
:encryption_date, :encrypted
|
12
|
+
def initialize(filename, cypher_filename = nil)
|
13
|
+
@filename = filename
|
14
|
+
@cypher_filename = cypher_filename
|
15
|
+
@encryption_key = rand(10**5).to_s.rjust(5,'0')
|
16
|
+
@encryption_date = Time.now.strftime("%d%m%y")
|
17
|
+
@offset = Offset.get_offset(@encryption_date)
|
18
|
+
@encrypted = ""
|
19
|
+
end
|
20
|
+
|
21
|
+
def encrypt
|
22
|
+
begin
|
23
|
+
plain_characters = Filer.read(@filename)
|
24
|
+
plain_characters.each_slice(4) do |batch|
|
25
|
+
encrypt_batch(batch)
|
26
|
+
end
|
27
|
+
@cypher_filename = Filer.write(@cypher_filename, @encrypted, @filename,
|
28
|
+
"encrypted"
|
29
|
+
)
|
30
|
+
show_confirmation_message(@cypher_filename, @encryption_key,
|
31
|
+
@encryption_date
|
32
|
+
)
|
33
|
+
rescue StandardError => e
|
34
|
+
puts "Could not open the file you supplied.
|
35
|
+
Make sure you are correctly typing the correct path. #{e.message}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def encrypt_batch(batch)
|
40
|
+
key_characters = @encryption_key.split('')
|
41
|
+
offset_characters = @offset.split('')
|
42
|
+
batch.each_with_index do |_value, index|
|
43
|
+
new_index = Rotator.rotate(key_characters[index],
|
44
|
+
key_characters[index + 1],
|
45
|
+
offset_characters[index], batch[index]
|
46
|
+
) { |x, y| x + y }
|
47
|
+
@encrypted += Enygma::CHARACTER_MAP[new_index]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Enygma
|
2
|
+
class Filer
|
3
|
+
def self.read(filename)
|
4
|
+
content = File.open(filename, 'r') { |file| file.read }
|
5
|
+
content.split('')
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.write(dest_file, content, source_file = '', action = '')
|
9
|
+
unless dest_file
|
10
|
+
name_split_array = source_file.split('.')
|
11
|
+
name_split_array.delete("encrypted")
|
12
|
+
dest_file = name_split_array.insert(-2, action).join('.')
|
13
|
+
end
|
14
|
+
File.open(dest_file, "w") { |file| file.write(content) }
|
15
|
+
dest_file
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Enygma
|
2
|
+
class Help
|
3
|
+
def self.encrypt
|
4
|
+
puts(<<-HERE)
|
5
|
+
|
6
|
+
To encrypt a file"
|
7
|
+
|
8
|
+
Usage:
|
9
|
+
encrypt <file> [<output>]
|
10
|
+
|
11
|
+
Options:
|
12
|
+
<file>\t\t The path to the input file to be encrypted
|
13
|
+
<output>\t (Optional) The path to the output file to save the cypher text. If not provided, the new name will be deduced from the source name
|
14
|
+
HERE
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.decrypt
|
18
|
+
puts(<<-HERE)
|
19
|
+
|
20
|
+
To decrypt a file encrypted with 'Enygma'
|
21
|
+
|
22
|
+
Usage:
|
23
|
+
decrypt <cypher-file> [<output>] <key> <date>
|
24
|
+
|
25
|
+
Options:
|
26
|
+
<cypher-file>\t The path to the input cypher file to be decrypted
|
27
|
+
<output>\t (Optional) The path to the output file to save the plain text. If not provided, the new name will be deduced from the source name
|
28
|
+
<key>\t\t The key used to encrypt the file
|
29
|
+
<date>\t\t The date the file was encrypted in this format: ddmmyy (eg. 061115)
|
30
|
+
HERE
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.crack
|
34
|
+
puts(<<-HERE)
|
35
|
+
|
36
|
+
To crack a file encrypted with 'Enygma', given that you know the \"encryption date\"
|
37
|
+
|
38
|
+
Usage:
|
39
|
+
crack <cypher-file> [<output>] <date>
|
40
|
+
|
41
|
+
Options:
|
42
|
+
<cypher-file>\t The path to the input cypher file to be decrypted
|
43
|
+
<output>\t (Optional) The path to the output file to save the plain text. If not provided, the new name will be deduced from the source name
|
44
|
+
<date>\t\t The date the file was encrypted in this format: ddmmyy (eg. 061115)
|
45
|
+
HERE
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "enygma/mixins/character_mapper"
|
2
|
+
module Enygma
|
3
|
+
class KeyGen
|
4
|
+
def self.get_key(differences)
|
5
|
+
tracker = []
|
6
|
+
differences.each_with_index do |value, index|
|
7
|
+
break if index == differences.size - 1
|
8
|
+
value1 = value
|
9
|
+
value2 = differences[index + 1]
|
10
|
+
until value1.to_i >= 100 || tracker[index]
|
11
|
+
until value2.to_i >= 100 || tracker[index]
|
12
|
+
if value1[1] == value2[0]
|
13
|
+
differences[index] = value1
|
14
|
+
differences[index + 1] = value2
|
15
|
+
tracker[index] = true
|
16
|
+
else
|
17
|
+
value2 = (value2.to_i + Enygma::CHARACTER_MAP.size).to_s
|
18
|
+
end
|
19
|
+
end
|
20
|
+
value2 = differences[index + 1]
|
21
|
+
value1 = (value1.to_i + Enygma::CHARACTER_MAP.size).to_s
|
22
|
+
end
|
23
|
+
end
|
24
|
+
"#{differences[0]}#{differences[1][-1]}#{differences[2][-1]}#{differences[3][-1]}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "enygma/mixins/character_mapper"
|
2
|
+
module Enygma
|
3
|
+
class Rotator
|
4
|
+
def self.rotate(key_character_1, key_character_2, offset_character,
|
5
|
+
character
|
6
|
+
)
|
7
|
+
character_index = Enygma::CHARACTER_MAP.index(character)
|
8
|
+
total_shift = (key_character_1 + key_character_2).to_i +
|
9
|
+
offset_character.to_i
|
10
|
+
rotation = yield(character_index, total_shift)
|
11
|
+
rotation % Enygma::CHARACTER_MAP.size
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
hello world, this is a plain text. ..end..
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enygma
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Emmanuel Chigbo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.10.6
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.10'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.10.6
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '10.0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '10.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: coveralls
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 0.8.2
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 0.8.2
|
75
|
+
description: |-
|
76
|
+
This gem uses Enigma algorithm to
|
77
|
+
encrypt and decrypt a file. It also provides you
|
78
|
+
with the ability to crack a file that was encrypted with 'enygma'
|
79
|
+
email:
|
80
|
+
- emmanuel.chigbo@andela.com
|
81
|
+
executables:
|
82
|
+
- encrypt
|
83
|
+
- decrypt
|
84
|
+
- crack
|
85
|
+
extensions: []
|
86
|
+
extra_rdoc_files: []
|
87
|
+
files:
|
88
|
+
- ".coveralls.yml"
|
89
|
+
- ".gitignore"
|
90
|
+
- ".rspec"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- bin/console
|
96
|
+
- bin/crack
|
97
|
+
- bin/decrypt
|
98
|
+
- bin/encrypt
|
99
|
+
- bin/setup
|
100
|
+
- circle.yml
|
101
|
+
- default.txt
|
102
|
+
- default_enc.txt
|
103
|
+
- enygma.gemspec
|
104
|
+
- lib/enygma.rb
|
105
|
+
- lib/enygma/cracker.rb
|
106
|
+
- lib/enygma/decryptor.rb
|
107
|
+
- lib/enygma/encryptor.rb
|
108
|
+
- lib/enygma/helpers/filer.rb
|
109
|
+
- lib/enygma/helpers/help.rb
|
110
|
+
- lib/enygma/helpers/key_gen.rb
|
111
|
+
- lib/enygma/helpers/offset.rb
|
112
|
+
- lib/enygma/helpers/rotator.rb
|
113
|
+
- lib/enygma/mixins/character_mapper.rb
|
114
|
+
- lib/enygma/mixins/confirmation.rb
|
115
|
+
- lib/enygma/test_file.txt
|
116
|
+
- lib/enygma/version.rb
|
117
|
+
homepage: https://github.com/andela-echigbo/enygma
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.4.8
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: A file encryption gem
|
141
|
+
test_files: []
|