SoFunction
Updated on 2024-11-19

Making a file encryptor with Python

preamble

For some know-it-all reason, my parents are particularly interested in the contents of the files on my computer. In order to prevent information leakage, I made a file encryptor in Python overnight to prevent my important information from being leaked.

在这里插入图片描述

download address

github:/13337356453/FileCipher

The resources on GitHub are Python source code

Effect Preview

I made the window in PyQt for ease of operation. It runs like this.

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

The encryption is still very good.

encryption algorithm

For me, the writing of the gui in the program was kind of easy. The hard part is the writing of the encryption algorithm.

I'll post the code for the password first and explain it in detail.

class Cipher:
    key = ""

    def __init__(self, key):
         = key

    def setKey(self, key):
         = key

    def getKey(self):
        return 

    def parseKey(self, key):
        # Processing keys
        if key != "":
            o = 0
            for k in key:
                n = 0
                i = str(ord(k))
                for t in i:
                    n += int(t)
                o += n
            # Keep the key range between 10 and 100
            while True:
                if o < 10:
                    o = int(o * 2)
                elif o > 100:
                    o = int(o / 2)
                else:
                    return o
        return

    def getOdd(self, max):
        return [i for i in range(1, max + 1) if i % 2 == 1]

    def encrypt(self, data):
        # Encryption algorithms
        if data == "":
            return
        result = ""
        length = len(data)  # Get data length
        a = [ord(x) for x in data]
        # Determine if it is a multiple of 4
        remainder = length % 4  # Remainder
        if remainder != 0:
            b = 4 - remainder
            for c in range(b):
                (0)
        # First grouping
        groups = []
        d = len(a) // 2
        e1 = a[:d]
        e2 = a[d:]
        indexs = (d)
        ([e1[i - 1] for i in indexs])
        ([e1[i] for i in indexs])
        ([e2[i - 1] for i in indexs])
        ([e2[i] for i in indexs])
        # Second grouping
        f1 = groups[0] + groups[3]
        f2 = groups[1] + groups[2]
        # First encryption
        keycode1 = (())
        g = []
        for h in f1:
            i = h + keycode1
            j = chr(i)
            (i)
            result += j
        # Get the keycode a second time
        k = str(sum(g))
        keycode2 = (k)
        # Second encryption
        for l in f2:
            m = l + keycode2
            n = chr(m)
            result += n
        # Encryption complete
        return result

    def decrypt(self, data):
        # Decryption algorithms
        if data == "":
            return
        result = ""
        # Get keycode1
        keycode1 = (())
        # First decryption
        a = len(data) // 2
        b1 = data[:a]
        b2 = data[a:]
        c = [ord(d) for d in b1]
        e = [f - keycode1 for f in c]
        # Get keycode2
        g = str(sum(c))
        keycode2 = (g)
        # Second decryption
        h = [ord(i) for i in b2]
        j = [k - keycode2 for k in h]
        # f1 corresponds to e , f2 corresponds to j
        # First grouping
        k = len(e) // 2
        group1 = e[:k]
        group4 = e[k:]
        group2 = j[:k]
        group3 = j[k:]
        # Second grouping
        datalength = len(group1) + len(group2) + len(group3) + len(group4)  # Data length
        l = datalength // 4
        m = []
        for n in range(l):
            (group1[n])
            (group2[n])
        o=[]
        for p in range(l):
            (group3[p])
            (group4[p])
        # Data splicing
        q=m+o
        for r in q:
            result+=chr(r)
        # Return results
        return result

Presumably the process goes something like this

在这里插入图片描述

Random drawings, probably this is the process, any errors do not care about it

The key is first processed by converting the key to ASCII and adding all the numbers to get the sum, which is controlled by multiplication and division in the range of 10-100.
The data is then processed and divided into 4 groups, with the missing digits filled in with 00. After grouping the data, cross-group them again. Get the result of the second grouping. Process the result one using the key to get data one. Then the sum of result one is processed with the key to get key two.
The result two is then processed using key two to get data two.
Splice data one and two to get encrypted data.

Decryption is the reverse operation.

summarize

to this article on the use of Python to create a file encryptor article is introduced to this, more related Python file encryption content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!