List of usage examples for javax.crypto.spec IvParameterSpec IvParameterSpec
public IvParameterSpec(byte[] iv)
iv
as the IV. From source file:org.grycap.gpf4med.security.FileEncryptionProvider.java
/** * Creates a new {@link FileEncryptionProvider} instance that provides encryption/decryption * capabilities based on the specified password. * @param password encryption/decryption password. * @return a new {@link FileEncryptionProvider} instance. * @throws Exception if an error occurs in the execution of the operation. *//*from ww w .ja v a 2 s . com*/ public static FileEncryptionProvider getInstance(final String password) throws Exception { checkArgument(StringUtils.isNotBlank(password), "Uninitialized or invalid password"); // generate key from password final byte[] salt = generateSalt(); LOGGER.trace("Generated salt: " + Hex.encodeHexString(salt)); final SecretKey secret = generateKey(password, salt); LOGGER.trace("Generated key: " + Hex.encodeHexString(secret.getEncoded())); // create encryption cipher - bouncycastle equivalent: Cipher.getInstance("AES/CBC/PKCS5Padding", "BC") final Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); encryptCipher.init(Cipher.ENCRYPT_MODE, secret); // initialization vector needed by the CBC mode final AlgorithmParameters params = encryptCipher.getParameters(); final byte[] initVector = params.getParameterSpec(IvParameterSpec.class).getIV(); // create decryption cipher - bouncycastle equivalent: Cipher.getInstance("AES/CBC/PKCS5Padding", "BC") final Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); decryptCipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(initVector)); LOGGER.trace(String.format("Encryption/decryption ciphers were created - %s", encryptCipher.getProvider().getInfo())); return new FileEncryptionProvider(encryptCipher, decryptCipher); }
From source file:org.mozilla.android.sync.crypto.Cryptographer.java
/** * Perform a decryption./*from w w w . j a va 2 s .c o m*/ * * @param info info bundle for decryption * @return decrypted byte[] * @throws CryptoException on error */ public static byte[] decrypt(CryptoInfo info) throws CryptoException { // Check HMAC. if (!verifyHMAC(info)) { throw new HMACVerificationException(); } Cipher cipher = getCipher(); try { byte[] encryptionKey = info.getKeys().getEncryptionKey(); SecretKeySpec spec = new SecretKeySpec(encryptionKey, KEY_ALGORITHM_SPEC); AlgorithmParameterSpec ivParamSpec = new IvParameterSpec(info.getIV()); cipher.init(Cipher.DECRYPT_MODE, spec, ivParamSpec); } catch (GeneralSecurityException ex) { throw new CryptoException(ex); } return commonCrypto(cipher, info.getMessage()); }
From source file:com.confighub.core.security.Encryption.java
private static String decryptShared(CipherTransformation ct, String encrypted, String secret) throws ConfigException { if (null == encrypted) return null; try {// w w w .j ava2 s . com Cipher cipher = Cipher.getInstance(ct.getName()); final int blockSize = cipher.getBlockSize(); SecretKeySpec sharedKey = new SecretKeySpec(getKeyAsBytes(secret, ct.getKeyLen()), ct.getAlgo()); if ("CBC".equals(ct.getMode())) cipher.init(Cipher.DECRYPT_MODE, sharedKey, new IvParameterSpec(getIV(blockSize))); else cipher.init(Cipher.DECRYPT_MODE, sharedKey); byte[] decrypted = cipher.doFinal(Base64.decodeBase64(encrypted)); return new String(decrypted, "UTF8"); } catch (Exception e) { throw new ConfigException(Error.Code.DECRYPTION_ERROR); } }
From source file:org.soulwing.credo.service.crypto.jca.JcaEncryptedPrivateKeyWrapper.java
/** * {@inheritDoc}// w w w. ja v a 2 s. c o m */ @Override public PrivateKey derive() { int delimiter = transform.indexOf('/'); if (delimiter == -1) { throw new IllegalArgumentException("illegal transform syntax: " + transform); } try { String algorithm = transform.substring(0, delimiter); Cipher cipher = Cipher.getInstance(transform); cipher.init(Cipher.UNWRAP_MODE, secretKey.derive(), new IvParameterSpec(iv)); return (PrivateKey) cipher.unwrap(cipherText, algorithm, Cipher.SECRET_KEY); } catch (NoSuchAlgorithmException ex) { throw new RuntimeException(ex); } catch (NoSuchPaddingException ex) { throw new RuntimeException(ex); } catch (InvalidAlgorithmParameterException ex) { throw new RuntimeException(ex); } catch (InvalidKeyException ex) { throw new RuntimeException(ex); } }
From source file:edu.cmu.sei.ams.cloudlet.impl.AESEncrypter.java
/** * Encrypts data and returns the encrypted string. * @param clear A byte array to encrypt. * @return An encrypted string./*from w w w. j av a 2 s.c om*/ * @throws EncryptionException */ public String encrypt(byte[] clear) throws EncryptionException { try { SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); byte[] b = new byte[16]; random.nextBytes(b); byte[] iv = b; //log.info("IV: " + String.valueOf(Hex.encodeHex(iv))); // TODO: change to CBC method with padding. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, this.skeySpec, new IvParameterSpec(iv)); byte[] encrypted = cipher.doFinal(clear); //log.info("Cipher Text: " + String.valueOf(Hex.encodeHex(encrypted))); String encryptedString = new String(Base64.encodeBase64(ivCipherConcat(iv, encrypted))); return encryptedString; } catch (Exception e) { throw new EncryptionException("Error encrypting information", e); } }
From source file:com.springcryptoutils.core.cipher.symmetric.Base64EncodedCiphererWithStaticKeyImpl.java
/** * A base64 encoded representation of the raw byte array containing the * initialization vector.//from w w w.j a v a 2 s. co m * * @param initializationVector the initialization vector */ public void setInitializationVector(String initializationVector) { this.initializationVectorSpec = new IvParameterSpec(Base64.decodeBase64(initializationVector)); }
From source file:org.toffi.domainmodel.encryptor.impl.AESEncryptorImpl.java
@Override public byte[] decrypt(byte[] data) { try {//www .j a va2 s .c o m // split message in iv - cipheredMessage int cipheredMessageLen = data.length - ivLen; byte[] iv = new byte[ivLen]; byte[] cipheredMessage = new byte[cipheredMessageLen]; System.arraycopy(data, 0, iv, 0, ivLen); System.arraycopy(data, ivLen, cipheredMessage, 0, cipheredMessageLen); // decrypt byte[] decryptedMessage = null; IvParameterSpec paramSpec = new IvParameterSpec(iv); synchronized (decCipher) { decCipher.init(Cipher.DECRYPT_MODE, key, paramSpec); decryptedMessage = decCipher.doFinal(cipheredMessage); } return decryptedMessage; } catch (Exception e) { LOG.error("Unable to decrypt input message", e); throw new EncryptorException("Decryption error!", e); } }
From source file:org.opendatakit.briefcase.util.CipherFactory.java
public Cipher getCipher(String context, String fieldName) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(instanceId.getBytes("UTF-8")); md.update(symmetricKey.getEncoded()); md.update(fieldName.getBytes("UTF-8")); byte[] messageDigest = md.digest(); for (int i = 0; i < IV_BYTE_LENGTH; ++i) { ivSeedArray[i] = messageDigest[(i % messageDigest.length)]; }//from www . j av a 2 s. c om IvParameterSpec baseIv = new IvParameterSpec(ivSeedArray); Cipher c = Cipher.getInstance(SYMMETRIC_ALGORITHM); c.init(Cipher.DECRYPT_MODE, symmetricKey, baseIv); return c; }
From source file:org.codice.ddf.configuration.migration.AbstractMigrationSupport.java
public static String decrypt(byte[] data, Path keyPath) throws IOException, DecoderException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException { String keyData = new String(readFileToByteArray(keyPath.toFile()), StandardCharsets.UTF_8); byte[] keyBytes = decodeHex(keyData.toCharArray()); SecretKey secretKey = new SecretKeySpec(keyBytes, MigrationZipConstants.KEY_ALGORITHM); Cipher cipher = Cipher.getInstance(MigrationZipConstants.CIPHER_ALGORITHM); IvParameterSpec iv = new IvParameterSpec(MigrationZipConstants.CIPHER_IV); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); String decryptedContent;//from ww w. j a v a 2 s . c o m try (InputStream is = new CipherInputStream(ByteSource.wrap(data).openStream(), cipher)) { decryptedContent = IOUtils.toString(is, StandardCharsets.UTF_8); } return decryptedContent; }
From source file:io.personium.core.model.file.DataCryptor.java
/** * Generate InputStream for decryption from Input and return it. * If encryptionType is NONE, it returns input as is. * @param input input data//from ww w . j ava2 s . com * @param encryptionType encryption type * @return InputStream for decryption */ public InputStream decode(InputStream input, String encryptionType) { if (ENCRYPTION_TYPE_AES.equals(encryptionType)) { try { Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING); cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv)); CipherInputStream decodedInputStream = new CipherInputStream(input, cipher); return decodedInputStream; } catch (Exception e) { throw new RuntimeException(e); } } else { return input; } }