does ecb mode have bugs? why my program not working even I did everything... I am stuck please help.
Text I encrypted : hello world
My attempt :
from Crypto.Cipher import AES
import base64
key = '0123456789abcdef'
#this is the password that we are going to use to encrypt and decrpyt the text
def encrypt(text):
global key
cipher = AES.new(key, AES.MODE_ECB)
if len(text) %16!=0:
while not len(text)%16==0:
text=text+" "
encrypted_text =cipher.encrypt(text)
return encrypted_text.strip()
def decrypt(text):
global key
decipher = AES.new(key, AES.MODE_ECB)
if len(text)%16!=0:
while len(text)%16!=0:
text=str(text)+" "
return decipher.decrypt(text).strip()
text=input("Enter encrypted text here : ")
#b'XhXAv\xd2\xac\xa3\xc2WY*\x9d\x8a\x02'
print(decrypt(text))
Input :
b'XhXAv\xd2\xac\xa3\xc2WY*\x9d\x8a\x02'
output :
b"yR\xca\xb1\xf6\xcal<I\x93A1`\x1e\x17R\xbb\xc8(0\x94\x19'\xb3QT\xeb\x9b\xfe\xc8\xce\xf4l9\x92\xe8@\x18\xf2\x85\xbe\x13\x00\x8d\xa8\x96M9"
Required Output :
hello world
Do not pad cipher text in decryption. Note that you might need to remove padding after decryption and not before it.
And here is the result:
UPDATE: You can also encode ciphertext by calling
b64encodeinbase64, in this case you need to decode before decryption by callingb64decode. It is recommended to use codings like base64 to make it easier for transferring ciphertext in network and Internet.