Added Lab2/Lab3 IS
This commit is contained in:
parent
011d5aded1
commit
0755dd4b76
6 changed files with 109 additions and 140 deletions
|
@ -5,76 +5,68 @@
|
|||
|
||||
def add_cipher_en(ptext, ak):
|
||||
result = ""
|
||||
for i in range(len(ptext)):
|
||||
ch = ptext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((ord(ch) + ak - 65) % 26 + 65)
|
||||
for ch in ptext:
|
||||
if ch.isalpha():
|
||||
base = ord('A') if ch.isupper() else ord('a')
|
||||
result += chr((ord(ch) - base + ak) % 26 + base)
|
||||
else:
|
||||
result += chr((ord(ch) + ak - 97) % 26 + 97)
|
||||
result += ch
|
||||
return result
|
||||
|
||||
def add_cipher_de(ctext, ak):
|
||||
result = ""
|
||||
for i in range(len(ctext)):
|
||||
ch = ctext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((ord(ch) - ak - 65) % 26 + 65)
|
||||
for ch in ctext:
|
||||
if ch.isalpha():
|
||||
base = ord('A') if ch.isupper() else ord('a')
|
||||
result += chr((ord(ch) - base - ak) % 26 + base)
|
||||
else:
|
||||
result += chr((ord(ch) - ak - 97) % 26 + 97)
|
||||
result += ch
|
||||
return result
|
||||
|
||||
def mult_cipher_en(ptext, mk):
|
||||
result = ""
|
||||
for i in range(len(ptext)):
|
||||
ch = ptext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((ord(ch) * mk - 65) % 26 + 65)
|
||||
for ch in ptext:
|
||||
if ch.isalpha():
|
||||
base = ord('A') if ch.isupper() else ord('a')
|
||||
x = ord(ch) - base
|
||||
result += chr((x * mk) % 26 + base)
|
||||
else:
|
||||
result += chr((ord(ch) * mk - 97) % 26 + 97)
|
||||
result += ch
|
||||
return result
|
||||
|
||||
def mult_cipher_de(ctext, mk):
|
||||
result = ""
|
||||
inverse = pow(mk, -1, 26)
|
||||
for i in range(len(ctext)):
|
||||
ch = ctext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((ord(ch) * inverse - 65) % 26 + 65)
|
||||
for ch in ctext:
|
||||
if ch.isalpha():
|
||||
base = ord('A') if ch.isupper() else ord('a')
|
||||
x = ord(ch) - base
|
||||
result += chr((x * inverse) % 26 + base)
|
||||
else:
|
||||
result += chr((ord(ch) * inverse - 97) % 26 + 97)
|
||||
result += ch
|
||||
return result
|
||||
|
||||
def affine_en(ptext, ak, mk):
|
||||
result = ""
|
||||
for i in range(len(ptext)):
|
||||
ch = ptext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((((ord(ch) - 65) * mk) + ak) % 26 + 65)
|
||||
for ch in ptext:
|
||||
if ch.isalpha():
|
||||
base = ord('A') if ch.isupper() else ord('a')
|
||||
x = ord(ch) - base
|
||||
result += chr(((x * mk + ak) % 26) + base)
|
||||
else:
|
||||
result += chr((((ord(ch) - 97) * mk) + ak) % 26 + 97)
|
||||
result += ch
|
||||
return result
|
||||
|
||||
def affine_de(ctext, ak, mk):
|
||||
result = ""
|
||||
inverse = pow(mk, -1, 26)
|
||||
for i in range(len(ctext)):
|
||||
ch = ctext[i]
|
||||
if 32 <= ord(ch) <= 47:
|
||||
result += ch
|
||||
elif 90 >= ord(ch) >= 65:
|
||||
result += chr((((ord(ch) - 65 - ak) * inverse) % 26) + 65)
|
||||
for ch in ctext:
|
||||
if ch.isalpha():
|
||||
base = ord('A') if ch.isupper() else ord('a')
|
||||
x = ord(ch) - base
|
||||
result += chr((((x - ak) * inverse) % 26) + base)
|
||||
else:
|
||||
result += chr((((ord(ch) - 97 - ak) * inverse) % 26) + 97)
|
||||
result += ch
|
||||
return result
|
||||
|
||||
def mult_inverse(mk):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue