jeudi 26 mars 2020

I've Invented a data encryption method. I wish to know if it's safe

I'm not 100% sure if this is the best place to ask this. If so, can you please suggest a better site to me.

I've written a program in python 3 to encrypt data. But it uses a completely new data encryption method I made. And I wish to know whether any of you think it's secure enough for any practical use. Or, if you see any flaws in it.

The basic gist of the program is this:

The program converts each character of the data into their ASCII numbers. Then to encrypt it, it "randomly" adds a large number to the original ASCII number of each character.

But it's not random, because the seed has been set using the random.seed() function. And The seed the program sets it to is determined by the key.

And then it shuffles each digit.

Due to lack of a better name, I called this method SABONK. It doesn't stand for anything.


import random


def ASCII(x): #converts string to ASCII and returns as a list.
    return [ord(c) for c in x]

def ASCII_char(x): #turns a list of ASCII numbers back into text and in a string
    try:
        result = ""
        for i in x:
            result = result + chr(i)
        return result
    except:
        return None

def modifier(key): #returns the "modifier" value used for encryption
    return len(key)*sum(key)

def pad(n, x): #adds 0 to front of number so the length is as required.

    if len(str(x)) >= n: return x
    return ("0"*(n-len(str(x))))+str(x)

class SABONK:
    def __init__(self, key, max=856076, min=100, length=7):
        self.keyString = key
        self.key = ASCII(key)
        self.m = modifier(self.key)

        self.length = 7
        self.maxLength = max
        self.minLength = min

    def setKey(self, newKey):
        pass

    def Encrypt(self, data, password=None): #the main encrypt function
        if data == "": return ""
        #setting up variables needed.
        key = password
        if password == None: key = self.key #If password is None, use the password saved in the class.

        return self.shuffle(self.combine(self.basicEncrypt(data, key)))

    def Decrypt(self, data, password=None, toText=True): #the main encrypt function
        if data == "": return ""
        #setting up variables needed.
        key = password
        if password == None: key = self.key #If password is None, use the password saved in the class.

        if toText: return ASCII_char(self.basicDecrypt(self.disjoin(self.unshuffle(data)), key)) #if toText is True, the function wil return decrypted text and translate it back to characters.
        return self.basicDecrypt(self.disjoin(self.unshuffle(data)), key)#If not, will return list of ASCII numbers


    def basicEncrypt(self, data, password=None): #does the 1/3 part of the encryption process.
        #setting up variables needed.
        key = password
        if password == None: key = self.key #If password is None, use the password saved in the class.

        m = self.m
        if password != self.key: m = modifier(key)
        current = 0 #current refers to the current item in the key list being applied to the encryption process.
        result = []
        data = ASCII(data)

        for x in data:
            random.seed(key[current]*m)#setting the seed
            result.append(pad(self.length, random.randint(self.minLength, self.maxLength)+x))#encrypted character to result

            if current >= (len(key)-1): current = 0 #changing the current item in the key list being applied to the encryption process.
            else: current +=1

            m += x*random.randint(0, 100000) #chaning the modifier

        return result


    def basicDecrypt(self, data, password=None): #does the 1/3 part of the decryption process.
        #setting up variables needed.
        key = password
        if password == None: key = self.key#If password is None, use the password saved in the class.

        m = self.m
        if password != self.key: m = modifier(key)
        current = 0 #current refers to the current item in the key list being applied to the encryption process.
        result = []

        for x in data:
            random.seed(key[current]*m)#setting the seed
            d = x-random.randint(self.minLength, self.maxLength)
            result.append(d)#encrypted character to result

            if current >= (len(key)-1): current = 0 #changing the current item in the key list being applied to the encryption process.
            else: current +=1

            m += d*random.randint(0, 100000) #chaning the modifier

        return result

    def combine(self, data):#combine the numbers from the encrypted data
        result = ""
        for x in data: #going through data list, converting it into string and joining it into single string to retrun
            result = result + str(x)
        return result

    def disjoin(self, data):#disjoin the list of data that was combined by the "combine" function
        l = self.length
        result = []

        while len(data) != 0: #going thorugh the data, converting it into intager and putting it in result list
            result.append(int(data[:l]))
            data = data[l:]
        return result

    def shuffle(self, data, password=None): #data should be a string
        #setting up variables needed.
        key = password
        if password == None: key = self.key#If password is None, use the password saved in the class.

        m = self.m
        if password != self.key: m = modifier(key)
        current = 0 #current refers to the current item in the key list being applied to the random.seed.
        result = []

        l = (len(data) - 1)

        #firist we split the data string into a list, so that every elemnt in the list is a single number
        for x in data:
            result.append(x)


        #And now we shuffle the list
        for x in range(6*len(data)):
            random.seed(key[current]*m)#setting the seed

            i1 = random.randint(0, l) #choosing the two indexes of the data list to swap
            i2 = i1
            while i2 == i1: i2 = random.randint(0, l) #this makes sure i2 is different from i1
            result[i1], result[i2] = result[i2], result[i1]

            current +=1
            if current >= (len(key)-1): current = 0 #changing the current item in the key list being applied to the encryption process.
            m += 1

        return "".join(result)

    def unshuffle(self, data, password=None): #data should be a string
        #setting up variables needed.
        key = password
        if password == None: key = self.key#If password is None, use the password saved in the class.
        m = self.m
        if password != self.key: m = modifier(key)
        current = 0 #current refers to the current item in the key list being applied to the random.seed.
        result = []
        actionsList = []
        l = (len(data) - 1)

        #firist we split the data string into a list, so that every elemnt in the list is a single number
        for x in data:
            result.append(x)

        #figure out list of swaps the shuffle dunctionn would have taken.
        for x in range(6*len(str(data))): 
            random.seed(key[current]*m)#setting the seed

            i1 = random.randint(0, l) #choosing the two indexes of the data list to swap
            i2 = i1
            while i2 == i1: i2 = random.randint(0, l) #this makes sure i2 is different from i1

            actionsList.append([i1,i2])
            current +=1
            if current >= (len(key)-1): current = 0 #changing the current item in the key list being applied to the encryption process.
            m += 1

        actionsList = list(reversed(actionsList))#Then reverse the list, so the swaps can be undone.


        #And now we unshuffle the list
        for x in range(6*len(str(data))):
            result[actionsList[x][0]], result[actionsList[x][1]] = result[actionsList[x][1]], result[actionsList[x][0]]

        return "".join(result)


if __name__ == "__main__":
    key = input("Please type out key: ")
    s = SABONK(key)
    text = input("Please type out text to encrypt: ")
    print("You have typed: " + text)
    e = s.Encrypt(text)
    print("Text Encrypted: "+ e)
    print("Text Decrypted: "+ str(s.Decrypt(e)))




Aucun commentaire:

Enregistrer un commentaire