Tutorial02 of security(using sageMath)
In this exercise students exercise mono-alphabetic and Playfair cipher encryption and decryption using SageMath tool.
First:Mono-alphabetic cipher
Monoalphabetic cipher Security(some artributes)
- with so many keys, might think is secure
- But would be wrong
Encrypt "Ifwewishtoreplaceletters"
using mono-alphabetic cipher with the key k="DKVQFIBJWPESCXHTMYAUOLRGZN"
Decrypt back the ciphertext
Encrypt "Welcometomysecuritycourse"
using mono-alphabetic cipher with the key k="DKVQFIBJWPESCXHTMYAUOLRGZN"
Decrypt back the ciphertext
alpha="abcdefghijklmnopqrstuvwxyz"
def is_alpha_char(c):
return(c.lower()in alpha)
def get_alpha_index(c):
return alpha.index(c.lower())
def get_key_index(c,key):
return key.index(c.upper())
def encrypt_ma(k,plaintext):
ciphertext=""
if len(k)==26:
key=k
for j in range(len(plaintext)):
p=plaintext[j]
if is_alpha_char(p):
x=get_alpha_index(p)
c=key[x]
else:
c=p
ciphertext +=c
return ciphertext
else:
print("wrong key size")
def decrypt_ma(k,ciphertext):
plaintext=""
if len(k)==26:
key=k
for j in range(len(ciphertext)):
c=ciphertext[j]
if is_alpha_char(c):
x=get_key_index(c,key)
p=alpha[x]
else:
p=c
plaintext +=p
return plaintext
else:
print("wrong key size")
ptext="Ifwewishtoreplaceletters"
k="DKVQFIBJWPESCXHTMYAUOLRGZN"
print(encrypt_ma(k,ptext))
ctext=encrypt_ma(k,ptext)
print(decrypt_ma(k,ctext))
the result:
WIRFRWAJUHYFTSDVFSFUUFYA
ifwewishtoreplaceletters
PlayFair cipher algorithm
if pair in same row, replace with letter to right(OA->NR)
pair in same column, replace with one beneath(DT->KZ)
replace row letter with letter in row of other letter’s column(组成矩形的另外俩)
Security of playfair Cipher
- security much improved over monoalphabetic
- 26x26=676 digrams
- correspondingly more ciphertext
- it can be broken.still has much of plaintext structure
原文地址:https://blog.csdn.net/yanlingyun0210/article/details/142300591
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!