2012-09-24 10 views
10

के साथ एईएस डिक्रिप्शन पैडिंग मैं पाइथन में एईएस सीबीसी डिक्रिप्शन को लागू करने की कोशिश कर रहा हूं। चूंकि सिफर्ड टेक्स्ट 16bytes का एक बहु नहीं है, इसलिए पैडिंग आवश्यक थी। गद्दी के बिना, इस त्रुटि सामनेपीकेसीएस 5 पायथन

"लेखन त्रुटि: अजीब-लंबाई स्ट्रिंग"

लेकिन मैं PyCrypto अजगर में PKCS5 लागू करने के लिए एक उचित संदर्भ नहीं मिल सका। क्या इसे लागू करने के लिए कोई आदेश हैं? धन्यवाद

मार्कस के सुझाव को देखने के बाद मैंने यह किया।

मेरा लक्ष्य वास्तव में इस कोड का उपयोग कर हेक्स संदेश (128bytes) को डिक्रिप्ट करना है। हालांकि, आउटपुट "?:" है जो बहुत छोटा है और अनपैड कमांड उन बाइट्स को हटा रहा है। यह कोड है।

from Crypto.Cipher import AES 
BS = 16 
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) 
unpad = lambda s : s[0:-ord(s[-1])] 

class AESCipher: 
    def __init__(self, key): 
    self.key = key 

    def encrypt(self, raw): 
     raw = pad(raw) 
     iv = raw[:16] 
     raw=raw[16:] 
     #iv = Random.new().read(AES.block_size) 
     cipher = AES.new(self.key, AES.MODE_CBC, iv) 
     return (iv + cipher.encrypt(raw)).encode("hex") 

    def decrypt(self, enc): 
     iv = enc[:16] 
     enc= enc[16:] 
     cipher = AES.new(self.key, AES.MODE_CBC, iv) 
     return unpad(cipher.decrypt(enc)) 

mode = AES.MODE_CBC 
key = "140b41b22a29beb4061bda66b6747e14" 
ciphertext = "4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81"; 
key=key[:32] 
decryptor = AESCipher(key) 
decryptor.__init__(key) 
plaintext = decryptor.decrypt(ciphertext) 
print plaintext 
+1

http://stackoverflow.com/questions/12524994/encrypt-decrypt-using-pycrypto-aes-256/12525165#12525165, जवाब में गद्दी कार्यों में मदद मिल सकती :) – Marcus

उत्तर

18

आपको डिक्रिप्शन से पहले अपने हेक्स एन्कोडेड मान को डीकोड करने की आवश्यकता है। यदि आप हेक्स एन्कोडेड कुंजियों के साथ काम करना चाहते हैं, तो इसे भी डीकोड करें ..

यहां, यह काम करना चाहिए।

from Crypto.Cipher import AES 
from Crypto import Random 

BS = 16 
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) 
unpad = lambda s : s[0:-ord(s[-1])] 

class AESCipher: 
    def __init__(self, key): 
     """ 
     Requires hex encoded param as a key 
     """ 
     self.key = key.decode("hex") 

    def encrypt(self, raw): 
     """ 
     Returns hex encoded encrypted value! 
     """ 
     raw = pad(raw) 
     iv = Random.new().read(AES.block_size); 
     cipher = AES.new(self.key, AES.MODE_CBC, iv) 
     return (iv + cipher.encrypt(raw)).encode("hex") 

    def decrypt(self, enc): 
     """ 
     Requires hex encoded param to decrypt 
     """ 
     enc = enc.decode("hex") 
     iv = enc[:16] 
     enc= enc[16:] 
     cipher = AES.new(self.key, AES.MODE_CBC, iv) 
     return unpad(cipher.decrypt(enc)) 

if __name__== "__main__": 
    key = "140b41b22a29beb4061bda66b6747e14" 
    ciphertext = "4ca00ff4c898d61e1edbf1800618fb2828a226d160dad07883d04e008a7897ee2e4b7465d5290d0c0e6c6822236e1daafb94ffe0c5da05d9476be028ad7c1d81" 
    key=key[:32] 
    decryptor = AESCipher(key) 
    plaintext = decryptor.decrypt(ciphertext) 
    print "%s" % plaintext 
+0

यह **; ** 'ciphertext = ... के अंत में शायद वहां नहीं होना चाहिए ... – kravietz

संबंधित मुद्दे