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 == 0
- return nil
- elsif possibles.size == 1
- pt[i] = possibles[0].chr
- else
- pt[i] = "(" + possibles.map(&:chr).join("|") + ")"
- end
- key += 1
- end
- pt.join
- end
- ciphertext = %W[102 141 142 161 148 131 158 157 120 131 126 153 130 143 154 135 118 137 148 111 116 145 134 133 118 103 136 107 138 127 102 131 194 99 114 200 202 127 120 105 126 202 108 194 198 214 222 204 116 224 194 198 202 230 166 222 216 234 232 210 222 220 210 230 194 230 198 210 210 230 234 218 222 204 194 216 216 224 216 194 210 220 232 202 240 232 198 208 194 228 230].map(&:to_i).map(&:chr).join
- (0..(ciphertext.size / 2)).each do |key|
- replaintext = decrypt(ciphertext, key)
- p key
- p replaintext
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement