当前位置:首页 > 开发 > 编程语言 > 加密解密 > 正文

密码加密

发表于: 2012-07-12   作者:czpae86   来源:转载   浏览次数:
摘要:   import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import java.util.Arr

 

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();
		}
	}
 

 

密码加密

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
涉及到密码,当然加密技术很重要,下面我给大家演示一下密码加密问题数据库只会存加密后的文件,所
http://www.ciker.net/ciker/ciker.asp?ciker=338 摘要: 1 加Salt散列 2 ASP.NET 2.0 Membership中
公钥加密,私钥解密。 私钥加密,公钥解密。 私钥签名,公钥验签。 一、公钥密码算法误区 误解一、
              密码数据MD5加密 一、程序说明   1.数据库 MySQL   2.JQuery表单
产品安全: 安全算法:DES算法,3DES算法 采用最高安全智能卡专用安全微处理器作为硬件平台 芯片内
为了用户安全,添加密码加密 Client/scripts/GUI/Admin/PasswordRecover.js var hash = CryptoJS.Hma
1:进入cmd 2:命令:mysql -u root mysql 回车 3:命令:update user set password=password('新密
原创地址:http://www.cnblogs.com/jfzhu/p/4023439.html 转载请注明出处 (一)为什么要用哈希函数
原创地址:http://www.cnblogs.com/jfzhu/p/4023439.html 转载请注明出处 (一)为什么要用哈希函数
在使用通用权限管理系统(吉日嘎拉)的单点登录功能时,对登录密码使用了RSA加密(非对称加密),有使
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号