@tech4teachers/

CaesarCipher in Ruby1.0

Ruby

No description

fork
loading
Files
  • main.rb
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ALPHABET_SIZE = 26

#def caesar_cipher(string)
  #shiftyArray = []
 # charLine = string.chars.map(&:ord)

#  shift = 1
 # ALPHABET_SIZE.times do |shift|
  #  shiftyArray << charLine.map do |c|
   #   ((c + shift) < 123 ? (c + shift) : (c + shift) - 26).chr
    #end.join
#  end

 # shiftyArray
#end

#puts caesar_cipher("The week is long. The silver cat feeds when blue meets yellow in the west. A trip to China sounds nice if you tread lightly.")




#def caesar_cipher(string, shift = 1)
  #alphabet   = Array('a'..'z')
  #encrypter  = Hash[alphabet.zip(alphabet.rotate(shift))]
  #string.chars.map { |c| encrypter.fetch(c, " ") }
#end

#p caesar_cipher("testing").join





#def caesar_cipher(string, shift = 1)
  #alphabet   = Array('a'..'z')
  #encrypter  = Hash[alphabet.zip(alphabet.rotate(shift))]
  #string.chars.map { |c| encrypter.fetch(c, " ") }
#end

#p caesar_cipher("testing").join




class Caesar
  def initialize(shift, alphabet = ('a'..'z').to_a.join)
    i = shift % alphabet.size #I like this
    @decrypt = alphabet
    @encrypt = alphabet[i..-1] + alphabet[0...i]
  end

  def encrypt(string)
    string.tr(@decrypt, @encrypt)
  end

  def decrypt(string)
    string.tr(@encrypt, @decrypt)
  end
end


#encrypts and decrypts but only asks for encryption
puts "Text for encrypt please: "
text_for_encrypt = gets.chomp

puts "E for Encrypt or D for Decrypt"
encr_or_decr = gets.chomp
# attempt at error checking ...does not work
#if encr_or_decr not ="E" or if encr_or_decr not ="D"
 # puts "E for Encrypt or D for Decrypt"
  #encr_or_decr = gets.chomp
  #else 
   # puts "Input encrypt key: "
    #key = gets.chomp.to_i
    #to_i is for INTEGER NOT the variable named i later on
  #end

puts "Input encrypt key: "
key = gets.chomp.to_i  
  

text_arrow = text_for_encrypt.split('').to_a
alphabet_az = ("a".."z").to_a.join
alphabet_AZ = ("A".."Z").to_a.join
i = key % alphabet_az.size


encrypt_az = alphabet_az.chars.rotate(i).join
encrypt_AZ = alphabet_AZ.chars.rotate(i).join

decrypt_az = alphabet_az.chars.rotate(-i).join
decrypt_AZ = alphabet_AZ.chars.rotate(-i).join

#store the reulting message reassembled from the split array
res = []

#encryption
if encr_or_decr=="E"
  text_arrow.each do |letter|
    if ("a".."z") === letter
      letter = letter.tr( alphabet_az, encrypt_az )
      res << letter
    else
      letter = letter.tr( alphabet_AZ, encrypt_AZ )
      res << letter
    end
  end
end

#decryption
if encr_or_decr=="D"
  text_arrow.each do |letter|
    if ("a".."z") === letter
      letter = letter.tr( alphabet_az, decrypt_az )
      res << letter
    else
      letter = letter.tr( alphabet_AZ, decrypt_AZ )
      res << letter
    end
  end
end

puts "Your code: #{res.join}"
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]