實驗吧 密碼學trivial

2021-08-17 07:33:40 字數 2512 閱讀 7952

原題**

#!/usr/bin/env python

import sys

alphal = "abcdefghijklnmopqrstuvqxyz"

alphau = "abcdefghijklmnopqrstuvqxyz"

num = "0123456789"

keychars = num+alphal+alphau

if len(sys.argv) != 3:

print

"usage: %s secret_key plaintext"

%(sys.argv[0])

sys.exit()

key = sys.argv[1]

ifnot key.isalnum():

print

"your key is invalid, it may only be alphanumeric characters"

sys.exit()

plaintext = sys.argv[2]

ciphertext = ""

for i in range(len(plaintext)):

rotate_amount = keychars.index(key[i%len(key)])

if plaintext[i] in alphal:

enc_char = ord('a') + (ord(plaintext[i])-ord('a')+rotate_amount)%26

elif plaintext[i] in alphau:

enc_char = ord('a') + (ord(plaintext[i])-ord('a')+rotate_amount)%26

elif plaintext[i] in num:

enc_char = ord('0') + (ord(plaintext[i])-ord('0')+rotate_amount)%10

else:

enc_char = ord(plaintext[i])

ciphertext = ciphertext + chr(enc_char)

print

"encryption complete, enc(%s,%s) = %s"

%(plaintext,key,ciphertext)

根據**可知,此演算法是對資料進行對稱加密。對資料進行加密時,需要輸入兩個引數,第乙個是加密的名文,第二個是金鑰,即題中所描述的資訊。

encryption complete, enc(???,t0ps3cre7key) =bot kmws mikferuigmzf rmfrxrwqe abs perudsf! nvm kda ut ab8bv_w4ue0_ab8v_ddu
???是加密的明文,key = t0ps3cre7key,密文是: bot kmws mikferuigmzf rmfrxrwqe abs perudsf! nvm kda ut ab8bv_w4ue0_ab8v_ddu ,所以解題時只需將密文和名文部分互換,即可解出答案。

解密程式

#!/usr/bin/env python

import sys

alphal = "abcdefghijklnmopqrstuvqxyz"

alphau = "abcdefghijklmnopqrstuvqxyz"

num = "0123456789"

keychars = num+alphal+alphau

key = 't0ps3cre7key'

ciphertext = 'bot kmws mikferuigmzf rmfrxrwqe abs perudsf! nvm kda ut ab8bv_w4ue0_ab8v_ddu'

plaintext = ""

for i in range(len(ciphertext)):

rotate_amount = keychars.index(key[i%len(key)])

if ciphertext[i] in alphal:

enc_char = ord('a') + (ord(ciphertext[i]) - ord('a') - rotate_amount) % 26

elif ciphertext[i] in alphau:

enc_char = ord('a') + (ord(ciphertext[i]) - ord('a') - rotate_amount) % 26

elif ciphertext[i] in num:

enc_char = ord('0') + (ord(ciphertext[i]) - ord('0') - rotate_amount) % 10

else:

enc_char = ord(ciphertext[i])

plaintext = plaintext + chr(enc_char)

print(plaintext)

so flag is ~~~自己動手,豐衣足食~

實驗吧 密碼學 變異凱撒

afz r9vyfscoeo ul rwuc 既然是凱撒,本質還是移位密碼,但是要注意區別,看到比傳統的凱撒增加了字元和數字,但是問題在於加了那些符號字元。為了避免這個問題,我們可以直接算移位個數,a f 移位5,f l移位6,z a移位7 似乎又與ascii表對應。比較簡單的指令碼就是把他們都移位...

密碼學實驗二

miller rabin檢測 miller rabin檢測是目前應用比較廣泛的一種 二次探測定理 如果p是乙個素數,且0這就是miller rabin素性測試的方法。不斷地提取指數n 1中的因子2,把n 1表示成d 2 r 其中d是乙個奇數 那麼我們需要計算的東西就變成了a的d 2 r次方除以n的餘...

CTF題庫 實驗吧(密碼學)之綜合篇

各種密碼加密與解密 md5 查md5 chamd5.org 摩斯電碼 摩爾斯電碼 柵欄密碼 ctf題庫 實驗吧 部分題目詳解 ctf題庫 實驗吧 密碼學 之變異凱撒 ctf題庫 實驗吧 密碼學 之傳統知識 古典密碼 ctf題庫 實驗吧 密碼學 之try them all ctf題庫 實驗吧 密碼學 ...