Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中加密数据?_Python_Python 3.x - Fatal编程技术网

如何在Python中加密数据?

如何在Python中加密数据?,python,python-3.x,Python,Python 3.x,如何使用Python加密数据? 就像如果数据是你好,我是一个编码员 如果密码是123AAVVC 那么如何将你好,我是一名编码员加密为不可读的字符,如0000 0000 0101 0101 只有在密码为“123AAVVC”的情况下,才能解密到你好,我是一名编码员 这是我的密码: def InputPassword(): Msg = "Enter your password: " InputPassword = input(Msg) return Inpu

如何使用Python加密数据?
就像如果数据是
你好,我是一个编码员
如果密码是
123AAVVC

那么如何将
你好,我是一名编码员
加密为不可读的字符,如
0000 0000 0101 0101

只有在密码为“123AAVVC”的情况下,才能解密到
你好,我是一名编码员

这是我的密码:

def InputPassword():
    Msg = "Enter your password: "
    InputPassword = input(Msg)
    return InputPassword

def InputData():
    Msg = "Enter your data: "
    InputData = input(Msg)
    return InputData


def Encode():
    Psd = InputPassword()
    Data = InputData()
    LenPsd = len(Psd)
    if LenPsd >= 8: #This will allow user to enter password that has 8 or more character
        if Psd:
            pass
            #Rest of the code, I know till here.
        else:
            print("Error!")
    else:
        print("Passord is too short!")

if __name__ == '__main__':
    try:
        Encode()
    except:
        pass
请帮助我编写这个程序,我是Python编程新手

提前谢谢。

听起来您需要加密,因为您使用的是密码

您可以使用pycryptodome这样简单的工具来制作不可破解的密码:

def __EncryptionBase(text, password):
    iv = get_random_bytes(16)
    cipher = AES.new(password, AES.MODE_CBC, iv=IV)
    padded_data = pad(text.encode(), cipher.block_size)
    ciphertext = cipher.encrypt(padded_data)
    return IV + ciphertext

def __DecryptionBase(text, password, IV):
    cipher = AES.new(password, AES.MODE_CBC, iv=IV)
    paddedBytes = cipher.decrypt(text)
    originalBytes = unpad(paddedBytes, cipher.block_size)
    return originalBytes
如果您需要进一步简化上述代码,请发表评论


根据请求,密码没有任何包和几行代码。 这几乎是最简单的加密,给你;D

state = [None] * 256
p = q = None


def setKey(key):
    global p, q, state
    key = [ord(c) for c in key]
    state = [n for n in range(256)]
    p = q = j = 0
    for i in range(256):
        if len(key) > 0:
            j = (j + state[i] + key[i % len(key)]) % 256
        else:
            j = (j + state[i]) % 256
        state[i], state[j] = state[j], state[i]


def byteGenerator():
    global p, q, state
    p = (p + 1) % 256
    q = (q + state[p]) % 256
    state[p], state[q] = state[q], state[p]
    return state[(state[p] + state[q]) % 256]


def encrypt(inputString, password):
    setKey(password)

    a = [ord(p) ^ byteGenerator() for p in inputString]
    b = ""
    for i in a:
        b += str(i) + ","
    b = b[:-1]
    return b


def decrypt(YourCipheredNumericList, password):
    setKey(password)
    a = YourCipheredNumericList.split(",")
    if a[-1] == "":
        a.pop()
    b = []
    for i in a:
        b.append(int(i))
    return "".join([chr(c ^ byteGenerator()) for c in b])

a = encrypt("hello world", "123")
print(a)
b = decrypt(a, "123")
print(b)
这是给你的一个挑战。。。 使用上面的简单加密,我创建了两条隐藏的消息给你,尝试破解它们并告诉我我的秘密:D

容易打破(花费我不到2分钟):

非常困难(我个人无法破译此消息)


我想你的问题在这里得到了回答:您好,YIt似乎是
#其余的代码实际上是这个问题的所有代码。到目前为止你试过什么吗?您是否寻找可以使用的加密算法(编码意味着您描述的以外的内容)?您是否正在寻找加密的特定功能,例如加密速度或解密难度?我正在寻找一种方法,如何使用密码加密数据,以及如何通过输入密码来解密加密数据。就像使用replace()函数将“hello”替换为“0001 0010 1010”,所以“0001 0010 1010”是将“0001 0010 1010”解密为“hello”的密钥(密码)。加密数据的方法很多。您选择合适加密算法的标准是什么?“加密数据”
0000 0000 0101 0101
只是一个虚构的结果,而不是实际想要的结果,是吗?我认为AES是一种合适的加密算法。有没有一种方法可以在不使用任何Python包的情况下加密数据?是的。但是发明一种新的加密或实现一种现有的加密并不像看上去那么容易。为什么不采用现有的解决方案?@LegalHacker先生我可以发布我自己的加密版本,只在稍后使用代码,就不可破解性而言,它很糟糕。。。但我可以100%告诉你,你或你的任何人都无法打破它:D@Mr.LegalHacker好了,试着打破我隐藏的信息,告诉他们说什么:D
MyFirstSecretMessage = decrypt("66,28,211,12,44,178,181,41,254,43,244,148,110,52,56,175", *MySecretPassword*)
MySecondSecretMessage = decrypt("129,189,215,167,37,158,208,75,4,161,96,213", *MySecretPassword*)