List of usage examples for java.security Key getAlgorithm
public String getAlgorithm();
From source file:org.tolven.security.password.PasswordHolder.java
private void loadSecretKey(File encryptedSecretKeyFile, char[] password) { try {/*w w w . j a va2 s .c o m*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileInputStream in = null; try { in = new FileInputStream(encryptedSecretKeyFile); byte[] bytes = new byte[1024]; int n = 0; while ((n = in.read(bytes)) != -1) { baos.write(bytes, 0, n); } } finally { if (in != null) { in.close(); } } byte[] encryptedSecretKey = Base64.decodeBase64(baos.toByteArray()); String alias = getKeyStore().aliases().nextElement(); Key key = getKeyStore().getKey(alias, password); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(Cipher.UNWRAP_MODE, key); secretKey = (SecretKey) cipher.unwrap(encryptedSecretKey, "DESede", Cipher.SECRET_KEY); } catch (Exception ex) { throw new RuntimeException("Could not load secret key from " + encryptedSecretKeyFile.getPath(), ex); } }
From source file:pt.lunacloud.services.storage.internal.crypto.EncryptionUtils.java
/** * Encrypts a symmetric key using the provided encryption materials and returns * it in raw byte array form./* w ww.ja v a 2s . c o m*/ */ public static byte[] getEncryptedSymmetricKey(SecretKey toBeEncrypted, EncryptionMaterials materials, Provider cryptoProvider) { Key keyToDoEncryption; if (materials.getKeyPair() != null) { // Do envelope encryption with public key from key pair keyToDoEncryption = materials.getKeyPair().getPublic(); } else { // Do envelope encryption with symmetric key keyToDoEncryption = materials.getSymmetricKey(); } try { Cipher cipher; byte[] toBeEncryptedBytes = toBeEncrypted.getEncoded(); if (cryptoProvider != null) { cipher = Cipher.getInstance(keyToDoEncryption.getAlgorithm(), cryptoProvider); } else { cipher = Cipher.getInstance(keyToDoEncryption.getAlgorithm()); // Use default JCE Provider } cipher.init(Cipher.ENCRYPT_MODE, keyToDoEncryption); return cipher.doFinal(toBeEncryptedBytes); } catch (Exception e) { throw new LunacloudClientException("Unable to encrypt symmetric key: " + e.getMessage(), e); } }
From source file:pt.lunacloud.services.storage.internal.crypto.EncryptionUtils.java
/** * Decrypts an encrypted symmetric key using the provided encryption materials and returns * it as a SecretKey object./*from w w w. j av a 2s .com*/ */ private static SecretKey getDecryptedSymmetricKey(byte[] encryptedSymmetricKeyBytes, EncryptionMaterials materials, Provider cryptoProvider) { Key keyToDoDecryption; if (materials.getKeyPair() != null) { // Do envelope decryption with private key from key pair keyToDoDecryption = materials.getKeyPair().getPrivate(); } else { // Do envelope decryption with symmetric key keyToDoDecryption = materials.getSymmetricKey(); } try { Cipher cipher; if (cryptoProvider != null) { cipher = Cipher.getInstance(keyToDoDecryption.getAlgorithm(), cryptoProvider); } else { cipher = Cipher.getInstance(keyToDoDecryption.getAlgorithm()); } cipher.init(Cipher.DECRYPT_MODE, keyToDoDecryption); byte[] decryptedSymmetricKeyBytes = cipher.doFinal(encryptedSymmetricKeyBytes); return new SecretKeySpec(decryptedSymmetricKeyBytes, JceEncryptionConstants.SYMMETRIC_KEY_ALGORITHM); } catch (Exception e) { throw new LunacloudClientException( "Unable to decrypt symmetric key from object metadata : " + e.getMessage(), e); } }
From source file:uk.gov.hmrc.mobiletokenproxy.aes.Decrypter.java
protected String decrypt(String data, Key key) { return decrypt(data, key, key.getAlgorithm()); }
From source file:uk.gov.hmrc.mobiletokenproxy.aes.Decrypter.java
protected byte[] decryptAsBytes(String data, Key key) { return decryptAsBytes(data, key, key.getAlgorithm()); }
From source file:uk.gov.hmrc.mobiletokenproxy.aes.Encrypter.java
protected String encrypt(byte[] data, Key key) { return encrypt(data, key, key.getAlgorithm()); }
From source file:uk.gov.hmrc.mobiletokenproxy.aes.Encrypter.java
protected String encrypt(String data, Key key) { return encrypt(data.getBytes(StandardCharsets.UTF_8), key, key.getAlgorithm()); }