CROSSINGTUD/Crypto-API-Rules

Model correct PBEKeySpec usage

Closed this issue · 0 comments

final byte[] salt = new byte[32];
SecureRandom.getInstanceStrong().nextBytes(salt);
final PBEKeySpec pbekeyspec = new PBEKeySpec(new char[] {'p','a','s','s','w','o','r','d'}, salt, 65000, 128);
final SecretKeyFactory secFac = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
final Cipher c = Cipher.getInstance("AES/GCM/PKCS5Padding");
SecretKey tmpKey = secFac.generateSecret(pbekeyspec);
byte[] keyMaterial = tmpKey.getEncoded();
final SecretKeySpec actKey = new SecretKeySpec(keyMaterial, "AES");
c.init(Cipher.ENCRYPT_MODE, actKey);
c.doFinal("TESTPLAIN".getBytes("UTF-8"));
c.getIV();