특정 프로젝트에 대한 필자의 요구를 충족시키는 간단한 순수 파이썬 복어 구현을 발견했습니다.이 블록 암호 키 기능을 리 팩터 처리합니다.
날 귀찮게 그것의 한 부분있다 :
def initialize(key):
"""
Use key to setup subkeys -- requires 521 encryptions
to set p and s boxes. key is a hex number corresponding
to a string of 32 up to 448 1s and 0s -- keylen says
how long
"""
# Note that parray and sboxes are globals that have been pre-initialized.
hexkey = hex(key)[2:]
if hexkey[-1]=='L':
hexkey = hexkey[:-1]
if len(hexkey)%2==1:
hexkey = '0'+hexkey
lenkey = len(hexkey)/8
if lenkey==0:
pos=0
# XOR key segments with P-boxes
for i in range(18):
if lenkey>0:
pos = (i%lenkey)*8 # offset into key gives subkey
subkey = eval('0x'+hexkey[pos:pos+8]+'L')
parray[i] ^= subkey # immediate XOR -- Python 2.0+ syntax
# encrypt 0-data, then keep re-encrypting and reassigning P-boxes
output = 0L
for i in range(0,17,2):
output = bfencrypt(output)
parray[i], parray[i+1] = output>>32, output & 0xFFFFFFFFL
# re-encrypt and reassign through all the S-boxes
for i in range(4):
for j in range(0,255,2):
output = bfencrypt(output)
sbox[i][j],sbox[i][j+1] = output>>32, output & 0xFFFFFFFFL
# print "Initialization complete"
subkey = eval('0x'+hexkey[pos:pos+8]+'L')
은? 더 좋은 방법이 있다고 말해주세요.
문자열의 16 진수 값 대신 실제 정수 유형을 사용하도록 리팩터링하는 방법이 없습니까?
이 코드는 schneier의 테스트 벡터 인 http://www.schneier.com/code/vectors.txt와 정확하게 일치합니다. 즉, 나는이 문자열 슬라이스의 어리 석음을 엉망이 아닌 schneier의 C 코드 (매우 복잡하지 않음)에 대해 다시 작성하려고합니다. –
사용 된 특정 키가 될 수 있습니까? 선행 제로 니블 (94 % 가능성)이 없으면 문자열은 8 진수 숫자의 배수가 될 것이므로 실제로 차이가 없습니다. 그렇지 않으면 다른 경계에서 일반 내부 표현으로 분할됩니다. – Brian