#!/usr/bin/env ruby ALPHABET = ('a'..'z').to_a ALPHABET << ' ' puts ALPHABET.to_s def mklettersubstitioncipher cipher = {} ALPHABET.each { |i| while true c = ALPHABET[ Random.rand(ALPHABET.size) ] if not cipher.values.include? c cipher[i] = c break end end } return cipher end def encrypt(key, plaintext) s = '' plaintext.each_char { |i| s += key[i] } return s end def decrypt(key, ciphertext) s = '' ciphertext.each_char { |i| key.each_pair { |p| if p[1] == i s += p[0] end } } return s end def randomcipherstring(cipher) length = Random.rand(49)+1 s = '' length.times { s += cipher[ ALPHABET[Random.rand(ALPHABET.size)] ] } return s end def sample(n, cipher) a = [] n.times { a << randomcipherstring(cipher) } puts a.to_s end def test cipher = mklettersubstitioncipher s = "das ist das haus vom nikolaus" s_enc = encrypt(cipher, s) s_dec = decrypt(cipher, s_enc) puts "#{s}\n#{s_enc}\n#{s_dec}" end test