List of usage examples for javax.crypto SecretKey getEncoded
public byte[] getEncoded();
From source file:es.logongas.fpempresa.security.SecureKeyGenerator.java
public static String getSecureKey() { try {//from w w w . j av a 2 s . c o m Base32 base32 = new Base32(); KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(keyLength); SecretKey secretKey = keyGen.generateKey(); byte[] encoded = secretKey.getEncoded(); return base32.encodeAsString(encoded); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:Main.java
private static Mac getMacForPassphrase(String passphrase, byte[] salt) throws GeneralSecurityException { SecretKey key = getKeyFromPassphrase(passphrase, salt); byte[] pbkdf2 = key.getEncoded(); SecretKeySpec hmacKey = new SecretKeySpec(pbkdf2, "HmacSHA1"); Mac hmac = Mac.getInstance("HmacSHA1"); hmac.init(hmacKey);/*from w ww.jav a 2 s . c o m*/ return hmac; }
From source file:fi.ilmoeuro.membertrack.util.Crypto.java
public static String hash(String candidate, String salt) { try {/*from w ww . ja v a 2 s . c o m*/ SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec ks = new PBEKeySpec(candidate.toCharArray(), salt.getBytes(StandardCharsets.US_ASCII), 1024, 128); SecretKey sk = skf.generateSecret(ks); Key k = new SecretKeySpec(sk.getEncoded(), "AES"); return Hex.encodeHexString(k.getEncoded()); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { throw new RuntimeException("Error while hashing", ex); } }
From source file:Main.java
private static Mac getMacForPassphrase(String passphrase, byte[] salt, int iterations) throws GeneralSecurityException { SecretKey key = getKeyFromPassphrase(passphrase, salt, iterations); byte[] pbkdf2 = key.getEncoded(); SecretKeySpec hmacKey = new SecretKeySpec(pbkdf2, "HmacSHA1"); Mac hmac = Mac.getInstance("HmacSHA1"); hmac.init(hmacKey);/*from www . j a va2 s . c o m*/ return hmac; }
From source file:Main.java
public static byte[] generateHash(char[] pass, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { PBEKeySpec keySpec = new PBEKeySpec(pass, salt, ITERATION_COUNT, 128); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_FACTORY); SecretKey key = keyFactory.generateSecret(keySpec); return key.getEncoded(); }
From source file:de.openflorian.crypt.CipherKeyGenerator.java
/** * Generate Cipher Secret<br/>//w ww .ja v a 2 s . co m * <br/> * Secret is generated by Blowfish {@link KeyGenerator} and a system default * {@link SecureRandom} provider and Base64 encoded afterwards. * * @return Base64 encoded {@link SecureRandom} generated encryption key * @throws GeneralSecurityException */ public static String generateKey() throws GeneralSecurityException { try { KeyGenerator gen = KeyGenerator.getInstance("Blowfish"); gen.init(192, new SecureRandom()); SecretKey key = gen.generateKey(); return new String(new Base64().encode(key.getEncoded())).trim(); } catch (Exception e) { log.error(e.getMessage(), e); throw new GeneralSecurityException(e.getMessage(), e); } }
From source file:com.healthcit.cacure.utils.PasswordService.java
private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(seed);/*w w w . j av a2 s .c om*/ kgen.init(128, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; }
From source file:models.logic.CipherDecipher.java
public static String keyToString(SecretKey key) { char[] hex = encodeHex(key.getEncoded()); return String.valueOf(hex); }
From source file:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java
public static byte[] encrypt(String input, String key, byte[] iv) throws Exception { javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator.getInstance("AES"); kgen.init(128, new SecureRandom(key.getBytes())); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec skey = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skey, new IvParameterSpec(INIT_VECTOR, 0, INIT_VECTOR.length)); return cipher.doFinal(input.getBytes()); }
From source file:net.foreworld.util.RestUtil.java
public static String genApiKey() { try {/*from www. ja v a2 s .c om*/ KeyGenerator generator = KeyGenerator.getInstance("HmacSHA1"); SecretKey key = generator.generateKey(); String encodedKey = Base64.encodeBase64URLSafeString(key.getEncoded()); return encodedKey; } catch (NoSuchAlgorithmException ignore) { } return null; }