我正在尝试使用ruby制作一个简单的加密算法。
当我使用.to_i转换为int时,ruby打印“没有从C:/Users/liam/Downloads/encryption.rb:60:in'中隐藏将字符串转换为Integer(TypeError)。
第60行如下:
cleartext.to_i
其余代码是:
print ">"
cleartext = gets.chomp
print ">"
symetricKey = gets.chomp
puts "cleartext and symetricKey obtained. calculating result..."
STDOUT.flush
symetricKey.gsub! 'a', '1'
symetricKey.gsub! 'b', '2'
symetricKey.gsub! 'c', '3'
symetricKey.gsub! 'd', '4'
symetricKey.gsub! 'e', '5'
symetricKey.gsub! 'f', '6'
symetricKey.gsub! 'g', '7'
symetricKey.gsub! 'h', '8'
symetricKey.gsub! 'i', '9'
symetricKey.gsub! 'j', '10'
symetricKey.gsub! 'k', '11'
symetricKey.gsub! 'l', '12'
symetricKey.gsub! 'm', '13'
symetricKey.gsub! 'n', '14'
symetricKey.gsub! 'o', '15'
symetricKey.gsub! 'p', '16'
symetricKey.gsub! 'q', '17'
symetricKey.gsub! 'r', '18'
symetricKey.gsub! 's', '19'
symetricKey.gsub! 't', '20'
symetricKey.gsub! 'u', '21'
symetricKey.gsub! 'v', '22'
symetricKey.gsub! 'w', '23'
symetricKey.gsub! 'x', '24'
symetricKey.gsub! 'y', '25'
symetricKey.gsub! 'z', '26'
symetricKey.to_i
cleartext.gsub! 'a', '1'
cleartext.gsub! 'b', '2'
cleartext.gsub! 'c', '3'
cleartext.gsub! 'd', '4'
cleartext.gsub! 'e', '5'
cleartext.gsub! 'f', '6'
cleartext.gsub! 'g', '7'
cleartext.gsub! 'h', '8'
cleartext.gsub! 'i', '9'
cleartext.gsub! 'j', '10'
cleartext.gsub! 'k', '11'
cleartext.gsub! 'l', '12'
cleartext.gsub! 'm', '13'
cleartext.gsub! 'n', '14'
cleartext.gsub! 'o', '15'
cleartext.gsub! 'p', '16'
cleartext.gsub! 'q', '17'
cleartext.gsub! 'r', '18'
cleartext.gsub! 's', '19'
cleartext.gsub! 't', '20'
cleartext.gsub! 'u', '21'
cleartext.gsub! 'v', '22'
cleartext.gsub! 'w', '23'
cleartext.gsub! 'x', '24'
cleartext.gsub! 'y', '25'
cleartext.gsub! 'z', '26'
cleartext.to_i
ciphertext = cleartext * (symetricKey * symetricKey * 100)
ciphertext.to_s
ciphertext.gsub! 'z', '26'
ciphertext.gsub! 'y', '25'
ciphertext.gsub! 'x', '24'
ciphertext.gsub! 'w', '23'
ciphertext.gsub! 'v', '22'
ciphertext.gsub! 'u', '21'
ciphertext.gsub! 't', '20'
ciphertext.gsub! 's', '19'
ciphertext.gsub! 'r', '18'
ciphertext.gsub! 'q', '17'
ciphertext.gsub! 'p', '16'
ciphertext.gsub! 'o', '15'
ciphertext.gsub! 'n', '14'
ciphertext.gsub! 'm', '13'
ciphertext.gsub! 'l', '12'
ciphertext.gsub! 'k', '11'
ciphertext.gsub! 'j', '10'
ciphertext.gsub! 'i', '9'
ciphertext.gsub! 'h', '8'
ciphertext.gsub! 'g', '7'
ciphertext.gsub! 'f', '6'
ciphertext.gsub! 'e', '5'
ciphertext.gsub! 'd', '4'
ciphertext.gsub! 'c', '3'
ciphertext.gsub! 'b', '2'
ciphertext.gsub! 'a', '1'
puts (ciphertext)
答案 0 :(得分:2)
您尚未分配结果
to_i
或to_s
会返回您需要分配回变量的结果,如下所示。
以这种方式分配
cleartext=cleartext.to_i
ciphertext=ciphertext.to_s
symetricKey=symetricKey.to_i
试试这个
print ">"
cleartext = gets.chomp
print ">"
symetricKey = gets.chomp
puts "cleartext and symetricKey obtained. calculating result..."
STDOUT.flush
symetricKey.gsub! 'a', '1'
symetricKey.gsub! 'b', '2'
symetricKey.gsub! 'c', '3'
symetricKey.gsub! 'd', '4'
symetricKey.gsub! 'e', '5'
symetricKey.gsub! 'f', '6'
symetricKey.gsub! 'g', '7'
symetricKey.gsub! 'h', '8'
symetricKey.gsub! 'i', '9'
symetricKey.gsub! 'j', '10'
symetricKey.gsub! 'k', '11'
symetricKey.gsub! 'l', '12'
symetricKey.gsub! 'm', '13'
symetricKey.gsub! 'n', '14'
symetricKey.gsub! 'o', '15'
symetricKey.gsub! 'p', '16'
symetricKey.gsub! 'q', '17'
symetricKey.gsub! 'r', '18'
symetricKey.gsub! 's', '19'
symetricKey.gsub! 't', '20'
symetricKey.gsub! 'u', '21'
symetricKey.gsub! 'v', '22'
symetricKey.gsub! 'w', '23'
symetricKey.gsub! 'x', '24'
symetricKey.gsub! 'y', '25'
symetricKey.gsub! 'z', '26'
symetricKey=symetricKey.to_i
cleartext.gsub! 'a', '1'
cleartext.gsub! 'b', '2'
cleartext.gsub! 'c', '3'
cleartext.gsub! 'd', '4'
cleartext.gsub! 'e', '5'
cleartext.gsub! 'f', '6'
cleartext.gsub! 'g', '7'
cleartext.gsub! 'h', '8'
cleartext.gsub! 'i', '9'
cleartext.gsub! 'j', '10'
cleartext.gsub! 'k', '11'
cleartext.gsub! 'l', '12'
cleartext.gsub! 'm', '13'
cleartext.gsub! 'n', '14'
cleartext.gsub! 'o', '15'
cleartext.gsub! 'p', '16'
cleartext.gsub! 'q', '17'
cleartext.gsub! 'r', '18'
cleartext.gsub! 's', '19'
cleartext.gsub! 't', '20'
cleartext.gsub! 'u', '21'
cleartext.gsub! 'v', '22'
cleartext.gsub! 'w', '23'
cleartext.gsub! 'x', '24'
cleartext.gsub! 'y', '25'
cleartext.gsub! 'z', '26'
cleartext=cleartext.to_i
ciphertext = cleartext * (symetricKey * symetricKey * 100)
ciphertext=ciphertext.to_s
ciphertext.gsub! 'z', '26'
ciphertext.gsub! 'y', '25'
ciphertext.gsub! 'x', '24'
ciphertext.gsub! 'w', '23'
ciphertext.gsub! 'v', '22'
ciphertext.gsub! 'u', '21'
ciphertext.gsub! 't', '20'
ciphertext.gsub! 's', '19'
ciphertext.gsub! 'r', '18'
ciphertext.gsub! 'q', '17'
ciphertext.gsub! 'p', '16'
ciphertext.gsub! 'o', '15'
ciphertext.gsub! 'n', '14'
ciphertext.gsub! 'm', '13'
ciphertext.gsub! 'l', '12'
ciphertext.gsub! 'k', '11'
ciphertext.gsub! 'j', '10'
ciphertext.gsub! 'i', '9'
ciphertext.gsub! 'h', '8'
ciphertext.gsub! 'g', '7'
ciphertext.gsub! 'f', '6'
ciphertext.gsub! 'e', '5'
ciphertext.gsub! 'd', '4'
ciphertext.gsub! 'c', '3'
ciphertext.gsub! 'b', '2'
ciphertext.gsub! 'a', '1'
puts (ciphertext)
更新: 正如@engineersmnky提到的那样
symetricKey = symetricKey.gsub(/[a-z]/) { |letter| letter.ord - 96 }.to_i