A detailed example of python transposition cipher algorithm

  • 2020-06-12 09:56:22
  • OfStack

A detailed example of python transposition cipher algorithm

1 introduction:

Basic principle of transposition cipher: the plaintext is first grouped according to a fixed length, and then the characters of each group are transposed to achieve encryption. For example, the string "Error should pass silently" is encrypted using the secret key 1432. First, the string is divided into several groups of length 4. Then, the characters in each group are transposed.

2 codes:


def encrypt(plainText,t): 
 result =[] 
 length = len(t) 
 temp =[plainText[i:i+length]for i in range(0,len(plainText),length)] 
for item in temp[:-1]: 
 newItem='' 
for i in t: 
 newItem = newItem + item[i-1] 
 result.append(newItem) 
return''.join(result)+ temp[-1] 
p ="Error should never pass silently" 
c = encrypt(p,(1,4,3,2)) 
print(c) 
print(encrypt(c,(1,4,3,2))) 
 

3 Operating results


Eorrrhs odlu venep ra ssselintly
Error should never pass silently

Above is the algorithm of python transposition password detailed explanation, you can leave a message or to the community discussion site, thank you for reading, I hope to help you, thank you for your support to the site!


Related articles: