import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
public class PasswordEncryptionService {
public boolean authenticate(String attemptedPassword,
byte[] encryptedPassword, byte[] salt)
throws NoSuchAlgorithmException, InvalidKeySpecException {
// Encrypt the clear-text password using the same salt that was used to
// encrypt the original password
byte[] encryptedAttemptedPassword = getEncryptedPassword(
attemptedPassword, salt);
// Authentication succeeds if encrypted password that the user entered
// is equal to the stored hash
return Arrays.equals(encryptedPassword, encryptedAttemptedPassword);
}
public byte[] getEncryptedPassword(String password, byte[] salt)
throws NoSuchAlgorithmException, InvalidKeySpecException {
// PBKDF2 with SHA-1 as the hashing algorithm. Note that the NIST
// specifically names SHA-1 as an acceptable hashing algorithm for
// PBKDF2
String algorithm = "PBKDF2WithHmacSHA1";
// SHA-1 generates 160 bit hashes, so that's what makes sense here
int derivedKeyLength = 160;
// Pick an iteration count that works for you. The NIST recommends at
// least 1,000 iterations:
// http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
// iOS 4.x reportedly uses 10,000:
// http://blog.crackpassword.com/2010/09/smartphone-forensics-cracking-blackberry-backup-passwords/
int iterations = 20000;
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations,
derivedKeyLength);
SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm);
return f.generateSecret(spec).getEncoded();
}
public byte[] generateSalt() throws NoSuchAlgorithmException {
// VERY important to use SecureRandom instead of just Random
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
// Generate a 8 byte (64 bit) salt as recommended by RSA PKCS5
byte[] salt = new byte[8];
random.nextBytes(salt);
return salt;
}
}
public static void main(String[] args) {
PasswordEncryptionService passwordEncryptionService = new PasswordEncryptionService();
String password = "testTest";
try {
//生成salt,可保存到数据库用于验证
byte[] salt = passwordEncryptionService.generateSalt();
//根据salt加密的密码,可保存到数据库用于验证
byte[] encryptedPassword = passwordEncryptionService.getEncryptedPassword(password, salt);
//验证密码,根据上面保存的salt,encryptedPassword进行密码验证
boolean bl = passwordEncryptionService.authenticate(password, encryptedPassword, salt);
System.out.println(bl);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
分享到:
相关推荐
SYD8801 密码加密 配对SYD8801 密码加密 配对SYD8801 密码加密 配对SYD8801 密码加密 配对SYD8801 密码加密 配对
SpringBoot(七)SpringBoot整合Druid实现数据库密码加密 SpringBoot(七)SpringBoot整合Druid实现数据库密码加密 SpringBoot(七)SpringBoot整合Druid实现数据库密码加密
proxool 数据库连接池用户名、密码加密处理,文件里面包含可直接使用加密的jar以及相关使用说明。
后台在通过shiro进行授权和认证,分为普通用户和管理员两种角色,普通用户只能访问商品,管理员可以访问商品和用户界面的基础上加入了密码加密。登录时利用了redis缓存。里面包含数据库文件希望能帮助到大家学习。
java中关于Springboot加密 md5加密,注册密码加密到数据库中的关键语句,md5加密语句
使用js对密码加密解密三种方式,包括md5、base64、sha1等主流加密方式。
内容概要:nacos1.1.4版本修改源码使用非对称加密算法RSA进行用户名和密码加密传输。 适用人群:需要适用nacos作为项目注册中心的相关人员、内网用户。 适用场景:linux或者windows系统,使用nacos作为注册中心,...
使用AES256加密技术实现tomcat7对连接池数据库密码加密解密,资源中包含加密小程序,小程序实现加密,tomcat中实现解密,方便客户自己修改数据库密码且是密文!支持多操作系统如:linux mac os 文件太多分成两部分请...
专门asp.net 密码加密实例。 后台数据库密码加密MD5
描述aix的密码加密机制及方法,留存待查,
1,易飞ERP用户密码加密 解密 清除工具 2,亲自测试可以使用
C++密码加密与解密C++密码加密与解密C++密码加密与解密C++密码加密与解密C++密码加密与解密C++密码加密与解密C++密码加密与解密C++密码加密与解密C++密码加密与解密C++密码加密与解密C++密码加密与解密C++密码加密与...
栅栏密码加密解密工具
集成jsencrypt实现密码加密传输方式.zip
用途:可以根据该密码加密器,把自己常用的密码加密后使用,防止被人根据自己的信息猜测出自己的密码; 备注:如需使用本软件,必须安装 FrameWork3.5 软件不可能是完美的,如果各位有什么好的建议,请将建议发送...
可以对登录用户的密码进行加密,起到保护密码安全。 之前上传的那个有点乱,换成了java源码打包的。
PTA_python3_jmu-python-凯撒密码加密算法(csdn)————程序
druid密码加密工具,根据明文算出密文,并且直接写入剪切板
java密码加密,解密方法,一个好例子帮助大家学习!
matlab开发-使用matlabguiguide进行凯撒密码加密和解密。使用MATLAB指南,该程序将使用凯撒密码加密和解密字母