Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "pp"
- def encrypt(plaintext, key)
- pt = plaintext
- key += pt.length / 2
- ct = Array.new(pt.size)
- (0..pt.size-1).each do |i|
- ct[i] = pt[i].ord + ( pt[i].ord % key )
- key += 1
- end
- ct.map(&:chr).join
- end
- def decrypt(ciphertext, key)
- ct = ciphertext
- key += ct.length / 2
- pt = Array.new(ct.size)
- (0..pt.size-1).each do |i|
- possibles = []
- (0..127).each do |possible_pt|
- if possible_pt + (possible_pt % key) == ct[i].ord
- possibles << possible_pt
- end
- end
- if possibles.size == 1
- pt[i] = possibles[0].chr
- else
- pt[i] = "(" + possibles.map(&:chr).join("|") + ")"
- end
- key += 1
- end
- pt.join
- end
- plaintext = "Hello, world!"
- key = rand(plaintext.size / 2)
- ciphertext = encrypt(plaintext, key)
- replaintext = decrypt(ciphertext, key)
- p key
- p plaintext
- p ciphertext
- p replaintext
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement