WIP library that will encrypt some text for the purpose of obfuscating my code I'm making a library called PyDll:

def encrypt(v):
    res=[]
    out=open("save.txt","w")
    j=bin(int.from_bytes(v.encode(), 'big'))
    out.write(j)

def decrypt():
    de=open("save.txt", "r")
    dev=de.read()
    dev=int(dev, 2)
    return dev.to_bytes((dev.bit_length() + 7) // 8, 'big').decode()


Here are some examples:

&gt;&gt;&gt;encrypt("import tkinter.messagebox as m \n m.showinfo('hello', 'Hey!')")  
&gt;&gt;&gt;exec(decrypt())


This will execute the code:

import tkinter.messagebox as m
m.showinfo('hello', 'Hey!')")


But, the file save.txt's value is:

0b110100101101101011100000110111101110010011101000010000001110100011010110110100101101110011101000110010101110010001011100110110101100101011100110111001101100001011001110110010101100010011011110111100000100000011000010111001100100000011011010010000000001010011011010010111001110011011010000110111101110111011010010110111001100110011011110010100000100111011010000110010101101100011011000110111100100111001011000010000000100111010010000110010101111001001000010010011100101001


In other words, it converts the text to binary and then converts the binary back to normal text.

If you know any way I can improve this, please tell me.
Preamble

What you're looking for is a way to obfuscate your code, not a way to encrypt it. The issue lies in the fact that the interpreter needs to have access to the unencrypted source code. This means that you have to store the key somewhere and decrypt the file before runtime, which defeats the purpose of encrypting the source code in the first place. It's almost impossible to properly protect Python code and there's lots of decompilers out there.



There's several better ways to go about doing this:


Use a restrictive (proprietary) license. Be sure to do some research on what license is best, so you know exactly what restrictions and rights people have when using your software.
If you need to interact with a (web)server, try to make as much as possible happen server-side, which, provided you secure your server, prevents people from viewing the source code.
Push frequent, backward-incompatible updates.
Think again and decide you may as well just release it under an open-source license like the MIT license or the GNU GPL.


Your code

Ram Chandra Giri covered most issues with style conventions. I'd like to point out something:


After calling open, one should call close to properly clean up. We may be tempted to do something like this:

f = open(FILENAME, MODE)
# Do stuff with `f`
f.close()


But what if an exception occurs before the close call? So, we have to wrap it in a try: | finally: statement instead:

try:
    f = open(FILENAME, MODE)
    # Do stuff with `f`
finally:
    f.close()


Python provides a cleaner way to do this, using context managers:

with EXPR as VAR:
    BLOCK


In the case of file descriptors, we can use:

with open(FILENAME, MODE) as f:
    # Do something with `f`


Specifically:

with open("save.txt", "r") as f:
    contents = f.read()
    contents = int(contents, 2)
    return contents.to_bytes(
        (contents.bit_length() + 7) // 8,
        'big'
    ).decode()

You can use sys.byteorder to find the system's native byte order. Alternatively if you want to use 'big' as byteorder, I'd make it a constant:

BYTE_ORDER = "big"

If the file is sufficiently large, the system may run out of memory trying to read it all at once. We can avoid this problem by reading 'chunks' into memory.


Here's the same code, but rewritten to implement all changes:

import sys


def write_binary_string(string, filename):
    """Convert `string` to binary and write it to `filename`.

    :param str string: The string to be obfuscated.
    :param str filename: The filename to write to.
    :raises FileNotFoundError: If `filename` is not a file.
    :return: None
    """
    with open(filename, "w") as f:
        binary_representation = bin(int.from_bytes(
            string.encode("utf-8"),
            sys.byteorder
            )
        )
        f.write(binary_representation)


def read_binary_string(filename):
    """Open `filename` and return the deobfuscated contents.

    :param str filename: The file to read from.
    :raises FileNotFoundError: If `filename` is not a file.
    :return: None
    """
    CHUNK_SIZE = 65536
    binary_representation = []
    with open(filename, "r") as f:
        while True:
            chunk = f.read()
            if not chunk:
                break
            binary_representation.append(chunk)
    binary_representation = "".join(binary_representation)
    return int(binary_representation, 2).to_bytes(
        (binary_representation.bit_length()+7) // 8,
        sys.byteorder
    ).decode("utf-8")