Example usage for javax.crypto Cipher getInstance

List of usage examples for javax.crypto Cipher getInstance

Introduction

In this page you can find the example usage for javax.crypto Cipher getInstance.

Prototype

public static final Cipher getInstance(String transformation)
        throws NoSuchAlgorithmException, NoSuchPaddingException 

Source Link

Document

Returns a Cipher object that implements the specified transformation.

Usage

From source file:de.adorsys.morphiaencryption.AES256CryptoProvider.java

@Override
public byte[] decrypt(byte[] encData) {
    try {//w w w.  j  a v a2  s  .  c o m
        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, key, getIV());
        byte[] decrypted = cipher.doFinal(encData);
        return decrypted;
    } catch (BadPaddingException | IllegalBlockSizeException | InvalidKeyException
            | InvalidAlgorithmParameterException | NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new CryptException(e);
    }
}

From source file:com.demandware.vulnapp.challenge.impl.ECBOracleChallenge.java

protected ECBOracleChallenge(String name) {
    super(name);/* www . j av  a2s .co  m*/
    this.keyType = "AES";
    this.encryptionType = "AES/ECB/NoPadding";
    try {
        SecretKey key = generateKey();
        this.eCipher = Cipher.getInstance(this.encryptionType);
        this.eCipher.init(Cipher.ENCRYPT_MODE, key);
        this.dCipher = Cipher.getInstance(this.encryptionType);
        this.dCipher.init(Cipher.DECRYPT_MODE, key);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
        throw new SetupRuntimeException(e);
    }
    tryRoundtrip();
}

From source file:com.adaptris.security.password.AesCrypto.java

public String decode(String encrypted, String charset) throws PasswordException {

    String encryptedString = encrypted;
    String result;//from   w w  w. ja  va  2  s .com

    if (encrypted.startsWith(Password.PORTABLE_PASSWORD)) {
        encryptedString = encrypted.substring(Password.PORTABLE_PASSWORD.length());
    }
    try {
        Input input = new Input(encryptedString);
        input.read();
        SecretKey sessionKey = new SecretKeySpec(input.getSessionKey(), ALG);
        Cipher cipher = Cipher.getInstance(CIPHER);
        if (input.getSessionVector() != null) {
            IvParameterSpec spec = new IvParameterSpec(input.getSessionVector());
            cipher.init(Cipher.DECRYPT_MODE, sessionKey, spec);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, sessionKey);
        }
        byte[] decrypted = cipher.doFinal(input.getEncryptedData());
        result = unseed(decrypted, charset);
    } catch (Exception e) {
        throw new PasswordException(e);
    }
    return result;
}

From source file:club.jmint.crossing.specs.Security.java

public static String desEncrypt(String data, String key) throws CrossException {
    String ret = null;//from   w w w  .  j  a  v  a2 s  . c o m
    try {
        DESKeySpec desKey = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);

        Cipher cipher = Cipher.getInstance(CIPHER_DES_ALGORITHM);
        SecureRandom random = new SecureRandom();
        cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
        byte[] results = cipher.doFinal(data.getBytes("UTF-8"));
        ret = Base64.encodeBase64String(results);
    } catch (Exception e) {
        CrossLog.printStackTrace(e);
        throw new CrossException(ErrorCode.COMMON_ERR_ENCRYPTION.getCode(),
                ErrorCode.COMMON_ERR_ENCRYPTION.getInfo());
    }
    return ret;
}

From source file:net.bioclipse.encryption.Encrypter.java

public String encrypt(String plaintext) {
    SecretKeyFactory keyFac;//  ww  w  . j  a va2 s.  com
    SecretKey pbeKey;
    Cipher pbeCipher;
    try {
        keyFac = SecretKeyFactory.getInstance(METHOD);
        pbeKey = keyFac.generateSecret(pbeKeySpec);
        pbeCipher = Cipher.getInstance(METHOD);
        pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
        return new String(Base64.encodeBase64(pbeCipher.doFinal(plaintext.getBytes())));
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException(e);
    } catch (NoSuchPaddingException e) {
        throw new IllegalStateException(e);
    } catch (InvalidKeyException e) {
        throw new IllegalStateException(e);
    } catch (InvalidAlgorithmParameterException e) {
        throw new IllegalStateException(e);
    } catch (IllegalBlockSizeException e) {
        throw new IllegalStateException(e);
    } catch (BadPaddingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.blackcrowsys.sinscrypto.AesEncryptor.java

@Override
public String decrypt(String secretkey, String iv, String toDecrypt)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, InvalidAlgorithmParameterException, DecoderException {
    Cipher cipher = Cipher.getInstance(AESMODE);
    SecretKeySpec secretKeySpec = new SecretKeySpec(secretkey.getBytes(), AES);
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(Hex.decodeHex(iv.toCharArray())));
    return new String(cipher.doFinal(Base64.decodeBase64(toDecrypt)));
}

From source file:com.amazonaws.cognito.devauthsample.AESEncryption.java

private static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) {
    try {//from ww  w.j a  v a2s .co m
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        cipher.init(Cipher.DECRYPT_MODE, getKey(key), params);
        return cipher.doFinal(cipherBytes);
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to decrypt.", e);
    }
}

From source file:com.vmware.fdmsecprotomgmt.PasswdEncrypter.java

/**
 * Decrypt the encrypted value with provided key
 *//*from   w w  w  .  j  a v a2 s . c o  m*/
public static String decrypt(String key, String encryptedValue) {
    String decryptedString = null;
    try {
        IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

        decryptedString = new String(cipher.doFinal(Base64.decodeBase64(encryptedValue)));

    } catch (Exception ex) {
        System.out.println("Caught exception while decrypting string");
        ex.printStackTrace();
    }

    return decryptedString;
}

From source file:corner.util.crypto.Cryptor.java

/**
 * IO?,IO??,./*from  w w  w .j  a  v  a 2 s. c  o m*/
 * @param outFileName ??. 
 * @param keyFile ??.
 * @return ??.
 */
public static OutputStream encryptFileIO(String outFileName, String keyFile) {
    if (keyFile == null) {
        try {
            return new FileOutputStream(outFileName);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    SecretKey key = null;

    //?
    ObjectInputStream keyis;
    try {
        keyis = new ObjectInputStream(new FileInputStream(keyFile));
        key = (SecretKey) keyis.readObject();
        keyis.close();
    } catch (FileNotFoundException e) {
        log.error("file not found!", e);
        throw new RuntimeException("file not found", e);
    } catch (IOException e) {
        log.error("io occour exception", e);
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        log.error("Class Not Found  exception", e);
        throw new RuntimeException(e);
    }

    //keyCipher
    Cipher cipher = null;
    //?Cipher?

    try {
        cipher = Cipher.getInstance("DES");
        //?

        cipher.init(Cipher.ENCRYPT_MODE, key);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    }

    //???

    //
    CipherOutputStream out = null;
    try {
        out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(outFileName)), cipher);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }

    return out;

}

From source file:com.charandeepmatta.oracle.sqldeveloper.DecodePasswordHash.java

private byte[] decryptPassword(final byte[] result) throws GeneralSecurityException {
    byte constant = result[0];
    if (constant != (byte) 5) {
        throw new IllegalArgumentException();
    }//from w w w.jav a 2  s  . co m
    byte[] secretKey = new byte[8];
    System.arraycopy(result, 1, secretKey, 0, 8);
    byte[] encryptedPassword = new byte[result.length - 9];
    System.arraycopy(result, 9, encryptedPassword, 0, encryptedPassword.length);
    byte[] iv = new byte[8];
    for (int i = 0; i < iv.length; i++) {
        iv[i] = 0;
    }
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey, "DES"), new IvParameterSpec(iv));
    return cipher.doFinal(encryptedPassword);
}