Search code examples
pythonpython-3.xencryptionpycrypto

PyCrypto Not Printing Properly


Hi so before I was getting the IV in front (decrypted value of IV I believe it was) of the decrypted string. Now I don't get the string... How do I make this work, been trying for hours now...

My code:

    from Crypto.Cipher import AES
    import hashlib
    import base64
    import os
    import string

    iv = os.urandom(16)
    key = hashlib.sha256(b'mypassword123').digest()
    plaintext = (b'the password is totally not secure')
    cipher = AES.new(key, AES.MODE_CFB, iv)
    ciphertext = iv + cipher.encrypt(plaintext)
    print (ciphertext)
    print ("IV = ",iv)
    ciphertext = ciphertext.split(iv)
    ciphertext = str(ciphertext)[1].strip()
    plaintext = cipher.decrypt(ciphertext)
    print (plaintext)

Solution

  • encrypt will change cipher, you have to make a new one; and str will change byte to repr(byte), like below:

    a=b'xxx'
    str(a) # "b'xxx'"
    
    from Crypto.Cipher import AES
    import hashlib
    import base64
    import os
    import string
    
    iv = os.urandom(16)
    key = hashlib.sha256(b'mypassword123').digest()
    plaintext = (b'the password is totally not secure')
    cipher = AES.new(key, AES.MODE_CFB, iv)
    ciphertext = iv + cipher.encrypt(plaintext)
    print (ciphertext)
    print ("IV = ",iv)
    ciphertext = ciphertext.split(iv)
    ciphertext = ciphertext[1]
    cipher2 = AES.new(key, AES.MODE_CFB, iv)
    plaintext = cipher2.decrypt(ciphertext)
    print (plaintext)
    

    detail see pycrypto