언어/Ruby
루비구현(시저암호)
파아랑새
2016. 1. 19. 11:13
#루비로 구현한 시저암호
Random.new_seed
# Generate a number from 1 to 25
# Including 25
def KeyRandomGenerator()
key = (rand()*10).to_i
return key
end
# field setting
alphabat = Array.new# => []
index = 97..122
for i in index
alphabat<<i.chr
end
len = 0...alphabat.length
print alphabat
puts
print "plaintext is "
plaintext = gets()#평문입력
secretKey = KeyRandomGenerator()
range = 0...plaintext.length
#puts plaintext[0]
puts secretKey
#puts str.include?("kim")
cipherText=Array.new# => []
for i in range
#printf는 자동 줄바꿈을 하지 않는다.
#puts는 자동 줄바꿈한다.
if(alphabat.include?(plaintext[i]) == true)
for j in len
if(plaintext[i] == alphabat[j])
num_index = (j+secretKey)%alphabat.length
end
end
cipherText << alphabat[num_index]
else
cipherText << plaintext[i]
end
end
print(cipherText)