Detailed Explanation of Information Encryption in python

  • 2021-07-01 07:59:41
  • OfStack

1. Attach the topic

Topic from PythonTip

Information encryption

Give you a lowercase English string a and a non-negative number b (0 < =b < 26), replace each lowercase character in a with a larger b letter in the alphabet. Here, connect z and a of the alphabet, and if it exceeds z, it will return to a.

For example, a= "cagy", b=3,

Output: fdjb

Step 2: Description

Investigation site

Conversion of English letters and numbers If the processing of z is exceeded

3. Reference code


c = "" # Define an empty string c Used to store encrypted strings 
for j in a: # Traversing string a Every one in 1 English lowercase letters 
if ord(j)+b < 124: # Judge whether it does not exceed after encryption z
c += chr(ord(j)+b) # If it does not exceed, add it directly 
else:
c += chr(ord(j)+b-26) # Otherwise add back to the initial letter 
print(c) # Print the encrypted string 

4. Other ways of writing

1. Remainder method


print ''.join([chr(ord('a')+(ord(x)+3-ord('a'))%26) for x in a])

2. Construction method


def conve(a,b):
low_set='abcdefghijklmnopqrstuvwxyz'*2
res=''
for x in a:
res+=low_set[low_set.index(x)+b]
return res
a=conve(a,b)
print a

Related articles: