- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
def simpleEncrDecr( alphabet, alphabetKey, encrDecrWord, triger) #простой метод одну букву из алфавита заменяет другой
alphabet = alphabet.chars.to_a
alphabetKey = alphabetKey.chars.to_a
encrDecrWord = encrDecrWord.chars.to_a
if triger == 'encryption' then
for i in 0..encrDecrWord.length-1
encrDecrWord[i] = alphabetKey[alphabet.rindex(encrDecrWord[i])]
end
elsif triger == 'decryption'
for i in 0..encrDecrWord.length-1
encrDecrWord[i] = alphabet[alphabetKey.rindex(encrDecrWord[i])]
end
else
p "You is looser! :-)"
end
encrDecrWord.join
end
def manyAlphEncrDecr( alphabet, alpKey, encrDecrWord, triger) #для закодування нужное слово которое будет ключом
alphabet = alphabet.chars.to_a
encrDecrWord = encrDecrWord.chars.to_a
if triger == 'encryption' then
for i in 0..encrDecrWord.length-1
sum = alphabet.rindex(encrDecrWord[i])+alphabet.rindex(alpKey[i])
encrDecrWord[i] = alphabet[sum%alphabet.length]
end
elsif triger == 'decryption'
for i in 0..encrDecrWord.length-1
div = alphabet.rindex(encrDecrWord[i])-alphabet.rindex(alpKey[i])
encrDecrWord[i] = alphabet[div]
end
else
p "You is looser! :-)"
end
encrDecrWord.join
end
def permutationEncrDecr( permKeyNew, encrDecrWord, triger) #меняет местами буквы в соответствии с ключом, 1234 <=> 3241
encrDecrWord = encrDecrWord.chars.to_a
encrDecrWordNew = Array.new
if triger == 'encryption' then
for i in 0..encrDecrWord.length-1
encrDecrWordNew[i] = encrDecrWord[permKeyNew[i]]
end
elsif triger == 'decryption'
for i in 0..encrDecrWord.length-1
encrDecrWordNew[permKeyNew[i]] = encrDecrWord[i]
end
else
p "You is looser! :-)"
end
encrDecrWordNew.join
end
def frequencyAnalysis(textForAnalis) #частотный анализ текста (подсчет букв)
analisHash = Hash.new
for i in 0..textForAnalis.length-1
analisHash[textForAnalis[i]] = 0
end
for i in 0..textForAnalis.length-1
analisHash[textForAnalis[i]] += 1
end
puts "Frequency Analysis - #{analisHash}"
end
p "Alphabet - #{alphabet = ('0'..'z').to_a.join}"
print "Enter words for encryption/decryption:"
wordForEncryped = gets.chomp
print "Type method:\n1-simple\n2-many alphabetic\n3-permutation\n"
method = gets.chomp
case
when method == '1' then
puts "Key for alphabet - #{alphabetKey = alphabet.chars.to_a.shuffle.join}"
puts "Simple Encryped - #{encrypedWord = simpleEncrDecr( alphabet, alphabetKey, wordForEncryped, 'encryption')}"
frequencyAnalysis(encrypedWord)
puts "Simple Decryped - #{simpleEncrDecr( alphabet, alphabetKey, encrypedWord, 'decryption')}"
when method == '2' then
print "Enter word for many alphabetical encryption/decryption:"
wordForManyAlphKey = gets.chomp.chars.to_a
manyAlphKey = Array.new
for i in 0..wordForEncryped.length-1
manyAlphKey[i] = wordForManyAlphKey[i%wordForManyAlphKey.length]
end
puts "Many Alphabetic Encryped - #{encrWordForManyAlph = manyAlphEncrDecr( alphabet, manyAlphKey, wordForEncryped, 'encryption')}"
frequencyAnalysis(encrWordForManyAlph)
puts "Many Alphabetic Decryped - #{manyAlphEncrDecr( alphabet, manyAlphKey, encrWordForManyAlph, 'decryption')}"
when method == '3' then
print 'Please typed key(f.e. 23451):'
permKey = gets.chomp.chars.to_a
lengthForSum = permKey.length
newPermKey = Array.new
for i in 0..wordForEncryped.length-1
lengthForSum += permKey.length if i==lengthForSum
newPermKey[i] = permKey[i%permKey.length].to_i + lengthForSum - permKey.length-1
end
puts "Permutation Encryped - #{encrypedWordForPrem = permutationEncrDecr( newPermKey, wordForEncryped, 'encryption')}"
frequencyAnalysis(encrypedWordForPrem)
puts "Permutation Decryped - #{permutationEncrDecr( newPermKey, encrypedWordForPrem, 'decryption')}"
else p 'Incorect typed'
end