Python 3 PBKDF2HMACBackend default_backend()在Java中等效 [英] Python 3 PBKDF2HMACBackend default_backend() equivalent in java

查看:69
本文介绍了Python 3 PBKDF2HMACBackend default_backend()在Java中等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是将一些加密代码从python重写为java.我是Python新手.Python示例:

I have a task to rewrite some crypto code from python to java. I'm new in Python. Python sample:

import default_backend
backend = default_backend()    

PBKDF2HMAC(hashes.SHA256(), 32, salt, iterations, backend)

当我阅读此处时,后端"是PBKDF2HMACBackend的实例.

As I read here 'backend' is an instance of PBKDF2HMACBackend.

我用Java编写了下一个代码:

I wrote the next code in Java:

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
SecretKey tmp = factory.generateSecret(new PBEKeySpec(???.toCharArray(), salt, iterations, 256));

在PBEKeySpec的构造函数中,第一个参数是password,它等效于python代码中的后端".当我理解python代码时,它通过库定义为默认值.但是我应该如何在我的Java代码中定义此参数?我可以使用一些随机值或常数吗?还是存在一些等效的default_backend()?

In constructor of PBEKeySpec first parameter is password which is equivalent of 'backend' in python code. As I inderstand in python code it defined as default via library. But how I should define this parameter in my java code? Can I use some random value or constant? Or maybe some default_backend() equivalent exist?

更新:获取密钥的完整python代码:

UPDATED: full python code to get key:

PBKDF2HMAC(hashes.SHA256(), 32, salt, iterations, backend).derive("somePassword")

推荐答案

在Python代码中,密码是在 derive 方法中传递的,该方法的调用在发布的代码中此处:

In the Python code, the password is passed in the derive method whose call is missing in the posted code, here:

kdf = PBKDF2HMAC(...)
key = kdf.derive(b'MyPassword')

密码与后端不对应.后端提供了支持诸如加密,哈希等操作的方法,并在这里.

The password doesn't correspond to the backend. The backend provides methods to support operations like encryption, hashing etc. and is described in more detail, here.

还要注意,在Python代码中使用了SHA256,在Java代码中使用了SHA1.由于Python代码是参考,因此 PBKDF2WithHmacSHA256 必须在Java代码中应用.

Also note that in the Python code SHA256 is used and in the Java code SHA1. Since the Python code is the reference, PBKDF2WithHmacSHA256 must be applied in the Java code.

这篇关于Python 3 PBKDF2HMACBackend default_backend()在Java中等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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