赞
踩
编写程序,要求输入一个字符串,然后输入一个整数作为凯撒加密算法的密钥,输出该字符串加密后的结果。(提示:凯撒加密是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目n进行偏移后被替换成密文,此处的n就是密钥。)
- '''
- import string
- def kaisa(s,k):
- lower=string.ascii_lowercase
- upper=string.ascii_uppercase
- before=string.ascii_letters
- after=lower[k:]+lower[:k]+upper[k:]+upper[:k]
- table=''.maketrans(before,after)
- return s.translate(table)
- s=input('input string:')
- k=int(input('input int security-key:'))
- print(kaisa(s,k))
- '''
-
- '''
- #凯撒加密
- import string
- #upper and lower letters
- print(string.ascii_letters)
- #0123456789
- print(string.digits)
- #a string containing all ASCII punctuation characters
- print(string.punctuation)
- #upper letters
- print(string.ascii_uppercase)
- #lower letters
- print(string.ascii_lowercase)
- uInput=input('input string:')
- secKey=int(input('input int security-key:'))
- #secKey=6
- lower=string.ascii_lowercase
- upper=string.ascii_uppercase
- before=string.ascii_letters
- after=lower[secKey:]+lower[:secKey]+upper[secKey:]+upper[:secKey]
- #print(after)
- table=''.maketrans(before,after)
- #print(table)
- print(uInput.translate(table))
- '''
-
- '''
- #凯撒加密
- key=int(input('input caesar encrypt key( key>0 ):'))
- inputstr=input('input string:')
- inputStrLower=inputstr.lower()
- #print(key)
- #print(inputstr)
- encryptstr=''
- letters='abcdefghijklmnopqrstuvwxyz'
- for i in inputStrLower:
- if i in letters:
- num1=letters.index(i)+1
- encryptkey=num1+key
- if encryptkey>26:
- encryptkey=encryptkey%26
-
- #print(encryptkey)
- encryptstr=encryptstr+letters[encryptkey-1]
-
- print(encryptstr)
- '''
-
- '''
- #凯撒加密破解
- encryptStr='klmnopqrstuvwxyzabcdefghij'
- letters='abcdefghijklmnopqrstuvwxyz'
- for key in range(1,27):
- decryptstr=''
- print(key)
- for j in encryptStr:
- if j in letters:
- num2=letters.index(j)
- decryptkey=num2+key
- if decryptkey>26:
- decryptkey=decryptkey%26
-
- decryptstr=decryptstr+letters[decryptkey-1]
- print(decryptstr)
- print('---------------------')
- '''
-
- '''
- #凯撒加密-python密码学编程
- message='guvF vF FrpErG zrFFntr.'
- key=13
- mode='encrypt'
- LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
- translated=''
- messagge=message.upper()
- for symbol in message:
- print(symbol)
- if symbol in LETTERS:
- num=LETTERS.find(symbol)
- if mode=='encrypt':
- num=num+key
- elif mode=='decrypt':
- num=num-key
-
- if num>=len(LETTERS):
- num=num-len(LETTERS)
- elif num<0:
- num=num+len(LETTERS)
- translated=translated+LETTERS[num]
-
- else:
- translated=translated+symbol
- print(translated)
- '''
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。