Python-ASCII加密程序

2024-09-29 17:18:05 发布

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

再见

我创建了一个简单的ASCII加密程序,我有3个问题:

  1. 我如何检查输入的密钥是否不正确,并告诉我的程序在输入错误时不要试图解密。在
  2. 为什么加密文本比原始文本长?在
  3. 如果我想加密其他东西而不是ASCII文本有多难?在

谢谢,下面是我的代码和结果:

import time key = "This■isôthe╝key¦b££glkHPAfgbm(*&%$$*(■ô▀" string = "Encryption the hell out of me, even if I repeatttttttt lettersssss you can't tell" entext = "" detext = "" keycnt=0 print("Displaing Text to be Encrypted") time.sleep(1) print(string) time.sleep(5) #Loops through the string and the key and adds the ascii values together to create a Encrypted character for sLetter in string: entext += chr(ord(sLetter) + ord(key[keycnt])) keycnt += 1 if keycnt == len(key): keycnt =0 print("Displaying encrypted Text") time.sleep(1) print(entext) #Resetting key position keycnt=0 #Loops through the string and the key and subtracts the ascii values together to create a decrypted character for sLetter in entext: detext += chr(ord(sLetter) - ord(key[keycnt])) keycnt += 1 if keycnt == len(key): keycnt =0 time.sleep(2) print("Displaying decrypted Text") time.sleep(1) print(detext) time.sleep(1)


Tags: andthekeytext文本stringiftime
1条回答
网友
1楼 · 发布于 2024-09-29 17:18:05

首先,加上字符的密钥不是一个好的密码,甚至不是一个好的凯撒或维根埃密码。为此,您需要模加法:对于普通字母表,要么是模数26(但是没有大小写和其他字符),要么是对于字节的模数256。在字节的情况下,密钥的每个字节都需要一个随机值。在

目前你的密文有一个偏差:如果你用0x00加上一个键值为0x00的字符值,那么你将得到0x00作为密文字节。问题是0x00值只有在加密方案中的特定组合中才会达到。因此,如果您看到值0x00,那么您将立即知道密钥和明文值。在

How could I check if the key in entered incorrectly and tell my program not to attempt to decrypt if it has been entered incorrectly.

无法检查键的值是否正确。您唯一能做的就是验证输出是否符合您的期望。在

现代密码学使用消息认证码(MAC)来创建认证标签。这个标签可以根据密文和密钥(或者,对于不太安全的方案,明文和密钥)进行验证。也有认证的加密模式,如GCM,基本上是内置MAC认证的密码。在

Why is the encrypted text longer than the original?

如果您添加的值为255或更低,则将得到510或更低的值。但是这些值至少需要两个字节来编码。在

If I wanted to encrypt other things not ASCII text how hard would it be?

不难:只要用一个真正随机的密钥执行异或模加法(例如8位/一字节的模256)。但是,要创建任何安全的东西,您可以使用一次性密码(其中密钥与二进制明文大小相同)或现代密码。在

相关问题 更多 >

    热门问题