I have a text file containing the following text:
message.txt
this is my secret message
I wrote a small python code to read that file and encode it using Crypto.Cipher
from Crypto.Cipher import DES
f = open('message.txt','rb')
text = f.read()
f.close()
iv = Random.get_random_bytes(8)
des1 = DES.new('01234567', DES.MODE_CFB, iv)
secret = des1.encrypt(text)
print("Encrypted text: " + secret)
output:
Encrypted text: Å..GH.ÿì.Vs~ö.~.wôÇ.k5â
I then saved the output string to a text file:
w = open('encrypted.txt','wb')
w.write(secret)
w.close()
Then I tried to open the same encrypted file and decrypt the content back to its original message:
d = open('encrypted.txt','rb')
secret = d.read()
d.close()
text = des1.decrypt(secret)
print("decreypted text is: " + text)
output:
decrypted text is: óF2â.}1Úmy secret message
It is very strange that the encryption works partially. I was under the impression that you would either encryption would produce the whole original message intact, or it would yield an unreadable gibberish text. But never expected a mixture of the two.
What am I doing wrong?
*Edit: I have edited my question: I always read binary and write binary. The half decrypted output happens when I read/write binary. When I don't read binary, the output is completely gibberish.