Reputation: 11
I'm building a decryptor using DES ECB
from Crypto.Cipher import DES
code = 'cb9108614c943d96bedd2bae934c5aa3d5c4318f81cc81f255127292f2935bbc0a8990f36c1ffa20a0639ed8a6989bacc36bd11f6b2ecdab'
key = b'5199D19B'
code= bytearray.fromhex(code)
print(code)
ciphert = DES.new(key, DES.MODE_ECB)
code = ciphert.decrypt(code)
code = code.decode('ISO-8859-1')
print(code)
but I keep getting this error
File "test.py", line 17, in
code = ciphert.decrypt(code)
File "build/bdist.macosx-10.14-intel/egg/Crypto/Cipher/blockalgo.py", line 295, in decrypt
TypeError: argument must be string or read-only buffer, not bytearray
Ive been working on this for hours now and can't figure out another way to store the variable.
Any ideas?
Upvotes: 0
Views: 2555
Reputation: 23296
I think this is what you want:
>>> from Crypto.Cipher import DES
>>> code = 'cb9108614c943d96bedd2bae934c5aa3d5c4318f81cc81f255127292f2935bbc0a8990f36c1ffa20a0639ed8a6989bacc36bd11f6b2ecdab'
>>> key = b'5199D19B'
>>> ciphert = DES.new(key, DES.MODE_ECB)
>>> ciphert.decrypt(code.decode('hex'))
'Well done, you have been able to decode the message. \x00\x00\x00'
Generally when passing ciphertext to a "decode" function in crypto libraries it should be passed as a bytes
object (as ciphertext can contain arbitrary bytes in general). For whatever reason it just doesn't accept a bytearray
object--that would have to be implemented separately.
code.decode('hex')
does the same thing you wanted--it converts a string of hex digits to their actual byte values.
On Python 3 code.decode('hex')
won't work. Instead use bytes.fromhex(code)
(similarly to bytearray.fromhex
but just return an immutable bytes
instance).
Upvotes: 1