Advertisement
t_a_w

cipher enc/dec

Apr 5th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.36 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. def enc(plain, key)
  4.   cipher = ""
  5.   for i in 0..plain.length-2
  6.     c = plain[i]
  7.     n = plain[i+1]
  8.     cc = c.ord ^ n.ord ^ key
  9.     cipher << cc.to_s + " "
  10.   end
  11.   cipher
  12. end
  13.  
  14. def dec(cipher, a0, key)
  15.   nums = cipher.split.map(&:to_i)
  16.   last = a0
  17.   result = a0.chr + ""
  18.   nums.each do |n|
  19.     last = last^n^key
  20.     result << last.chr
  21.   end
  22.   result
  23. end
  24.  
  25. cipher = "75 117 99 113 101 97 38 37 119 126 97 46 32 97 107 100 55 121 4 72 110 102 39 58 117 121 54 39 98 101 105 110 98 98 39 55 120 44 41 102 101 100 45 46 107 38 39 98 117 100 126 100 116 52 109 20 81 38 45 104 99 98 39 55 120 44 48 100 120 124 51 48 113 96 115 116 114 120 99 120 127 43 34 108 105 39 55 127 110 38 17 116 6 2 69 97 104 110 121 52 109 10 10 52 125 102 99 47 33 100 38 55 114 117 100 126 100 106 36 58 117 121 100 49 32 114 104 106 103 120 44 48 123 97 102 99 47 37 112 100 101 127 110 116 49 33 100 96 106 126 116 38 34 110 99 96 123 125 100 106 36 58 117 121 54 55 120 44 48 117 99 38 46 119 58 47 96 111 97 118 126 101 98 35 109 22 69 117 38 100 55 127 96 96 97 97 98 60 96 98 113 101 100 34 113 48 34 34 51 114 113 99 103 123 126 117 39 34 108 105 39 0 73 122 123 110 116 49 27 27 52 125 102 99 47 47 106 114 55 58 117 121 54 51 97 126 107 118 116 117 99"
  26.  
  27.  
  28. encrypted = enc("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 123)
  29. decrypted = dec(encrypted, "A".ord, 123)
  30.  
  31. puts decrypted
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement