我如何做一个简单的加密/解密程序? [英] How would I make a simple encryption/decryption program?

查看:537
本文介绍了我如何做一个简单的加密/解密程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道(正如问题所在)如何制作一个简单的加密和解密程序。

I'd like to know (just as the question says) how to make a simple encryption and decryption program.

letters =' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
encryption_code ='LFWOAYUISVKMNXPBDCRJTQEGHZ'

我希望后面的部分是字母表中的字母加密如果用户选择解密,我希望将其反转或恢复为字母表。

I'd like the latter section to be what the letters of the alphabet are encrypted to and if the user chooses the decrypt it, I'd like it to be reversed or reverted back into the alphabet form.

这是太复杂了,因为从我看过的例子中,他们似乎所做的就是将字母3或4的空格移动,并将其用作加密代码。

Is this too complicated because from the examples I've looked over, all they seem to do is shift the alphabet 3 or 4 spaces and use that as their encryption code.

推荐答案

使用两个对象进行映射,一个是从字母到encryption_code,反之亦然:

Use two dicts to do the mapping, one from letters to encryption_code and the reverse to decrypt:

letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
encryption_code = 'LFWOAYUISVKMNXPBDCRJTQEGHZ'

enc = dict(zip(letters,encryption_code))

dec = dict(zip(encryption_code, letters))


s = "HELLO WORLD"

encr = "".join([enc.get(ch, ch) for ch in s])
decr = "".join([dec.get(ch, ch) for ch in encr])

print(encr)
print(decr)

输出:

IAMMP EPCMO 
HELLO WORLD

使用您的方法,您的输入将必须为大写,并且用户被限制为AZ,字母被加密ted,如果你想允许其他人物只是添加映射到dicts。

Using your method your input will have to be uppercase and the user is restricted to A-Z for the letters to be encrypted, if you want to allow other characters just add the mapping to the dicts.

letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
encryption_code = 'LFWOAYUISVKMNXPBDCRJTQEGHZ'
letters += letters.lower()
encryption_code += encryption_code.lower()
enc = dict(zip(letters,encryption_code))

dec = dict(zip(encryption_code, letters))


s = "HELLO world"

encr = "".join([enc.get(ch, ch) for ch in s])
decr = "".join([dec.get(ch, ch) for ch in encr])

print(encr)
print(decr)

输出:

IAMMP epcmo
HELLO world

任何字符不是在信件中将在encr和decr中是相同的:

Any characters not in letters will be the same in encr and decr i.e:

 s = "HELLO world!#}%"
 IAMMP epcmo!#}% # encr
 HELLO world!#}% # decr

这篇关于我如何做一个简单的加密/解密程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆