赞
踩
用PBKDF加密密码
- public class EncrptyUtil
- {
- public static String encrptyPB2(String password)
- {
- try
- {
- int iterationCount = 1000;
-
- int keyLength = 64*8;
-
- char[] charPwd = password.toCharArray();
-
- byte[] salt = getSalt().getBytes();
- for (byte b : salt)
- {
- System.out.print(b);
- }
- System.out.println();
- System.out.println("------------------------");
-
- PBEKeySpec keySpec = new PBEKeySpec(charPwd, salt, iterationCount, keyLength);
-
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
- byte[] pbePwd = keyFactory.generateSecret(keySpec).getEncoded();
-
- for (byte b : pbePwd)
- {
- System.out.print(b);
- }
- System.out.println();
- System.out.println("------------------------");
- System.out.println(salt.toString()+pbePwd.toString());
-
- return salt.toString()+pbePwd.toString();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return null;
- }
-
- private static String getSalt()
- {
- try
- {
- byte[] bytes = new byte[16];
- SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
- random.nextBytes(bytes);
- System.out.println(random.toString());
- return random.toString();
- }
- catch (NoSuchAlgorithmException e)
- {
- e.printStackTrace();
- }
- return null;
- }
-
- @Test
- public void testPB2()
- {
- String password="ghrte";
- EncrptyUtil.encrptyPB2(password);
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。