古典密碼 python實現凱撒密碼

2021-10-25 10:34:17 字數 2212 閱讀 1397

def

secret()

:str

=input

("請輸入明文:"

) k =

int(

input

("請輸入移位:"))

str_list =

list

(str

)#i = 0

for i in

range

(len

(str_list)):

if(96<

ord(str_list[i]

)<

123)

: str_list[i]

=chr((

ord(str_list[i])-

ord(

'a')

+ k)%26

+ord

('a'))

elif(64

<

ord(str_list[i]

)<91)

: str_list[i]

=chr((

ord(str_list[i])-

ord(

'a')

+ k)%26

+ord

('a'))

else

: str_list[i]

= str_list[i]

for i in

range

(len

(str_list)):

print

(str_list[i]

,end="")

print

(" "

)def

deciphering()

:str

=input

("請輸入密文:"

) k =

int(

input

("請輸入移位:"))

str_list =

list

(str

) i =

0for i in

range

(len

(str_list)):

if(96<

ord(str_list[i]

)<

123)

: str_list[i]

=chr((

ord(str_list[i])-

ord(

'a')

- k +26)

%26+ord

('a'))

elif(64

<

ord(str_list[i]

)<91)

: str_list[i]

=chr((

ord(str_list[i])-

ord(

'a')

- k +26)

%26+ord

('a'))

else

: str_list[i]

= str_list[i]

for i in

range

(len

(str_list)):

print

(str_list[i]

,end="")

print

(" "

)control =

'y'while

(control ==

'y')

: moudle =

input

("加密輸入s,解密輸入d:")if

(moudle ==

's')

: secret(

) control =

input

("繼續操作輸入y,退出輸入n:"

)elif

(moudle ==

'd')

: deciphering(

) control =

input

("繼續操作輸入y,退出輸入n:"

)else

:print

("輸出錯誤!"

) control =

input

("繼續操作輸入y,退出輸入n:"

)

古典密碼 凱撒密碼Caeser

在早期,凱撒密碼 caeser cipher 是指將密文平移三位,後來經過推廣,平移個數擴充套件為任意位,即移位密碼 shift cipher 凱撒密碼 caesar 加密時會將明文中的每個字母都按照其在字母表中的順序向後 或向前 移動固定數目 迴圈移動 作為密文。例如,當偏移量是左移 3 的時候 ...

古典密碼 凱撒加密和解密

在密碼學中,愷撒密碼是一種最簡單且最廣為人知的加密技術。凱撒密碼最早由古羅馬軍事統帥蓋烏斯 尤利烏斯 凱撒在軍隊中用來傳遞加密資訊,故稱凱撒密碼。這是一種位移加密方式,只對26個字母進行位移替換加密,規則簡單,容易破解。建立類 kaiserdemo,把 hello world 往右邊移動3位 pac...

密碼學之古典凱撒

西元前一百年,在羅馬處上了一位對世界影響巨大的人物,他是當時羅馬三巨頭之一。在執政生涯中,傳言他率先使用了一種簡單的加密函,因此這種加密方法以他的名字命名。以下密文被解開後可以獲得乙個有意義的單詞,你可以用這個相同的加密向量加密附件中的密文,作為答案進行提交 frphevgl。答案為非常規形式。凱撒...