Multi-Alphabet-Encryption 1.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.
Files changed (2) hide show
  1. data/lib/main.rb +173 -0
  2. metadata +47 -0
@@ -0,0 +1,173 @@
1
+ # encoding: utf-8
2
+
3
+ class Encrypt
4
+
5
+ #global array, contains every charcter which can be encrypted.
6
+ $availiable_charcaters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"," ",",",".","'",'"',"!",";",":","?","@","#","=","+","-","_",")","(","&",'$',"£","*","/","\\","]","[","{","}","~","%","^","<",">","|"]
7
+
8
+ $generated_key = []
9
+
10
+ #encrypt method, passes 2 parameters, second must be integer
11
+ def encrypt(plain_text)
12
+
13
+ new_string = ""
14
+ #varify parameter 2 is an integer, if false return.
15
+
16
+
17
+ #splits the plain text and stores every character in an array
18
+ plain_text_character = plain_text.split("")
19
+
20
+ #assign each character to the element id, makes it easier to reference
21
+ array_index = Hash[$availiable_charcaters.map.with_index.to_a]
22
+
23
+ #set the counter variable to 0, allows to loop through each key line
24
+ count = 0
25
+
26
+ #loop through the split plain_text_character array
27
+ for character in plain_text_character
28
+
29
+ #check if the character is in the $availiable_characters array
30
+ if $availiable_charcaters.include? (character)
31
+
32
+ element_number = array_index[character].to_i
33
+
34
+ new_string = new_string + $generated_key[count][element_number].to_s
35
+
36
+ end
37
+
38
+ count += 1
39
+
40
+ if count > $generated_key.length - 1
41
+
42
+ count = 0
43
+
44
+ end
45
+
46
+ end
47
+
48
+ return new_string
49
+
50
+ end
51
+
52
+ #decryption method, passed 1 parameter
53
+ def decrypt(encrypted_text)
54
+
55
+ #split the encrypted string into an array
56
+ string_split = encrypted_text.split("")
57
+
58
+ array_count = 0
59
+
60
+ #new string variable
61
+ new_string = ""
62
+
63
+ #loop through each character in the string_split array
64
+ for letter in string_split
65
+
66
+ hash = Hash[$generated_key[array_count].map.with_index.to_a]
67
+
68
+ number = hash[letter].to_i
69
+
70
+ new_string = new_string + $availiable_charcaters[number].to_s
71
+
72
+ if array_count >= $generated_key.length - 1
73
+
74
+ array_count = 0
75
+
76
+ else
77
+
78
+ array_count += 1
79
+
80
+ end
81
+
82
+ end
83
+
84
+ #return the decoded string
85
+ return new_string
86
+
87
+ end
88
+
89
+ #load encryption key method, 1 parameter
90
+ def load_encryption_key(file_location)
91
+
92
+ file = File.open(file_location)
93
+
94
+ count = 0
95
+
96
+ file.each_line{ |file_line|
97
+
98
+ line_split = file_line.split("")
99
+
100
+ line_split.delete("\n")
101
+
102
+ $generated_key.push(line_split)
103
+ }
104
+ end
105
+
106
+ def generate_encryption_key(output_file_location, encryption_strength)
107
+
108
+ loop_count = 0
109
+
110
+ for key_generation_count in 0..encryption_strength - 1
111
+
112
+ #Add a new array to they Key storage aka multi-dimentional
113
+ $generated_key.push(Array.new())
114
+
115
+ #loop through each character in the $availiable_characters array to build the unique key.
116
+ while loop_count < $availiable_charcaters.length
117
+
118
+ #generate the random number which will be used to get the "letter"
119
+ random_number = Random.new
120
+
121
+ random_element_number = random_number.rand(0..$availiable_charcaters.length - 1)
122
+
123
+ #push the random letter to the key array
124
+ if !$generated_key[key_generation_count].include?($availiable_charcaters[random_element_number])
125
+
126
+ #if the character has not been entered into the generate_key array it will add it aka push()
127
+ $generated_key[key_generation_count].push($availiable_charcaters[random_element_number])
128
+
129
+ #increment loop character
130
+ loop_count += 1
131
+ else
132
+
133
+ #redo the current loop.
134
+ redo
135
+
136
+ end
137
+ end
138
+
139
+ #reset the loop count variable to populate the multiple arrays(depending on the key strength)
140
+ if loop_count >= $availiable_charcaters.length
141
+
142
+ loop_count = 0
143
+
144
+ end
145
+
146
+ end
147
+
148
+ output_key_file = File.open(output_file_location, "w") do |file|
149
+
150
+ for i in 0..$generated_key.length - 1
151
+
152
+ file_line = ""
153
+
154
+ #loop through chars and build file line
155
+ for count in 0..$availiable_charcaters.length - 1
156
+
157
+ #create the file line from each arrat element
158
+ file_line = file_line + $generated_key[i][count].to_s
159
+ end
160
+
161
+ #print file line and reset variable
162
+ file.puts(file_line)
163
+
164
+ #reset the file_line variable
165
+ file_line = ""
166
+
167
+ end
168
+
169
+ end
170
+
171
+ end
172
+
173
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Multi-Alphabet-Encryption
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Peter Arnell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-02-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Multiple alphabet encryption allows you en encrypt any text via a randomly
15
+ generated key file.
16
+ email:
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/main.rb
22
+ homepage:
23
+ licenses:
24
+ - MIT
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.23
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Multiple alphabet encryption gem.
47
+ test_files: []