In Java 7, I want to do Encryption and Decryption of a password by using
SHA-256 and AES-256.
I tried to use PBKDF2WithHmacSHA256
, but this is unsupported in Java7.
Do you have any ideas? Please tell me the Library information.
Thanks.
Example(Java8)
public class PasswordUtil { private static final String ALGORITHM = "PBKDF2WithHmacSHA256"; private static final int ITERATION_COUNT = 10000; private static final int KEY_LENGTH = 256; /** * * @param password * @param salt * @return */ public static String getSafetyPassword(String password, String salt) { char[] passCharAry = password.toCharArray(); byte[] hashedSalt = getHashedSalt(salt); PBEKeySpec keySpec = new PBEKeySpec(passCharAry, hashedSalt, ITERATION_COUNT, KEY_LENGTH); SecretKeyFactory skf; try { skf = SecretKeyFactory.getInstance(ALGORITHM); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } SecretKey secretKey; try { secretKey = skf.generateSecret(keySpec); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } byte[] passByteAry = secretKey.getEncoded(); StringBuilder sb = new StringBuilder(64); for (byte b : passByteAry) { sb.append(String.format("%02x", b & 0xff)); } return sb.toString(); } /** * * @param salt * @return */ private static byte[] getHashedSalt(String salt) { MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } messageDigest.update(salt.getBytes()); return messageDigest.digest(); } }