这看起来是密码学的吗?

时间:2017-03-16 10:02:12

标签: python-2.7 cryptography

我正在python 2.7中创建加密/解密设备。最终目标是允许使用此加密在2个节点之间进行加密通信。我真的只是想知道它是否会被轻易解密。我正在使用system.random这个,所以生成的数字的随机性应该非常好。但实际的算法不是太简单吗?

它生成大量0到255之间的随机整数,这是密钥流。然后将每个字符表示为0到255之间的整数。消息中的第一个字符被添加到第一个整数,第二个字符被添加到第二个等等。这应该使加密消息随着密钥流整数呈现为随机。我只是想我会解释它是如何工作的,而不仅仅是发布脚本。

def createkey():
    file = raw_input('file name to save key under:')
    maxcount = input("digits? Default is 2500.")
    if maxcount == '':
        maxcount = 2500

    digit = 0
    while digit < maxcount:
        rng=random.SystemRandom()
        key=rng.randint(100,356)
        print key
        with open(file, "a+") as myfile:
            myfile.write(str(key))
        digit = digit + 1
    myfile.close()


def encrypt():
    file_to_encrypt = raw_input('file name or path to encrypt:')

    destruct = raw_input('self destruct?')

    with open(file_to_encrypt, 'a+') as file:
        if destruct == 'y':
            file.write('\nThis message will self destruct.')

    with open(file_to_encrypt) as file:
        length = file.read()
        length = len(length)
        print length

    file_to_write = raw_input('Desired filename for encrypted info:')

    keyfile=raw_input('Key file:')

    num_chars = len(keyfile)

    count = 1
    paswd = raw_input('Enter password with which to encrypt file.')
    passum = 0
    charcount = 0
    counter = 0

    for letter in paswd:
        paschar = ord(letter)
        passum = passum + paschar
        charcount = charcount + 1
    if passum != 0:
        passum = int(math.sqrt((2 * passum) / charcount))
    else:
        pass

    with open(file_to_encrypt, 'r') as file:
        with open(keyfile) as key:
            while counter != length:
                txt = file.read(1)
                charnum = ord(txt)

                lines = key.read(3)
                x = lines
                x = int(x)
                x = x - 100
                keynum = (x)

                keynum = keynum + passum
                enmes = int(charnum) + (keynum)

                if enmes >= 256:
                    enmes = enmes - 256

                enmes = chr(enmes)
                print enmes

                enfile = open(file_to_write, 'a+')
                enfile.write(enmes)
                count = count + 1
                if count == num_chars:
                    count = 1
                counter = counter + 1

1 个答案:

答案 0 :(得分:0)

您设计了一个Stream Cipher。已有经过测试且安全的Stream Cyphers,有关示例,请参阅eSTREAM

Stream Cipher的安全性取决于密钥流生成的安全性。除非你是加密专家,否则你的密钥流生成器几乎肯定是不安全的。您似乎正在使用标准系统RNG。这样的RNG 安全。至少使用您的系统提供的安全RNG。

你可能想看一下现在已经过时的RC4,但这是一个简单的Stream Cipher示例。除非他为国家安全局工作,否则它会阻止你的小弟弟读你的文件。

相关问题