티스토리 뷰
전역변수
- ENC : 암호화
- DEC : 복호화
from functools import reduce
ENC = 0
DEC =1
def makeDisk(key):
keytable = map(lambda x: (chr(x+65), x),range(26))
key2index = {}
for t in keytable:
alphabet, index = t[0],t[1]
key2index[alphabet] = index
if key in key2index:
k = key2index[key]
else:
return None,None
enc_disk = {}
dec_disk = {}
for i in range(26):
enc_i = (i+k)%26
enc_ascii = enc_i + 65
enc_disk[chr(i+65)] = chr(enc_ascii)
dec_disk[chr(enc_ascii)] = chr(i+65)
return enc_disk, dec_disk
def caesar(msg, key, mode):
ret = ''
msg = msg.upper()
enc_disk, dec_disk = makeDisk(key)
if enc_disk is None:
return ret
if mode is ENC:
disk = enc_disk
if mode is DEC:
disk = dec_disk
for c in msg:
if c in disk:
ret += disk[c]
else:
ret += c
return ret
def main():
plaintext = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
key = 'F'
print('Original:\t%s' %plaintext.upper())
ciphertext = caesar(plaintext, key, ENC)
print('Caesar Cipher:\t%s' %ciphertext)
deciphertext = caesar(ciphertext, key, DEC)
print('Deciphered:\t%s' %deciphertext)
if __name__=='__main__':
main()
'Hacking > Cryptography' 카테고리의 다른 글
[WeChall] Training: Crypto - Caesar I (0) | 2017.07.12 |
---|---|
[Cryptography] C언어 caeser 암호 (1) | 2017.01.02 |
- Total
- Today
- Yesterday
- ubuntu
- 0xdeadbeef
- MySQL
- Los
- pwnable.kr
- 파이썬
- ftz
- lob
- pwnable
- BOF
- wargame
- 우분투
- 1번
- java
- 설치
- attackme
- my-pass
- wargame.kr
- 명령어
- Python
- cobolt
- 워게임
- Lord of SQL Injection
- C
- 자바
- 웹해킹
- WebHacking
- lord of sqlinjection
- webhacking.kr
- c언어
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |