2013-07-09 2 views
1

왜이 코드 않습니다RSA 암호 해독 스트립

s = "\x00\x00\x00\x00\x03\x00\x00\x00id\x00\x00" 
from Crypto.PublicKey import RSA 
from Crypto.Util import randpool 
key = RSA.generate(1024, randpool.RandomPool().get_bytes) 
d = key.encrypt(s, None) 
dec = key.decrypt(d) 
print ''.join([ "%02X " % ord(x) for x in dec ]).strip() 

출력 :

03 00 00 00 69 64 00 00 

대신

00 00 00 00 03 00 00 00 69 64 00 00 

답변

1

의 그것은 기본 암호화 알고리즘은 일부 선두를 추가 일이 발생하기 때문에 메시지에 null-bytes를 넣으면 (자), 복호 알고리즘은, 선두에있는 모든 null-byte를 스트립합니다. 메시지.

해결 방법은 암호화/암호 해독을 위해 Crypto.Cipher.PKCS1_OAEP를 사용하는 것입니다.

from Crypto.PublicKey import RSA 
from Crypto.Util import randpool 
from Crypto.Cipher import PKCS1_OAEP as PKCS 


s = "\x00\x00\x00\x00\x03\x00\x00\x00id\x00\x00" 
key = RSA.generate(1024, randpool.RandomPool().get_bytes) 

cipher = PKCS.new(key) 
encr = cipher.encrypt(s) 
decr = cipher.decrypt(encr) 
+2

+1 답변을 원하지만 PKCS1_v1_5를 PKCS1_OAEP로 대체 할 수 있습니까? 효과는 동일하지만 누군가가 깨진 암호화 체계를 사용하도록 권장하지는 않습니다. – SquareRootOfTwentyThree

+0

완료! 수정 해줘서 고마워. –