【问题标题】:How to encrypt data in Python?如何在 Python 中加密数据?
【发布时间】:2020-11-04 17:32:23
【问题描述】:

如何使用 Python 加密数据?
就像数据是Hello, I am a coder!
如果密码是123AAVVC
那么如何将Hello, I am a coder加密成0000 0000 0101 0101等不可读的字符
并且只有在密码为"123AAVVC"时才能解密为Hello, I am a coder

这是我的代码:

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

请帮我编写这个 prgram,我是 Python 编程新手
提前致谢。

【问题讨论】:

  • 我想你的问题在这里得到了回答:stackoverflow.com/questions/3815656/…问候,Y
  • 看起来# Rest of the code 实际上是此问题的所有代码。到目前为止,您尝试过什么吗?您是否在寻找可以使用的加密算法(encoding 的含义与您所描述的不同)?您是否正在寻找加密的特定功能,例如加密速度还是解密难度?
  • 我正在寻找一种如何使用密码加密数据以及通过输入密码来解密加密数据的方法。就像使用 replace() 函数将“hello”替换为“0001 0010 1010”一样,所以“0001 0010 1010”是把这个“0001 0010 1010”解密为“hello”的密钥(密码)。
  • 嗯,加密数据的方法有很多。您选择合适的加密算法的标准是什么? “加密数据”0000 0000 0101 0101 只是虚构的结果,并不是真正想要的结果,是吗?
  • 我认为AES是一种合适的加密算法。

标签: python python-3.x


【解决方案1】:

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

你可以使用像 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 分钟):

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*)

【讨论】:

  • 有没有办法在不使用任何 Python 包的情况下加密数据?
  • 是的。但是发明一种新的加密或实施现有的加密并不像看起来那么容易。为什么不采用现有的解决方案?
  • @Mr.LegalHacker 我可以发布我自己的加密版本,稍后才使用代码,它在不可破解性方面很糟糕......但我可以 100% 告诉你,也不告诉你或你身边的任何人技能组合将无法打破它:D
  • @Mr.LegalHacker 你去吧,试着破解我隐藏的信息,告诉他们说什么:D
猜你喜欢
  • 1970-01-01
  • 2013-12-24
  • 2012-06-10
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多