自定义加密有多安全?

2024-10-01 13:45:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我花了几个星期的时间开发了两种加密算法,不,我没有用它来加密任何敏感数据,我只想知道如果要使用这些算法,它们有多安全。(在说自定义加密不安全之前,请先查看代码)

加密算法一:

import random
def encrypt(plainText, password):
  plaintextList = []
  passwordSeed = ''
  key = 0
  encryptedText = ''
  count = 0
  for i in plainText:
    plaintextList.insert(count, str(ord(i)))
    count += 1
  for i in password:
    passwordSeed += str(ord(i))
  for i in range(10000):
    random.seed(int(passwordSeed) + (i*1000))
    key += random.randint(-1000000000, 1000000000)
  for i in plaintextList:
    encryptedText += str(int(i)*abs(key)) + '.'
  return encryptedText


def decrypt(hashText, password):
  splitHashText = []
  passwordSeed = ''
  key = 0
  decryptedText = ''
  splitHashText = hashText.split('.')
  for i in password:
    passwordSeed += str(ord(i))
  for i in range(10000):
    random.seed(int(passwordSeed) + (i*1000))
    key += random.randint(-1000000000, 1000000000)
  for i in splitHashText:
    if i.isdigit():
      decryptedText += chr(int(int(i)/abs(key)))
  return decryptedText

while 1 == 1:
  if input('Type e then enter to encrypt and d then enter to decrypt: ') == 'e':
    print(encrypt(input('Encrypt: '),input('Password: ')))
  else:
    print(decrypt(input('Decrypt: '),input('Password: ')))

加密算法二:

def encrypt(plainText):
  plainList = []
  count = 0
  total = 0
  key = ''
  for i in plainText:
    plainList.insert(count,ord(i))
    total += int(ord(i))
    count += 1
  for i in plainList:
    key += str(i/total) + ';'
  return total, key
def decrypt(hashedText, key):
  keys = key.split(';')
  letters = []
  count = 0
  for i in keys:
    if i != '':
      letters.insert(count, chr(int(float(hashedText)*float(i))))
    count += 1
  return ''.join(letters)
while 1 == 1:
  if input('Type e to encrypt and d to decrypt: ') == 'e':
    data = encrypt(input('String to encrypt: '))
    print('Hashed text: '+ str(data[0]) + '   Key: ' + str(data[1]))
  else:
    print(decrypt(input("Hash: "), input("Key: ")))

如果您想测试代码,可以在任何pythonide中运行它。如果这个加密是安全的,那么请随意使用代码。你知道吗


Tags: tokeyinforinputdefcountrandom
3条回答

让我从Bruce Schneierblog post开始:

One of the most singular characteristics of the art of deciphering is the strong conviction possessed by every person, even moderately acquainted with it, that he is able to construct a cipher which nobody else can decipher.

所以如果你不得不问,那么我敢说这意味着你对密码学的知识是有限的,不幸的是,这也意味着你的代码从一开始就有很大的缺陷,特别是在考虑到安全性的情况下。你知道吗

just want to know how secure these algorithms are If they were to be used

快速看一下发布的代码,我会说它充其量就像Caesar cipher一样弱。虽然实验和学习没有什么错(很明显),但是您不应该将代码用于比这更重要的事情。该领域的专家设计了强大的密码算法,并由该领域的其他专家验证。有各种形式的现成实现。您的代码看起来像是试图重新发明轮子,但实际上并不知道正确的轮子应该是什么样子。一个人可以从一辆方轮自行车上摔下来。。。你知道吗

最后,让我推荐Code Book by Simon Singh——这本书将帮助你认识到领域密码学有多么复杂,你还有多少东西要学——这本书是为非专家而写的,所以你实际上应该从中受益,学到很多东西。真的向任何人推荐阅读(也向非技术人员推荐)。你知道吗

附言:还有https://crypto.stackexchange.com/

Please look at the code before saying custom encryption is not safe

自定义加密几乎从来都不安全;有充分的理由表明,到目前为止,对于这类问题,答案是最常见的答案。看起来安全的东西通常在算法、实现中都有可利用的属性,或者仅仅因为它们易于暴力攻击。你知道吗

你真正面临的最大问题是,世界上很少有人有资格评估一个加密算法是否真的安全,我怀疑那些人不会这么做。你知道吗

虽然我自己过去常常修补加密,但最终你必须问自己是否真的相信自己的想法。问题在于

Please look at the code before saying custom encryption is not safe

burden of proof在你身上。你必须证明你的加密是安全的。你知道吗

例如,RSA has such proofs。你知道吗

所以不。你的加密是不安全的,因为你没有证明它。但我不想阻止你尝试!我和那样的事情混在一起很开心。你知道吗

相关问题 更多 >