如何使用我自己的criptgraphy制作python加密和解密程序?

时间:2018-05-05 18:47:19

标签: python encryption

criptogrphy看起来像这样:

a = "v07u4tLKinfa"
b = "zz45tTn8u4o0"
c = "W46tEr10d01z"
d = "5aPSCr36quA7"
e = "Zg6pPrg17t15"
f = "Po84cr3Ud25M"
g = "L0l1crXtOO11"
h = "7G1CaZ2wBg9f"
i = "Z1Yd84we71y3"
j = "LL6TRe0yR4tj"
k = "kt426ug76Vz3"
l = "26IzOZyrXCt4"
m = "Z3uj09HJ6Iou"
n = "0d2Wna13lERT"
o = "N935efQef3JH"
p = "7j43Ghnf6R4o"
q = "dF13Rw9nX6i8"
r = "DOu3fw073r81"
s = "3C0w23rTg5Ij"
t = "TE436nyWi5ee"
u = "58Ybt78n3e06"
v = "39j8rH9TYrcO"
w = "0NAtO48veQM7"
x = "K2uC2tgEVBn4"
y = "9nRm03iH8ECn"
z = "8brYHI304ld2"

我希望程序通过更改每个字母来加密纯文本,如(“”)。

例如: (“”)更改为(“ TE436nyWi5ee7G1CaZ2wBg9fZ1Yd84we71y33C0w23rTg5Ij ”)

注意: 我还希望文本是用户输入。

1 个答案:

答案 0 :(得分:1)

首先,将映射放在字典中:

cipher = {
    'a': "v07u4tLKinfa",
    'b': "zz45tTn8u4o0",
    'c': "W46tEr10d01z",
    'd': "5aPSCr36quA7",

    'e': "Zg6pPrg17t15",
    'f': "Po84cr3Ud25M",
    'g': "L0l1crXtOO11",
    'h': "7G1CaZ2wBg9f",

    'i': "Z1Yd84we71y3",
    'j': "LL6TRe0yR4tj",
    'k': "kt426ug76Vz3",
    'l': "26IzOZyrXCt4",

    'm': "Z3uj09HJ6Iou",
    'n': "0d2Wna13lERT",
    'o': "N935efQef3JH",
    'p': "7j43Ghnf6R4o",

    'q': "dF13Rw9nX6i8",
    'r': "DOu3fw073r81",
    's': "3C0w23rTg5Ij",
    't': "TE436nyWi5ee",

    'u': "58Ybt78n3e06",
    'v': "39j8rH9TYrcO",
    'w': "0NAtO48veQM7",
    'x': "K2uC2tgEVBn4",

    'y': "9nRm03iH8ECn",
    'z': "8brYHI304ld2"
}

然后你可以用这种方式“加密”一个字符串:

>>> s = 'this'
>>> ' '.join(cipher[c] for c in s)
'TE436nyWi5ee 7G1CaZ2wBg9f Z1Yd84we71y3 3C0w23rTg5Ij'

为了解密,构建cipher

的逆映射
reverse_cipher = {v: k for k, v in cipher.items()}
以这种方式

和“解密”:

>>> sc = 'TE436nyWi5ee 7G1CaZ2wBg9f Z1Yd84we71y3 3C0w23rTg5Ij'
>>> ''.join(reverse_cipher[x] for x in sc.split())
'this'

完成此学习实验后,如果您确实要加密never do it by yourself,请使用现有的库。

这个substitution cipher很容易被打破。