cea(ver3)

언어/Ruby2016. 2. 27. 12:17
# =================== Echo Function ==========================
class Echo
def Function__Echo__PlainText(plainText)
print("Plaintext is [ #{plainText} ] \n")
end

def Function__Echo__RandomKey(key)
print("RandomKey is [ #{key} ] \n")
end

def Function__Echo__Field(flist)
print("Field list is #{flist} \n")
end

def Function__Echo__CipherText(cipherText)
print("Ciphertext is #{cipherText} \n")
end

def Function__Echo__Field_LEN(fieldlen)
print("Field Length is #{fieldlen} \n")
end

def Function__Echo__DecryptText(decrypt)
print("DecryptText is #{decrypt} \n")
end
end
# =================== Main Function ==========================
class CEA < Echo
attr_accessor :STRING__plaintext
attr_accessor :STRING__ciphertext
attr_accessor :INT__random_cea_key
attr_accessor :STRING__decrypt_plaintext
attr_accessor :INT__plaintext_LEN
attr_accessor :FieldList
attr_accessor :INT__FieldList_LEN
def initialize
@STRING__plaintext = ""
@STRING__ciphertext = ""
@INT__random_cea_key = 0
@STRING__decrypt_plaintext = ""
@FieldList = []
@INT__FieldList_LEN = 0
end
#[Func 1]--------------------------------------------------------
def Function__FieldList_SETTING
(97..122).each do |encoding| #a, b, c, d, ..., z
@FieldList.push(encoding.chr)
end
(65..90).each do |encoding| #A, B, C, D, ..., Z
@FieldList.push(encoding.chr)
end
(''..'').each do |hangul|
@FieldList.push(hangul)
end
@FieldList.push(" ")
@FieldList = @FieldList.shuffle
@INT__FieldList_LEN = @FieldList.length
end
#[Func 2]--------------------------------------------------------
def Function__Random_Secret_Key
@INT__random_cea_key = Random.new
@INT__random_cea_key = rand(1...@INT__FieldList_LEN) # 1, 2, 3, 4, ...,25
end
#[Func 3]--------------------------------------------------------
def Function__Encrypt
@STRING__plaintext.each_char do |plainT_CHAR|
if (@FieldList.include?(plainT_CHAR))
position = @FieldList.index(plainT_CHAR)
position = (position + @INT__random_cea_key)%@INT__FieldList_LEN
@STRING__ciphertext = @STRING__ciphertext + @FieldList[position]
else
@STRING__ciphertext = @STRING__ciphertext + plainT_CHAR
end
end
end
#[Func 3]--------------------------------------------------------
def Function__Decrypt
@STRING__ciphertext.each_char do |ciphterT_CHAR|
if (@FieldList.include?(ciphterT_CHAR))
position = @FieldList.index(ciphterT_CHAR)
position = (position - @INT__random_cea_key)%@INT__FieldList_LEN
@STRING__decrypt_plaintext = @STRING__decrypt_plaintext + @FieldList[position]
else
@STRING__decrypt_plaintext = @STRING__decrypt_plaintext + ciphterT_CHAR
end
end
end
end

#===================== << MAIN >> ===========================
def main
cea = CEA.new
cea.Function__FieldList_SETTING
cea.Function__Echo__Field(cea.FieldList)
cea.Function__Echo__Field_LEN(cea.INT__FieldList_LEN)
cea.STRING__plaintext = gets.chomp.to_s
cea.Function__Random_Secret_Key
cea.Function__Echo__PlainText(cea.STRING__plaintext)
cea.Function__Echo__RandomKey(cea.INT__random_cea_key)
cea.Function__Encrypt
cea.Function__Echo__CipherText(cea.STRING__ciphertext)
cea.Function__Decrypt
cea.Function__Echo__DecryptText(cea.STRING__decrypt_plaintext)
end
main


'언어 > Ruby' 카테고리의 다른 글

ruby + elasticsearch indices/ health  (0) 2020.07.02
ruby elasticsearch client  (0) 2020.05.26
class  (0) 2016.02.27
루비(ruby)상속 -> 상속 -> 상속  (0) 2016.02.26
시저 (ver2) 확실히 루비는 파이썬과 형제언어다  (0) 2016.02.20