Lekensteyn / lglaf

LG Download Mode utility and documentation
https://lekensteyn.nl/lglaf/
MIT License
137 stars 74 forks source link

"LAF Crypto failed to import!" error is unhelpful #39

Open Jookia opened 6 years ago

Jookia commented 6 years ago
try:
    import laf_crypto
except ImportError:
    _logger.warning("LAF Crypto failed to import!")
   pass

Printing a warning but not explaining why it happened isn't helpful, there's no way to know why it didn't load (missing a library, etc)

gjdunga commented 6 years ago

So, the following advice may not work for you, but this is what I found.

I am using Windows x64 Python 3.6.4. because the laf_crypto was not importing, there must be an issue with the module. (lglaf requires _AES.py) in case your wondering.

Sure enough, there is a further dependency in laf_crypto for crypto.cipher.

However getting it installed with 'pip pycrypto'. fails. Digging in further, the project seems dead ( SEE: https://github.com/dlitz/pycrypto/issues/173 ) and is not working with Python for windows. There is a FORK of the project named pycryptodome that at least provides the library.

Your best bet is to remove any version of pycrypto and install pycryptodome with: pip uninstall pycrypto & pip install pycryptodome

See: https://pycryptodome.readthedocs.io/en/latest/src/examples.html

Lekensteyn commented 6 years ago

Only an AES ECB encrypt implementation is required, the https://cryptography.io/en/latest/ library seems better maintained, would it be OK to switch to that @tuxuser ?

tuxuser commented 6 years ago

@Lekensteyn sure!

Maybe somebody can test this?

diff --git a/laf_crypto.py b/laf_crypto.py
index fa6cad5..fb1c434 100644
--- a/laf_crypto.py
+++ b/laf_crypto.py
@@ -1,4 +1,5 @@
-from Crypto.Cipher import AES
+from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
+from cryptography.hazmat.backends import default_backend
 from utils import int_as_byte

 class LafCrypto(object):
@@ -30,5 +31,6 @@ class LafCrypto(object):
             plaintext += int_as_byte(k)
         encryption_key = LafCrypto.key_transform(encryption_key)
         xored_key = LafCrypto.xor_key(encryption_key, kilo_challenge)
-        obj = AES.new(xored_key, AES.MODE_ECB)
-        return obj.encrypt(plaintext)
+        obj = Cipher(algorithms.AES(xored_key), modes.ECB(), backend=default_backend()).encryptor()
+        # Is finalize (aka. add padding) desired?
+        return obj.update(plaintext) + obj.finalize()

BTW, oscrypto is worth a look too, completely relies on system-own crypto libs, I will post a snippet in a few -> oscrypto does not support ECB

UPDATE: PR sent, #44