일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 스택 삭제
- 장고 하는법
- 자료구조 데크
- 스택 구현
- flex html
- rest gql
- 중위수식을 후위수식
- 데크 구현
- grid flex
- 스택 중위수식
- restapi graphql
- 풀스택
- golang
- 스택 괄호
- 스택 자료구조
- go
- 스택 삽입
- 괄호 유효성
- rest graphql
- 루비 초보
- https://stackoverflow.com/questions/219110/how-python-web-frameworks-wsgi-and-cgi-fit-together/219124#219124
- 풀스택?
- grid html
- 장고 웹 만들기
- 스택 후위수식
- 스택 유효성
- Django tutorial
- 괄호 짝 잘맞는지
- 후위수식
- 중위수식
- Today
- Total
donchanee
3-round Simple DES / CBC 모드 본문
이번엔 Simple DES 모델을 바탕으로 python 코드를 짜보았다.
생각보다 어렵지 않은 과정이어서 흥미로웠다.
cbc모드도 같이 구현했다.
import random
def expansion(pblock):
left = []
left = pblock[:2]
left.append(pblock[3])
left.append(pblock[2])
left.append(pblock[3])
left.append(pblock[2])
left.extend(pblock[4:])
return left
def left_half(pblock):
num = int(len(pblock)/2)
return pblock[:num]
def right_half(pblock):
num = int(len(pblock)/2)
return pblock[num:]
def xor(pblock, key):
new = ''
for bit, key_bit in zip(pblock, key):
new += str(bit ^ key_bit)
return list(map(int,new))
def sbox1(bits):
sbox1 = [[[1,0,1], [0,1,0], [0,0,1], [1,1,0], [1,1,0], [1,0,0], [1,1,1], [0,0,0]],
[[0,0,1], [1,0,0], [1,1,0], [0,1,0], [0,0,0], [1,1,1], [1,0,1], [0,1,1]]]
index = bits[1]*4 + bits[2]*2 + bits[3]*1
return sbox1[bits[0]][index]
def sbox2(bits):
sbox2 = [[[1,0,0], [0,0,0], [1,1,0], [1,0,1], [1,1,1], [0,0,1], [0,1,1], [0,1,0]],
[[1,0,1], [0,1,1], [0,0,0], [1,1,1], [1,1,0], [0,1,0], [0,0,1], [1,0,0]]]
col = bits[1]*4+bits[2]*2+bits[3]*1
return sbox2[bits[0]][col]
def key_shift_enc(key) :
key_s = key[1:]
key_s.append(key[0])
return key_s
def key_shift_dec(key) :
key_s = key[:8]
key_s.insert(0, key[-1])
return key_s
def function(key, pblock):
key = key[:8]
rpblock = pblock[6:]
lpblock = pblock[:6]
bits = expansion(rpblock)
bits = xor(bits, key)
left = left_half(bits)
right = right_half(bits)
left = sbox1(left)
right = sbox2(right)
bits = left + right
bits = xor(bits, lpblock)
bits = rpblock + bits
return bits
def sdes_encrypt(key, pblock):
pblock_tmp = []
pblock = function(key, pblock)
key = key_shift_enc(key)
pblock = function(key, pblock)
key = key_shift_enc(key)
pblock = function(key, pblock)
pblock_tmp.extend(pblock[6:])
pblock_tmp.extend(pblock[:6])
pblock = pblock_tmp
return pblock
def sdes_decrypt(key, pblock):
key3 = []
pblock_tmp = []
key3 = key[2:]
key3.extend(key[:2])
key = key3
pblock = function(key, pblock)
key = key_shift_dec(key)
pblock = function(key, pblock)
key = key_shift_dec(key)
pblock = function(key, pblock)
pblock_tmp.extend(pblock[6:])
pblock_tmp.extend(pblock[:6])
pblock = pblock_tmp
return pblock
def cbc_genkey():
numlist = [random.randint(0,1) for _ in range(9)]
return numlist
def cbc_encrypt(key, IV, pblock):
pblock_tmp = []
result = []
cnt = 0
IV_tmp = IV
while True:
pblock_tmp = pblock[cnt*12:cnt*12+12]
if(len(pblock)/12 == cnt):
break
else:
pblock_tmp = xor(pblock_tmp, IV_tmp)
pblock_tmp = sdes_encrypt(key, pblock_tmp)
IV_tmp = pblock_tmp
cnt += 1
result.extend(pblock_tmp)
return result
def cbc_decrypt(key, IV, cblock):
cblock_tmp = []
result = []
cnt = 0
IV_tmp = []
while True:
cblock_tmp = cblock[cnt*12:cnt*12+12]
if(len(cblock)/12 == cnt):
break
else:
cblock_tmp = sdes_decrypt(key, cblock_tmp)
if(cnt == 0):
cblock_tmp = xor(cblock_tmp, IV)
else:
cblock_tmp = xor(cblock_tmp, IV_tmp)
IV_tmp = cblock[cnt*12:cnt*12+12]
cnt += 1
result.extend(cblock_tmp)
return result