凯撒密码加密解密python(凯撒密码加密解密算法)

2023-03-01 2:44:36 密码用途 思思

jmu-python-凯撒密码加密算法,谢谢

def encryption():

str_raw = input("请输入明文:")

k = int(input("请输入位移值:"))

str_change = str_raw.lower()

str_list = list(str_change)

str_list_encry = str_list

i = 0

while i len(str_list):

if ord(str_list[i]) 123-k:

str_list_encry[i] = chr(ord(str_list[i]) + k)

else:

print ("解密结果为:"+"".join(str_list_decry))

while True:

print (u"1. 加密")

print(u"2. 解密")

choice = input("请选择:")

if choice == "1": encryption()

elif choice == "2": decryption()

else: print (u"您的输入有误!")

凯撒密码加密解密python(凯撒密码加密解密算法) 第1张

python中凯撒密码num=num+key是什么意思

python中凯撒密码num=num+key是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。根据查询相关公开信息,凯撒密码是古罗马凯撒大帝用来对军事情报进行加密的算法,它采用了替代方法将信息中的每一个英文字母循环替换为字母表序列中该字符后面的第k个字符(k为密钥)。加密方法:C=(P+k)mod26,P为原文字符,k为密钥,解密方法:P=(C-3)mod26。

求python中的恺撒密码的加密,解密,以及破解的程序

凯撒密码作为一种最为古老的对称加密体制,在古罗马的时候都已经很流行,他的基本思想是:通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推X将变成A,Y变成B,Z变成C。由此可见,位数就是凯撒密码加密和解密的密钥。

如下代码是以偏移量为13展开计算的。123

源代码如下:

sr1="abcdefghijklmnopqrstuvwxyz"sr2=sr1.upper()

sr=sr1+sr1+sr2+sr2

st="The Zen of Python"sResult=""for j in st: if j==" ":

sResult = sResult +" "

continue

i=sr.find(j) if(i-1):

sResult=sResult+sr[i+13]print sResult12345678910111213

运行结果为:

Gur Mra bs Clguba

如何用python编写凯撒密码 ?

凯撒密码是对字母表整体进行偏移的一种变换加密。因此,建立一个字母表,对明文中每个字母,在这个字母表中偏移固定的长度即可得到对应的密文字母。

最基本的实现如下:

def caesarcipher(s: str,rot: int=3) -str:

    _ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

    encode = ''

    i = 0

    for c in s:

        try:

            encode += _[(_.index(c.upper()) + rot) % len(_)]

        except (Exception,) as e:

            encode += c

    return encode

print(caesarcipher('hellow'))

print(caesarcipher('KHOORZ', -3))

如果要求解密后保持大小写,那么,字母表_还需要包含所有小写字母并且index时不对c做upper处理.

同样的,也可以在字母表中追加数字,各种符号,空格等.

怎么用Python编辑出此凯撒密码的解密密码?

凯撒密码的加密密钥与解密密钥是相反数,因此,k给相反数即可:

kaisa(kaisa(s, 3), -3)

用Python2.7.10编写凯撒密码加密和解密程序

s = raw_input('[开始加密]please input your str:')

s = list(s)

n = 0

for sw in s:

    s[n] = chr(ord(sw)+3)

    n = n + 1

sout = ''

for sw2 in s:

    sout = sout + sw2

print '[加密结果]:',sout

解密的类似,主要用到ord、chr函数。