-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.rb
More file actions
31 lines (27 loc) · 892 Bytes
/
encryption.rb
File metadata and controls
31 lines (27 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def shift_char char, amt, alphabet, strict = false
char.downcase! unless strict
index = alphabet.find_index char
return char if index.nil?
alphabet[(index + amt) % alphabet.count]
end
def encrypt_string string, key, alphabet, strict = false, decrypt = false
encrypted = ""
string.chars.each_with_index do |char, i|
amt = alphabet.find_index(key.chars[i % key.length])
encrypted += shift_char char, decrypt ? -amt : amt, alphabet, strict
end
encrypted
end
alias encrypt encrypt_string
def decrypt_string string, key, alphabet, strict = false
encrypt_string string, key, alphabet, strict, true
end
alias decrypt decrypt_string
class String
def encrypt key, alphabet, strict = false, decrypt = false
encrypt_string self, key, alphabet, strict, decrypt
end
def decrypt key, alphabet, strict = false
decrypt_string self, key, alphabet, strict
end
end