我需要在Python中加密一个小字符串。是否可以使用密钥加密字符串?有没有一种好的方法来实现这一点并仅使用Python库实现合理的加密级别?你能告诉我怎么做吗?
我对密码学的了解非常基础。
答案 0 :(得分:2)
看看py-bcrypt。也许它会满足您的需求。来自网站:
py-bcrypt是OpenBSD的Blowfish密码散列码的Python包装器,如Niels Provos和DavidMazières的“A Future-Adaptable Password Scheme”中所述
答案 1 :(得分:2)
KeyCzar有一个很好的界面,应该符合您的要求。从主页:
Keyczar是一个开源加密工具包,旨在让devlopers更容易,更安全地在其应用程序中使用加密技术。 Keyczar支持使用对称密钥和非对称密钥进行身份验证和加密
crypter = Crypter.Read("/path/to/your/keys")
ciphertext = crypter.Encrypt("Secret message")
答案 2 :(得分:2)
我通过使用我在ASPN上找到的lightweight XTEA library来解决这个问题。它不需要任何额外的Python库,并且在实现合理的加密级别时实现起来非常简单。
答案 3 :(得分:0)
我最近创建了一段代码,几乎可以说明你的意思。它需要一个代码字,例如'abc'gets the values(1,2,3),然后将它们添加到单词中的每个字母进行加密。因此,如果'abc'是代码字,'bcd'是要加密的文本。 (1 + 2 = 3 2 + 3 = 5和3 + 4 = 7)因此输出将是'ceg'
codeword = input('Enter codeword : ')
codeword = codeword.replace(" ", "")
encrypt = input('Enter text to encrypt : ')
encrypt = encrypt.replace(" ", "")
j = 0
for i in codeword:
print(chr(ord(encrypt[j])+ ord(codeword[j])-96))
j+=1
答案 4 :(得分:0)
这是我的代码:
from cryptography.fernet import Fernet
import pyperclip
print("For this program to work, please send the file named 'pwkey' and the encrypted code to the other user.")
key = Fernet.generate_key()
file = open('pwkey', 'wb')
file.write(key)
file.close()
print('File Generated')
original = input('Enter message>>>')
message = original.encode()
f = Fernet(key)
encrypted = f.encrypt(message)
encrypted = encrypted.decode("ascii")
print('Encrypted:', encrypted)
pyperclip.copy(encrypted)
print('Please tell the other user to input the encrypted code in the Decrypt program')
print('(Code copied to Clipboard)')
print("Note: Please delete the 'pwkey' file after sending it to the other user. It
is for one-time use only.")
解密
# Decrypt
from cryptography.fernet import Fernet
print("For this program to work, make sure you have the file named 'pwkey' in your Python folder and the encrypted "
"code.")
file = open('pwkey', 'rb')
key = file.read()
file.close()
print('Key Retrieved')
encrypted = input('Please input your encrypted code>>>')
encrypted = bytes(encrypted, 'utf-8')
f = Fernet(key)
decrypted = f.decrypt(encrypted)
decrypted = decrypted.decode()
print('Decrypted Message:')
print(decrypted)
print("Note: Please delete the 'pwkey' file after getting the decrypted message. It is for one-time use only.")
答案 5 :(得分:0)
我会使用易于使用的cryptocode
库(https://github.com/gdavid7/cryptocode)来做到这一点。
与其他库相比,加密和解密相对简单:
## Encrypting:
>>> import cryptocode
>>> myEncryptedMessage = cryptocode.encrypt("I like trains", "password123")
>>> print(myEncryptedMessage)
M+Wykmlub0z7FhEdmA==*PvAbXRNx0SiSDHHxLsKZ5w==*ihQM/fdkgrX3G+yOItyAUQ==*QFNDmuUP1ysgo01/P2MNpg==
## Decrypting:
>>> import cryptocode
>>> myDecryptedMessage = cryptocode.decrypt("M+Wykmlub0z7FhEdmA==*PvAbXRNx0SiSDHHxLsKZ5w==*ihQM/fdkgrX3G+yOItyAUQ==*QFNDmuUP1ysgo01/P2MNpg==", "password123")
>>> print(myDecryptedMessage)
I like trains
该库相对于其他库的唯一大缺点是,与其他加密库相比,它没有太多选项可以更改字符串。但是对于许多只需要抽象并且不需要配置自定义设置的人来说,它是完美的选择。