java密码加盐_如何使用Java中的BouncyCastle API对密码进行加密和加盐?

本文建议使用基于密码的密钥派生函数(如PKCS5S2ParametersGenerator)而非基础哈希函数来存储和验证密码。通过设置随机盐值、迭代次数,增加密码猜测的计算成本,从而提高系统的安全性。在保存新密码时,生成盐值并用指定迭代次数派生MAC参数;验证密码时,对比派生的MAC是否匹配已存储的值,以确认密码的有效性。
摘要由CSDN通过智能技术生成

小编典典

我建议为此使用基于密码的密钥派生函数,而不是基本的哈希函数。像这样:

// tuning parameters

// these sizes are relatively arbitrary

int seedBytes = 20;

int hashBytes = 20;

// increase iterations as high as your performance can tolerate

// since this increases computational cost of password guessing

// which should help security

int iterations = 1000;

// to save a new password:

SecureRandom rng = new SecureRandom();

byte[] salt = rng.generateSeed(seedBytes);

Pkcs5S2ParametersGenerator kdf = new Pkcs5S2ParametersGenerator();

kdf.init(passwordToSave.getBytes("UTF-8"), salt, iterations);

byte[] hash =

((KeyParameter) kdf.generateDerivedMacParameters(8*hashBytes)).getKey();

// now save salt and hash

// to check a password, given the known previous salt and hash:

kdf = new Pkcs5S2ParametersGenerator();

kdf.init(passwordToCheck.getBytes("UTF-8"), salt, iterations);

byte[] hashToCheck =

((KeyParameter) kdf.generateDerivedMacParameters(8*hashBytes)).getKey();

// if the bytes of hashToCheck don't match the bytes of hash

// that means the password is invalid

2020-10-20

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值