如何使用Python加密数据?
就像数据是Hello, I am a coder!一样
如果密码是123AAVVC
然后如何将Hello, I am a coder加密为不可读字符(如0000 0000 0101 0101 )
并且只有在密码为"123AAVVC"的情况下才被解密到"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请帮助我编写这个prgram,我是Python编程新手。
提前谢谢。
发布于 2020-11-03 09:00:34
听起来你想要加密,因为你使用的是密码。
您可以使用一些简单的方法,如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如果您需要进一步简化上述代码,请发表评论
根据要求,一个没有任何包和很少代码行的密码。这几乎是最简单的加密,给你;
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*)https://stackoverflow.com/questions/64659457
复制相似问题