-6

I've created my own Python library on my Raspberry Pi 3 called myLibrary.py, and in another program called example.py, I use this library:

import myLibrary

I need to hand this Pi over to someone else, but I want the library encrypted. I've tried GnuPG, bcrypt, and ccrypt.

My issue is that the program no longer runs once the library is encrypted, I can an error saying "No module named myLibrary

How can I encrypt the library and still be allowed to use it in my program?

Thanks

RAgarwal
  • 21
  • 3
  • You could use something like https://github.com/astrand/pyobfuscate but that may have limited value; note that this question has been asked before as well – Foon Aug 13 '18 at 21:05
  • 1
    Possible duplicate of [how to encrypt python source code?](https://stackoverflow.com/questions/6578452/how-to-encrypt-python-source-code) – Foon Aug 13 '18 at 21:05
  • Why do you want the library encrypted? – AmphotericLewisAcid Aug 13 '18 at 21:14

3 Answers3

1

Python has to read the library to run it, and to read it correctly, it must be decrypted. Otherwise how do you expect python to be able to run encrypted code?

If you distribute an encrypted library, you have to add code to decrypt the library first before importing it. Otherwise there is no way python can read encrypted data and know what it means - that is the whole point of cryptography!

nosklo
  • 217,122
  • 57
  • 293
  • 297
1

If you are trying to hide the source, it may be difficult to do so. You may want to find out whether it is worth to go through such effort.

  • PYC only distribution: One of the options, I don't personally like this option, is to distribute only the Bytecode i.e. just by distributing only .pyc files. But it will take only few more steps for anyone to deconstruct the bytecode from those files into a human readable format.
  • PyInstaller: The second option may be to use the PyInstaller and you may want to read the documentation here.

PyInstaller

  • Use Obfuscation: There may be some obfuscation tools for Python and you can look at them to obfuscate. You may want to read stackoverflow question here:

Obfuscating Python code

  • SaaS: Last but not the least if you really want to protect the code, host it in a server and give access to your service. It is so called software as a service(SaaS).
MichaelR
  • 196
  • 1
  • 14
  • 1
    `.pyc` alone can be decompiled, besides it is already used by obfuscators anyway. What most obfuscators do is create a `.pyc` that is padded with garbage between jump calls. Because Python doesn't validate the `.pyc` code before running it, the `.pyc` is still a valid program, but anyone trying to parse it will run into problems. – Havenard Aug 13 '18 at 21:30
0

Of course it wont run, the file contents stop making sense to anyone trying to read once you encrypt it (well unless they know it is encrypted and have the key to decrypt it).

The file cannot be encrypted, it has to make sense at least to the Python interpreter.

What you can do to protect the file is obfuscate it. There are several Python Obfuscators around, just Google the term.

Havenard
  • 27,022
  • 5
  • 36
  • 62