Cov txheej txheem:

Caesar Cipher Program hauv Python: 4 Cov Kauj Ruam
Caesar Cipher Program hauv Python: 4 Cov Kauj Ruam

Video: Caesar Cipher Program hauv Python: 4 Cov Kauj Ruam

Video: Caesar Cipher Program hauv Python: 4 Cov Kauj Ruam
Video: TUDev's Cryptography with Python Workshop! Creating a Substitution Cipher (Caesar Cipher) 2024, Kaum ib hlis
Anonim
Caesar Cipher Program hauv Python
Caesar Cipher Program hauv Python

Caesar Cipher yog lub qub thiab siv dav cipher uas yooj yim rau encrypt thiab decrypt. Nws ua haujlwm los ntawm kev hloov cov tsiaj ntawv ntawm cov tsiaj ntawv los tsim cov tsiaj ntawv tshiab (ABCDEF tuaj yeem hloov pauv ntau dua 4 tus ntawv thiab yuav dhau los ua EFGHIJ).

Caesar Ciphers tsis yog tus ciphers ruaj ntseg tshaj plaws nyob ntawd tab sis zoo rau cov haujlwm me me xws li hla cov ntawv zais lossis ua tus lej zais me ntsis muaj zog. Nws yog qhov yooj yim heev rau kev txiav txim siab tus lej, tab sis nws tuaj yeem ua rau nyuaj rau encrypt ib qho yog tias koj tsis muaj cov tsiaj ntawv tshwj xeeb cim.

Txhawm rau ua cov txheej txheem no yooj yim dua, peb tuaj yeem siv lub zog ntawm cov khoos phis tawj, tshwj xeeb tshaj yog cov lus programming Python.

Cov Lus Qhia no yuav qhia koj yuav ua li cas tsim ib qho program uas hloov cov lus mus rau hauv tus lej ntawm koj cov lus txib.

Khoom siv

Txhua yam koj xav tau yog tus neeg txhais lus nab nab: IDLE, Pycharm, thiab Thonny yog qee qhov zoo, xaiv dawb (Kuv siv Pycharm)

Kev paub pib ntawm Python

Kauj Ruam 1: Tshaj Tawm Cov Hloov Pauv thiab Tau Txais Cov Lus Qhia

Tshaj tawm Cov Hloov Pauv thiab Tau Txais Cov Lus Qhia
Tshaj tawm Cov Hloov Pauv thiab Tau Txais Cov Lus Qhia

Txhawm rau khaws cov hlua (ntawv) qhov tseem ceeb ntawm tus niam ntawv, xov, hloov pauv, thiab lwm yam peb yuav tsum siv cov lej sib txawv. Peb pib los ntawm kev tshaj tawm qhov hloov pauv 'cov tsiaj ntawv', 'partialOne', 'partialTwo', thiab 'newAlphabet'. Kuv tau sau cov npe ntawm qhov hloov pauv hauv Camel Case hauv kuv tus lej (thawj lo lus yog tus ntawv me thiab tus ntawv thib ob) tab sis koj tuaj yeem sau nws txhua txoj kev uas koj xav tau, tsuav koj nco ntsoov hloov nws thoob plaws qhov seem ntawm tus lej ib yam. Cov tsiaj ntawv sib txawv muaj nuj nqis "abcdefghijklmnopqrstuvwxyz". Tag nrho lwm qhov kev hloov pauv tau teeb tsa rau "", uas yog txoj hlua khoob vim peb tseem tsis tau muaj lawv qhov txiaj ntsig.

Qhov no tau ua dab tsi yog teeb tsa Cov Txheej Txheem Ib Feem, uas yog yam peb tab tom siv los tsim qhov hloov pauv. Qhov no yuav piav qhia hauv qib tom ntej.

Tom qab qhov no, peb yuav tsum tau txais cov lus thiab hloov tus nqi los ntawm tus neeg siv. Peb siv cov tswv yim ua haujlwm los ua qhov no. Ib feem ntawm txoj cai nug tus neeg siv cov lus thiab tus lej kom hloov cov tsiaj ntawv los ntawm.

CODE:

niam ntawv = "abcdefghijklmnopqrstuvwxyz"

partialOne = "ib"

partialTwo = ""

newAlphabet = ""

message = input ("Thov sau cov lus koj xav txhais:").lower ()

key = int (input ("Thov sau tus lej koj xav hloov los ntawm:"))

Kauj Ruam 2: Tsim Tus Kheej Tshiab

Tsim Cov Ntawv Tshiab
Tsim Cov Ntawv Tshiab

Tam sim no los tsim cov ntawv hloov pauv. Txhawm rau ua qhov no, peb yuav siv cov txheej txheem ib nrab. Cov kab ke ib nrab yog qhov uas lub khoos phis tawj muab cov tsiaj ntawv faib ua ob ntu (txoj hauv kev zoo los hais ib feem). Thawj ib nrab yog ntev npaum li cas koj tau hais qhia txoj haujlwm kom txav mus los, thiab qhov thib ob yog qhov seem. Lub computer hloov pauv ib feem. Qhov ntawd yog qhov ua raws txoj cai, nrog rau thawj nqe lus, uas hais tias yog tias qhov hloov pauv yog 0, cov tsiaj ntawv tshiab thiab cov tsiaj ntawv qub zoo ib yam vim tias koj tsis hloov dab tsi.

Piv txwv li:

Cov duab - 123456789

Ib Feem Ib - 123; Ib nrab Ob - 456789

Sequence Tshiab - 456789123

CODE:

yog qhov tseem ceeb == 0:

newAlphabet = niam ntawv

elif qhov tseem ceeb> 0:

partialOne = niam ntawv [: key]

partialTwo = cov tsiaj ntawv [qhov tseem ceeb:]

newAlphabet = partialTwo + partialOne

lwm tus:

partialOne = niam ntawv [:(26 + qhov tseem ceeb)]

partialTwo = niam ntawv [(26 + qhov tseem ceeb):]

newAlphabet = partialTwo + partialOne

Kauj Ruam 3: Hloov Cov Lus

Hloov Cov Lus
Hloov Cov Lus

Tam sim no peb muaj peb tus niam ntawv thiab tus niam ntawv tshiab. Txhua yam uas tshuav yog hloov cov lus rau hauv txoj cai.

Ua ntej, peb teeb tsa qhov hloov pauv tshiab thiab hu nws 'encrypted' thiab teeb nws rau "". Tom qab ntawd peb sau qhov nyuaj rau lub voj uas txheeb xyuas txhua tsab ntawv hauv kab lus thiab hloov nws mus rau tsab ntawv tshiab. Nws tso tawm qhov tshwm sim thiab muaj koj muaj nws, ua tiav txoj cai hloov pauv!

CODE:

encrypted = "" rau message_index hauv ntau (0, len (lus)):

yog message [message_index] == "":

encrypted+= ""

rau cov niam ntawv_index hauv ntau (0, len (newAlphabet)):

yog cov lus [message_index] == cov tsiaj ntawv [alphabet_index]:

encrypted+= newAlphabet [cov tsiaj ntawv_index]

luam (encrypted)

Kauj ruam 4: Ntxiv

Ntxiv
Ntxiv
Ntxiv
Ntxiv

Txuas nrog yog cov ntaub ntawv code.

Pom zoo: