【发布时间】:2021-02-15 02:13:33
【问题描述】:
我有一个用 python 编写的项目。我使用密码库来加密和解密数据。 我按照他们的tutorial 显示的方式进行操作。
这是我的python代码:
import base64
import os
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
password = b"my password"
salt = os.urandom(16)
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend())
key = base64.urlsafe_b64encode(kdf.derive(password))
f = Fernet(key)
data = b"my data..."
token = f.encrypt(data)
然后我可以使用解密:
f.decrypt(token)
一切都在 python 中完美运行,但现在我需要在 kotlin 中做同样的事情。我发现了 fernet java-8 库,但我不知道如何以同样的方式使用它。
问题是我有两个工具:一个是用 python 编写的,另一个是我想用 kotlin 编写的。这两种工具都是为了做同样的事情——python 一个用于桌面,而 kotlin 一个将是一个 android 应用程序。因此,它们的加密相同非常重要,以便在 python(桌面工具)中加密的文件可以在 kotlin(android 应用程序)中解密,反之亦然。
但我不知道如何编写类似的 kotlin 代码。
您会看到有一个名为PBKDF2HMAC 的函数(或类),还有base64.urlsafe_b64encode 等。而且我不知道 kotlin 或 fernet java-8 中的类似函数是什么。
那我该怎么做呢?假设在 kotlin 中我必须使用我在 python 中使用的密码和盐。
谢谢!
【问题讨论】:
标签: python android kotlin encryption fernet