[英]How do I encrypt and decrypt a string in python 3.7?
我發現了這個完全相同的問題。 但是 PyCrypto 不會同時安裝在 python 3.6.5 和 3.7.0 上。
所以,我實現了某種類似 Gronsfeld 的密碼。 我知道,這很糟糕,但我可以簡單地用密碼加密和解密字符串
def encrypt(string, password):
int_list = []
password_len = len(password)
for cnt, sym in enumerate(string):
password_sym = password[cnt % password_len]
int_list.append(ord(sym)-ord(password_sym))
return int_list
# got some list which contain mine key to Todoist api, yes, this can be bruteforced, but same as any other API key
>>> [-20, -20, -50, -14, -61, -54, 2, 0, 32, 27, -51, -21, -54, -53, 4, 3, 29, -14, -51, 29, -10, -6, 1, 4, 28,
29, -55, -17, -59, -42, 2, 50, -13, -14, -52, -15, -56, -59, -44, 4]
def decrypt(int_list, password):
output_string = ""
password_len = len(password)
for cnt, numb in enumerate(int_list):
password_sym = password[cnt % password_len]
output_string += chr(numb+ord(password_sym))
return output_string
那么,如何正確操作呢?
密碼學是一個積極開發的庫,提供密碼配方和原語。 它支持 Python 2.6-2.7、Python 3.3+ 和 PyPy。
安裝
$ pip install cryptography
使用高級對稱加密配方的示例代碼:
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")
plain_text = cipher_suite.decrypt(cipher_text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.