Created
September 10, 2019 05:16
-
-
Save Ethan-Rivas/60b8984120796d50386dda5a1f33b1e5 to your computer and use it in GitHub Desktop.
Multiplication Encryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mathEncryption(alphabet, key, message) | |
# Format the alphabet (split by element and slice) | |
alphabet = alphabet.split("") | |
alphabetDivided = alphabet.each_slice(alphabet.size / 2).to_a | |
encryptedMessage = "" | |
array = [] | |
# Build they encryption table (is not sorted yet) | |
alphabet.size.times do | |
alphabetDivided = alphabet.each_slice(alphabet.size / 2).to_a | |
array.push(alphabetDivided) | |
end | |
# Create encryption table mirror effect for elements idx bigger than alphabet size / 2 | |
array.drop(alphabet.size / 2).each do |array| | |
left = array[0].dup | |
right = array[1].dup | |
array[0] = right | |
array[1] = left | |
end | |
# Sort encryption table (puts for debugging) | |
alphabet.size.times do |i| | |
if i === 0 | |
#puts "#{i}. #{array[i]}" | |
end | |
if i != 0 && i < array.size / 2 | |
pivotLeft = array[i-1][0].dup | |
char = pivotLeft.shift() | |
pivotLeft << char | |
array[i][0] = pivotLeft | |
pivotRight = array[i-1][1].dup | |
char = pivotRight.shift() | |
pivotRight << char | |
array[i][1] = pivotRight | |
#puts "#{i}. #{array[i]}" | |
end | |
if i != 0 && i > array.size / 2 | |
pivotLeft = array[i-1][0].dup | |
pivotLeft = pivotLeft.insert(0,pivotLeft.delete_at((alphabet.size / 2) - 1)) | |
array[i][0] = pivotLeft | |
pivotRight = array[i-1][1].dup | |
pivotRight = pivotRight.insert(0,pivotRight.delete_at((alphabet.size / 2) - 1)) | |
array[i][1] = pivotRight | |
#puts "#{i}. #{array[i]}" | |
end | |
if i == array.size / 2 | |
pivotLeft = array[i][0].dup | |
pivotLeft = pivotLeft.reverse.insert(0,pivotLeft.reverse.delete_at((alphabet.size / 2) - 1)) | |
pivotLeft.pop() | |
array[i][0] = pivotLeft | |
pivotRight = array[i][1].dup | |
pivotRight = pivotRight.reverse.insert(0,pivotRight.reverse.delete_at((alphabet.size / 2) - 1)) | |
pivotRight.pop() | |
array[i][1] = pivotRight | |
#puts "#{i}. #{array[i]}" | |
end | |
end | |
#puts "#{array}" | |
# Search and store message letters position [idx] [position] | |
messageIndex = [] | |
msg = message.split("") | |
msg.size.times do |char| | |
array[0].size.times do |i| | |
array[0][i].size.times do |z| | |
if array[0][i][z].downcase == msg[char].downcase | |
messageIndex.push([i,z]) | |
end | |
end | |
end | |
end | |
#puts "#{messageIndex}" | |
# Search and store key letters position [idx] [position] | |
keyIndex = [] | |
key = key.split("") | |
key.size.times do |char| | |
array.size.times do |i| | |
if array[i][0][0].downcase == key[char].downcase | |
keyIndex.push(i) | |
end | |
end | |
end | |
#puts "#{keyIndex}" | |
# Search encryption letter based on array[keyIdx][idx][position] | |
messageIndex.size.times do |i| | |
encryptedMessage += array[keyIndex[i]][messageIndex[i][0]][messageIndex[i][1]] | |
end | |
# Return encrypted message | |
encryptedMessage | |
end | |
mathEncryption("abcdef", "bebecafebebecafebebecafebebecafe", "adadebebecaadf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment