使用Python Fernet生成自己的密钥 [英] Generating own key with Python Fernet

查看:73
本文介绍了使用Python Fernet生成自己的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from cryptography.fernet import Fernet

import base64
# Put this somewhere safe!
key = Fernet.generate_key()

f = Fernet()
token = f.encrypt(b"A really secret message. Not for prying eyes.")
token
print f.decrypt(token)

如何生成自己的密钥而不是 fernet.genrate_key()?

How can I generate my own key instead of fernet.genrate_key()?

推荐答案

在Fernet中,可以使用Fernet的

In fernet a key can be generated using one of fernet's Key Derivation Functions

fernet提供的功能之一是基于密码的密钥派生功能2".可以在与Fernet一起使用密码中找到使用PBKDF2HMAC的示例.这在 pyca/cryptography的git问题#1333 中进行了讨论,maebert指出该示例使用salt = os.urandom(16),并且每次使用不同的salt值构造kdf类时,都会从密码生成一个新密钥.

One of the functions provided by fernet is the 'Password Based Key Derivation Function 2'. An example that uses PBKDF2HMAC can be found at Using Passwords with Fernet. This is discussed in git issue #1333 of pyca/cryptography, maebert points out that the example uses salt=os.urandom(16) and will generate a new key from a password each time the kdf class is constructed with a different salt value.

如果您需要使用自定义密钥派生功能,请查看 kdf pbkdf2 具有实现KeyDerivationFunction接口的类的示例.

If you need to use a custom key derivation function look at source code for kdf and pbkdf2 to have an example of a class that implements the KeyDerivationFunction interface.

匹配其签名并实现该接口的类应该能够作为自定义键派生函数插入.

A class that matches its signature and implements the interface should be able to be dropped in as a custom key derivation function.

这篇关于使用Python Fernet生成自己的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆