0

I am trying to understand how to use ARC4 encryption algorithm (just for educational purposes, I know it's not secure anymore), and I seem to be able to encrypt text but not decrypt it. Here is the very basic code:

from Crypto.Cipher import ARC4

key = '#KCMDDC51#-890'

cipher = ARC4.new(key)
crypted = cipher.encrypt('wha frjcnvnb')
print(crypted)

notEncrypted = cipher.decrypt(crypted)
print(notEncrypted)

now, the encryption part outputs the following: b'\x82J\x96~r\x9e\xe2\xd7G\xa5Jz\xba'

but the decryption part doesn't output normal text, but this other jumbled up thing: b'qI\xa7\x97\xb7\x94XQ\x8a\xb1iu\xc1'.

why?

Alex K.
  • 171,639
  • 30
  • 264
  • 288
JustABeginner
  • 321
  • 1
  • 5
  • 14
  • 2
    Because RC4 has an internal state that changes every time you encrypt/decrypt something, so you have to use a new instance for decryption. – t.m.adam Aug 23 '19 at 14:59
  • thanks! it worked. – JustABeginner Aug 23 '19 at 16:24
  • so, say I wanted to decrypt something from hex values that i have put in a .txt file, how would I do that? because I have tried but seems to give me the same problem as before, since I wasn't the one who encrypted the initial stream, and also I am not sure whether it's getting the right format for the input as I am reading from file, transforming the input from hex to bytes and then decrypting. Thanks! – JustABeginner Aug 23 '19 at 16:39
  • If you have the key you should be able to decrypt the file. If the contents are hex-encoded you should decode first, then decrypt. If you're using Python3 use `crypted = bytes.fromhex(crypted)`, if using Python2 I think you can decode with `crypted = crypted.decode('hex')`. – t.m.adam Aug 23 '19 at 16:49
  • I tried using bytes.fromhex and then decrypt with the key, but it doesn't seem to work. – JustABeginner Aug 23 '19 at 21:01
  • I can't do much with the given info. Could you share your code, the code that was used for encryption, if it is different and the tracebacks, if any errors occurred? – t.m.adam Aug 23 '19 at 21:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198391/discussion-between-justabeginner-and-t-m-adam). – JustABeginner Aug 23 '19 at 22:40

0 Answers0