List of usage examples for javax.crypto SecretKey getEncoded
public byte[] getEncoded();
From source file:org.cryptomator.crypto.aes256.AesSivCipherUtil.java
static byte[] sivDecrypt(SecretKey aesKey, SecretKey macKey, byte[] plaintext, byte[]... additionalData) throws InvalidKeyException, DecryptFailedException { final byte[] aesKeyBytes = aesKey.getEncoded(); final byte[] macKeyBytes = macKey.getEncoded(); if (aesKeyBytes == null || macKeyBytes == null) { throw new IllegalArgumentException("Can't get bytes of given key."); }//from w ww . ja v a2 s . c o m try { return sivDecrypt(aesKeyBytes, macKeyBytes, plaintext, additionalData); } finally { Arrays.fill(aesKeyBytes, (byte) 0); Arrays.fill(macKeyBytes, (byte) 0); } }
From source file:com.zacwolf.commons.crypto.Crypter_AES.java
/** * @param keyfile/* ww w . ja v a 2s. com*/ * @param keysize * @param salter * @throws InvalidKeySpecException * @throws IOException * @throws NoSuchAlgorithmException */ public final static void generateNewKeyFile(final File keyfile, final int keysize, final SecureRandom salter) throws InvalidKeySpecException, IOException, NoSuchAlgorithmException { if (keysize > Cipher.getMaxAllowedKeyLength(mytype)) throw new InvalidKeySpecException( "You specified a key size not supported by your current cryptographic libraries, which currently have a max key size of " + Cipher.getMaxAllowedKeyLength(mytype) + " for " + mytype); final FileOutputStream fos = new FileOutputStream(keyfile); try { final KeyGenerator kgen = KeyGenerator.getInstance(mytype); kgen.init(keysize > mykeysizemax ? mykeysizemax : keysize, salter); // 192 and 256 bits may not be available final SecretKey sk = kgen.generateKey(); fos.write(sk.getEncoded()); } finally { fos.flush(); fos.close(); ; } }
From source file:org.openmrs.util.Security.java
/** * generate a new secret key; should only be called once in order to not invalidate all * encrypted data/*from w ww . j a v a2 s . c om*/ * * @return generated secret key byte array * @since 1.9 */ public static byte[] generateNewSecretKey() { // Get the KeyGenerator KeyGenerator kgen = null; try { kgen = KeyGenerator.getInstance(OpenmrsConstants.ENCRYPTION_KEY_SPEC); } catch (NoSuchAlgorithmException e) { throw new APIException("could.not.generate.cipher.key", null, e); } kgen.init(128); // 192 and 256 bits may not be available // Generate the secret key specs. SecretKey skey = kgen.generateKey(); return skey.getEncoded(); }
From source file:com.zacwolf.commons.crypto.Crypter_Blowfish.java
/** * //from w w w .j a v a2 s . co m * Create a new key with a custom keysize less-than equal to mykeysizemax, * specifying a custom salter * * @param keyfile * @param keysize * @param salter * @throws NoSuchAlgorithmException * @throws IOException * @throws InvalidKeySpecException */ public final static void generateNewKeyFile(final File keyfile, final int keysize, final SecureRandom salter) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException { if (keysize > Cipher.getMaxAllowedKeyLength(mytype)) throw new InvalidKeySpecException( "You specified a key size not supported by your current cryptographic libraries, which currently have a max key size of " + Cipher.getMaxAllowedKeyLength(mytype) + " for " + mytype); final FileOutputStream fos = new FileOutputStream(keyfile); try { final KeyGenerator kgen = KeyGenerator.getInstance(mytype); kgen.init(keysize > mykeysizemax ? mykeysizemax : keysize, salter); // 192 and 256 bits may not be available final SecretKey sk = kgen.generateKey(); fos.write(sk.getEncoded()); } finally { if (fos != null) { fos.flush(); fos.close(); } } }
From source file:com.joyent.manta.client.crypto.SecretKeyUtils.java
/** * Writes the specified key in X509 encoding to a stream. Note: This method * doesn't close the supplied stream./* w w w.j ava2s . c o m*/ * * @param key key to write * @param out output stream to write to * @throws IOException thrown when there is a problem writing the key */ public static void writeKey(final SecretKey key, final OutputStream out) throws IOException { Validate.notNull(key, "Key must not be null"); Validate.notNull(out, "OutputStream must not be null"); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key.getEncoded()); out.write(x509EncodedKeySpec.getEncoded()); }
From source file:edu.internet2.middleware.openid.message.encoding.EncodingUtils.java
/** * Encode a secret key. This can be used on DH shared secrets as well as MAC keys. * // w w w . j a v a 2 s . c om * @param key secret key to encode * @return encoded secret key */ public static String encodeSecretKey(SecretKey key) { return new String(Base64.encodeBase64(key.getEncoded())); }
From source file:org.entrystore.repository.security.Password.java
private static String hash(String password, byte[] salt) { if (password == null || password.length() == 0) { throw new IllegalArgumentException("Empty passwords are not supported"); }// ww w.j a v a2 s . c o m try { long before = new Date().getTime(); SecretKey key = secretKeyFactory .generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLen)); log.info("Password hashing took " + (new Date().getTime() - before) + " ms"); return Base64.encodeBase64String(key.getEncoded()); } catch (GeneralSecurityException gse) { log.error(gse.getMessage()); } return null; }
From source file:io.github.goadinggoat.BungeeQuarantine.Commands.AcceptRules.java
private static String hash(String password, byte[] salt) { SecretKeyFactory f;//from ww w . ja v a 2 s.c om SecretKey key; try { f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512"); key = f.generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLen)); return Base64.encodeBase64String(key.getEncoded()); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeySpecException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:org.paxml.util.CryptoUtils.java
private static String getKey(KeyStore keyStore, String keyName, String keyPassword) { if (StringUtils.isBlank(keyName)) { keyName = DEFAULT_KEY_NAME;//from w w w . ja v a 2 s . co m } if (keyPassword == null) { keyPassword = DEFAULT_KEY_PASSWORD; } PasswordProtection _keyPassword = new PasswordProtection(keyPassword.toCharArray()); KeyStore.Entry entry; try { if (!keyStore.containsAlias(keyName)) { return null; } entry = keyStore.getEntry(keyName, _keyPassword); } catch (Exception e) { throw new PaxmlRuntimeException(e); } SecretKey key = ((KeyStore.SecretKeyEntry) entry).getSecretKey(); try { return new String(key.getEncoded(), KEY_VALUE_ENCODING); } catch (UnsupportedEncodingException e) { throw new PaxmlRuntimeException(e); } }
From source file:net.jmhertlein.core.crypto.Keys.java
private static byte[] getKeyBytes(SecretKey k) { byte[] key = k.getEncoded(); byte[] keyBytes = new byte[16]; System.arraycopy(key, 0, keyBytes, 0, Math.min(key.length, keyBytes.length)); return keyBytes; }