这个加密文本的程序需要以下用户输入:
1.要加密的文本text
2.正整数prime而有效的prime值包括:1,3,5,7,9,11,15,17,19,21,23,25
3.正整数number和字母移动位置n,其中n由下式给出:
(primer ∗ position of each letter + number)
例如,如果primer = 1,number = 1,则’A’将变为’B’
则:(1 * 0 ['A’的字母位置] + 1)= 1 ['B’的字母位置]。
- 思路是首先由用户输入文本和两个加密值,先判断是否输入符合要求
def check(text, prime, positiveInt):
if not text.isalpha():
print("text are not right!\n")
prime = str(prime)
if not prime.isdigit():
print("prime are not right!\n")
positiveInt = str(positiveInt)
if not positiveInt.isdigit():
print("positive number are not right!\n")
- 然后检验其字母是大写还是小写然后匹配处位置作下标再分别代入公式后结果用append()函数加入列表即可:
def transform(text, prime, positiveInt):
PrimeArea = [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]
lower_str = 'abcdefghigklmnopqrstuvwxyz'
upper_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZ'
str = []
if prime not in PrimeArea:
print("please enter right prime!!!\n")
else:
for item in range(len(text)):
if text[item].isupper():
for i in range(26):
if text[item] == upper_str[i]:
bposition = ((prime * i) + positiveInt) % 26
str.append(upper_str[bposition])
elif text[item].islower():
for i in range(26):
if text[item] == lower_str[i]:
bposition = ((prime * i) + positiveInt) % 26
str.append(lower_str[bposition])
else:
print("error text!!!\n")
print("".join(str))