Merge pull request #7 from benphelps/patch-1

Add ruby example
This commit is contained in:
Kyle Banks 2017-02-06 09:23:21 -05:00 committed by GitHub
commit a748f049f8
2 changed files with 16 additions and 0 deletions

View File

@ -14,6 +14,7 @@ Simple implementation of XOR Encryption/Decrypting in various languages, includi
- [JavaScript \(Node.js Compatible\)](JavaScript/XOREncryption.js)
- [Objective-C](Objective-C/main.m)
- [Python](Python/XOREncryption.py)
- [Ruby](Ruby/xor.rb)
- [Visual Basic.NET](VB.NET/XORCrypto.vb)
This implementation goes beyond the basic single-key model to use multiple keys in a particular sequence, making it that much more difficult to brute-force.

15
Ruby/xor.rb Normal file
View File

@ -0,0 +1,15 @@
def encryptDecrypt(string)
key = ['K', 'C', 'Q']
result = ""
codepoints = string.each_codepoint.to_a
codepoints.each_index do |i|
result += (codepoints[i] ^ key[i % key.size].ord).chr
end
result
end
encrypted = encryptDecrypt("kylewbanks.com")
puts "Encrypted: #{encrypted}"
decrypted = encryptDecrypt(encrypted)
puts "Decrypted: #{decrypted}"