我发明了一种数据加密方法。我想知道它是否安全



我不能100%确定这是否是问这个问题的最佳地点。如果是的话,你能给我推荐一个更好的网站吗?

我用python 3编写了一个程序来加密数据。但它使用了我制作的一种全新的数据加密方法。我想知道你们中是否有人认为它足够安全,可以用于任何实际用途或者,如果你看到它有任何缺陷的话。

程序的基本要点是:

该程序将数据的每个字符转换为ASCII数字。然后,为了加密它,它"随机"在每个字符的原始ASCII数字上添加一个大数字。

但它不是随机的,因为种子是使用random.seed()函数设置的。程序设置的种子由键决定。

然后它打乱每个数字。

由于没有更好的名称,我将这种方法称为SABONK。它不代表任何东西。


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)))

我想知道你们中是否有人认为它足够安全,可以用于任何实际用途。

总之,不。需要多年的数学和密码学研究才能远程了解现代密码是如何设计的,以及数学家在多年的研究中发现的令人难以置信的细微缺陷。

斯坦福大学的在线密码学I课程很好地介绍了一些概念,但你至少需要大学水平的数学技能。Cryptopals加密挑战也可以教你一些关于现代密码学的知识,但要注意,这些挑战会变得非常困难。

虽然娱乐性地想出密码可能很有趣,但请不要用你的密码来保护任何实际敏感的东西,也不要向他人歪曲你的密码的安全性。

像r/code这样的网站是你可以发布这样的"业余密码"的地方。西蒙·辛格的《密码本》一书很好地讲述了不同的历史密码以及它们是如何被破解的。


您是否考虑过以下内容:

  • basicEncrypt生成最多为6位的数字,然后将它们填充到7位。这可能会导致一些问题,例如使您的混洗算法严重偏向于混洗填充数字。

  • data的长度影响给定数字被交换或不被交换的概率。

  • 如果在相同大小的数据上使用相同的键两次,它将生成相同的混洗模式。

  • 你严重依赖random.seed(),你知道它到底在做什么吗?用于安全目的是否安全?

最新更新