Substitution cipher: Difference between revisions

→‎{{header|Scala}}: Add Ruby; with Reve quote like Scala
(→‎{{header|Scala}}: Add Ruby; with Reve quote like Scala)
Line 1,934:
Encoded : SIAA ZQ LKBA. VA ZOA RFPBLUAOAR!
Decoded : flee at once. we are discovered!
</pre>
 
=={{header|Ruby}}==
<lang ruby>Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Key = "VsciBjedgrzyHalvXZKtUPumGfIwJxqOCFRApnDhQWobLkESYMTN"
 
def encrypt(str) = str.tr(Alphabet, Key)
def decrypt(str) = str.tr(Key, Alphabet)
str = 'All is lost, he thought. Everything is ruined. It’s ten past three.'
p encrypted = encrypt(str)
p decrypt(encrypted)
</lang>
{{out}}
<pre>"Vnn RL nQLk, Fq kFQECFk. BSqbTkFRhC RL bERhqx. gk’L kqh WILk kFbqq."
"All is lost, he thought. Everything is ruined. It’s ten past three."
</pre>
 
1,149

edits