Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could use OpenSSL::Cypher</p> <pre><code># for more info, see http://ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL/Cipher.html require 'openssl' require 'digest/sha1' # create the cipher for encrypting cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") cipher.encrypt # you will need to store these for later, in order to decrypt your data key = Digest::SHA1.hexdigest("yourpass") iv = cipher.random_iv # load them into the cipher cipher.key = key cipher.iv = iv # encrypt the message encrypted = cipher.update('This is a secure message, meet at the clock-tower at dawn.') encrypted &lt;&lt; cipher.final puts "encrypted: #{encrypted}\n" # now we create a sipher for decrypting cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") cipher.decrypt cipher.key = key cipher.iv = iv # and decrypt it decrypted = cipher.update(encrypted) decrypted &lt;&lt; cipher.final puts "decrypted: #{decrypted}\n" </code></pre> <p>But the intermediate form doesn't lend itself well to printing</p> <hr> <p>Given your thought that it would be nice if the intermediate form was the same length, you might just use a simple map of one char to another.</p> <h1><strong>PLEASE UNDERSTAND THAT THIS IS NOT SECURE</strong></h1> <p>You can <em>easily</em> brute force the key, but it seems to be congruent with your requirements.</p> <pre><code>class Cipher def initialize(shuffled) normal = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + [' '] @map = normal.zip(shuffled).inject(:encrypt =&gt; {} , :decrypt =&gt; {}) do |hash,(a,b)| hash[:encrypt][a] = b hash[:decrypt][b] = a hash end end def encrypt(str) str.split(//).map { |char| @map[:encrypt][char] }.join end def decrypt(str) str.split(//).map { |char| @map[:decrypt][char] }.join end end # pass the shuffled version to the cipher cipher = Cipher.new ["K", "D", "w", "X", "H", "3", "e", "1", "S", "B", "g", "a", "y", "v", "I", "6", "u", "W", "C", "0", "9", "b", "z", "T", "A", "q", "U", "4", "O", "o", "E", "N", "r", "n", "m", "d", "k", "x", "P", "t", "R", "s", "J", "L", "f", "h", "Z", "j", "Y", "5", "7", "l", "p", "c", "2", "8", "M", "V", "G", "i", " ", "Q", "F"] msg = "howdy pardner" crypted = cipher.encrypt msg crypted # =&gt; "1IzXAF6KWXvHW" decrypted = cipher.decrypt crypted decrypted # =&gt; "howdy pardner" </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload