fork download
  1. message = 'Hello, world!'
  2. key = 4
  3.  
  4. def encrypt(message, key)
  5. return message.split('').map{ |c| c = (c.ord + key).chr }.join
  6. end
  7.  
  8. def decrypt(cipher, key)
  9. return cipher.split('').map{ |c| c = (c.ord - key).chr }.join
  10. end
  11.  
  12. cipher_text = encrypt(message, key)
  13. plain_text = decrypt(cipher_text, key)
  14.  
  15. puts "#{message} encrypted with key value of #{key}:"
  16. puts cipher_text
  17.  
  18. puts "#{cipher_text} deciphered with key value of #{key}:"
  19. puts plain_text
Success #stdin #stdout 0.01s 7412KB
stdin
Standard input is empty
stdout
Hello, world! encrypted with key value of 4:
Lipps0${svph%
Lipps0${svph% deciphered with key value of 4:
Hello, world!