In this tutorial, you will learn How to Encrypt a string in python. The encrypted value is impossible to decrypt. To do this, we need 3 modules, marshal{code}, zlib{code} and base64{code}. These modules are pre-installed in python, so you don't need to worry about it.
{tocify}
Understanding
Python has a built-in function called compile{code} It takes 3 argument. First argument is the string which you want to encrypt. Second argument is filename, filename can be anything it doesn't matter. Third argument is mode, mode will be one of eval{code} or exec{code}. Normally, eval{code} used to doing sum. Eg: eval("5+3"){code}. And exec{code} used to execute python code as string format. So compile{code} is impossible to decrypt. Eg:
>>> encrypted_text=compile('print("This is encrypted")', 'filename', 'exec')
>>> print(encrypted_text)
<code object <module> at 0x0000002979B3F5B0, file "filename", line 1>
>>> exec(encrypted_text)
This is encrypted{codeBox}
Here you can see, the compiled value can be executed but not possible to decrypt. If you print that value, it returns a code object. Now, encrypted_text{code} can be seen. We will use that 3 modules for encrypt and decrypt that variables value.
Step 1: Importing modules
from marshal import dumps, loads
from zlib import compress, decompress
from base64 import b64encode, b64decode{codeBox}
Explanation:
- dumps - returns a byte object
- loads - convert that byte object to a value
- compress - returns a byte object containing data
- decompress - returns a byte object containing the uncompressed data
- b64encode - encodes the byte-like object using Base64 and returns a byte object
- b64decode - decodes the Base64 encoded byte-like object
Step 2: Compile your string
Remember, compile{code} needs 3 argument. First one is the string, second one is filename and third one is exec{code} or eval{code}. We will use exec here.
secret_text="print('This is a secret text')"Here I have stored my secret string in a variable then compiled that string and stored it another variable.
enc_text=compile(secret_text, 'fakefile', 'exec'){codeBox}
Step 3: Encrypt that compiled value
At first, we need to convert that compiled value into byte then compress that byte for small data (it also returns a byte object) and at last, encode that byte object with base64 encoding.
encoded_text=b64encode(compress(dumps(enc_text))){codeBox}Here dumps{code} function convert that compiled value into byte then compress{code} function compresses that byte value and also returns a byte object and b64encode{code} function encode that byte object with base64 encoding.
Step 4: Store the encrypted value in another file
We can simply print{code} that encrypted value and store it in another file. So till here, the full code is:
from marshal import dumps, loadsand output is:
from zlib import compress, decompress
from base64 import b64encode, b64decode
secret_text="print('This is a secret text')"
enc_text=compile(secret_text, 'fakefile', 'exec')
encoded_text=b64encode(compress(dumps(enc_text)))
print(encoded_text){codeBox}
b'eJx7zIAGmIDYAYiLeYBEKkMKQzMjI0MKYzCDJlOVaEhGZrECECUqFKcmF6WWKJSkVpT4aTLeYi0oyswrWclQBNIOJm5xpCVmp6Zl5qTe4rDJzU8pzUm1YwSKfwZZAgDlhhoJ'{codeBox}We need to store that output in another file and also need to import those modules:
from marshal import dumps, loads
from zlib import compress, decompress
from base64 import b64encode, b64decode
enc_value=b'eJx7zIAGmIDYAYiLeYBEKkMKQzMjI0MKYzCDJlOVaEhGZrECECUqFKcmF6WWKJSkVpT4aTLeYi0oyswrWclQBNIOJm5xpCVmp6Zl5qTe4rDJzU8pzUm1YwSKfwZZAgDlhhoJ'{codeBox}
Step 5: Decrypt the value
We will use opposite functions which we used to encrypt. So dumps will be loads, compress will be decompress and b64encode will be b64decode.
dec_value=loads(decompress(b64decode(enc_value))){codeBox}
We are done! If we print this, we will see the code object again
Step 6: Execute the value
Now, We are executing the value with exec{code}, and also printing that to see is it visible publicly.
print(dec_value)And the output is:
exec(dec_value){codeBox}
<code object <module> at 0x000000EC6AAFB920, file "fakefile", line 1>
This is a secret text{codeBox}
So that's how you can encrypt and execute a string
Full Code
File 1:
from marshal import dumps, loadsFile 2:
from zlib import compress, decompress
from base64 import b64encode, b64decode
secret_text="print('This is a secret text')"
enc_text=compile(secret_text, 'fakefile', 'exec')
encoded_text=b64encode(compress(dumps(enc_text)))
print(encoded_text){codeBox}
from marshal import dumps, loads
from zlib import compress, decompress
from base64 import b64encode, b64decode
enc_value=b'eJx7zIAGmIDYAYiLeYBEKkMKQzMjI0MKYzCDJlOVaEhGZrECECUqFKcmF6WWKJSkVpT4aTLeYi0oyswrWclQBNIOJm5xpCVmp6Zl5qTe4rDJzU8pzUm1YwSKfwZZAgDlhhoJ'
dec_value=loads(decompress(b64decode(enc_value)))
print(dec_value)
exec(dec_value){codeBox}