Python 如何旋转字符串中的字符以加密消息
我试着和我的一个朋友玩得开心,我们是特务,但是我们怎么能成为特务而没有绝密的信息代码来互相交流呢Python 如何旋转字符串中的字符以加密消息,python,cryptography,Python,Cryptography,我试着和我的一个朋友玩得开心,我们是特务,但是我们怎么能成为特务而没有绝密的信息代码来互相交流呢 # txt = the secret message to convert! # n = number of times to jump from original position of letter in ASCII code def spy_code(txt,n): result = '' for i in txt: c = ord(i)+n
# txt = the secret message to convert!
# n = number of times to jump from original position of letter in ASCII code
def spy_code(txt,n):
result = ''
for i in txt:
c = ord(i)+n
if ord(i) != c:
b = chr(c)
result += b
print result
spy_code('abord mission, dont atk teacher!',5)
在转换秘密消息中的消息后,我们将得到一行文本
fgtwi%rnxxnts1%itsy%fyp%yjfhmjw&
问题是,我们想要达到这样的结果:
fgtwi rnxxnts, itsy fyp yjfhmjw!
只考虑信件
只使用间谍代码转换字母,不要转换空格或特殊符号。一个简单的方法,使用GP89给你的提示 只需检查您当前的角色是否在字母列表中;否则就按原样退回
import string
vals = string.letters
def spy_code(txt,n):
result = ''
for i in txt:
if i in vals:
c = ord(i)+n
if ord(i) != c:
b = chr(c)
result += b
else:
result += i
print result
spy_code('abord mission, dont atk teacher!',5)
返回
fgtwi rnxxnts, itsy fyp yjfhmjw!
我知道这很旧,但您可以使用:
我没有得到否定票:s。这是一个正确的问题,因为记录它们不会帮助你获得选票:作为一个提示,你的问题永远不会以“字符串中的移位字符”或其他更一般的标题来结束:)提示:如果你要成为特别探员,并且你有一个安全的渠道(你们两个实际上是在物理上相遇的),你为什么不使用而不是(使用频率分析很容易破坏)来交换你的信息呢。一次性pad的示例实现(以numpy为单位)是相关问题的一半
s = 'Impending doom approaches!'
n = 6
result = ''
for c in s:
result += chr(ord(c) + n) if c.isalpha() else c
print(result)
>>> Osvktjotm juus gvvxuginky!