Example usage for javax.crypto Cipher DECRYPT_MODE

List of usage examples for javax.crypto Cipher DECRYPT_MODE

Introduction

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

Prototype

int DECRYPT_MODE

To view the source code for javax.crypto Cipher DECRYPT_MODE.

Click Source Link

Document

Constant used to initialize cipher to decryption mode.

Usage

From source file:de.scrubstudios.srvmon.agent.classes.Crypt.java

/**
 * This function is used to decrypt incoming data.
 * @param key Encryption key/*from www.  j a va2 s .  c  o m*/
 * @param data Data string which should be decrypted.
 * @return The decrypted data string.
 */
public static String decrypt(String key, String data) {
    byte[] decryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        decryptedData = cipher.doFinal(Base64.decodeBase64(data));

        return new String(decryptedData);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:baggage.hypertoolkit.security.CipherText.java

public ClearText decrypt(SecretKey secretKey) throws MessageIntegrityException {
    try {// ww w  . j  av  a 2  s.co m
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new ClearText(cipher.doFinal(bytes));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new MessageIntegrityException();
    } catch (BadPaddingException e) {
        throw new MessageIntegrityException();
    }
}

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

protected ECBOracleChallenge(String name) {
    super(name);/*from   w  w  w  .  j  av a  2 s .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;// www  .ja  v a2s . c  o m

    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:com.POLIS.licensing.common.license.AbstractSerializationBasedLicenseFactory.java

@Override
public T loadLicense(String data, PrivateKey senderEncryptionKey, PublicKey senderSignatureKey)
        throws BadLicenseException, SystemStateException, OperationException {
    byte[] decodedLicense;

    decodedLicense = Base64.decodeBase64(data);

    byte[] encryptedSymKey = new byte[symkeySize];
    byte[] encryptedLicense = new byte[decodedLicense.length - symkeySize];
    byte[] decryptedSymKey;
    byte[] decryptedLicense;
    Key symkey;/*from  w ww  .j  av a 2 s .c om*/

    System.arraycopy(decodedLicense, 0, encryptedSymKey, 0, symkeySize);
    System.arraycopy(decodedLicense, symkeySize, encryptedLicense, 0, decodedLicense.length - symkeySize);

    Cipher asymetriccipher;
    try {
        asymetriccipher = Cipher.getInstance(AbstractSerializationBasedLicense.asymmetricEncoding,
                AbstractSerializationBasedLicense.provider);
        asymetriccipher.init(Cipher.DECRYPT_MODE, senderEncryptionKey);
    } catch (NoSuchAlgorithmException | NoSuchProviderException
            | /*InvalidKeySpecException |*/ NoSuchPaddingException | InvalidKeyException ex) {
        throw new SystemStateException("The specified encryption provider or algorithm was not found", ex);
    }

    try {
        decryptedSymKey = asymetriccipher.doFinal(encryptedSymKey);
        symkey = new SecretKeySpec(decryptedSymKey, "AES");
    } catch (IllegalBlockSizeException | BadPaddingException ex) {
        throw new SystemStateException("Could not decode the symkey for the license", ex);
    }

    Cipher symmetriccipher;
    try {
        symmetriccipher = Cipher.getInstance(AbstractSerializationBasedLicense.symmetricEncoding,
                AbstractSerializationBasedLicense.provider);
        symmetriccipher.init(Cipher.DECRYPT_MODE, symkey);
    } catch (NoSuchAlgorithmException | NoSuchProviderException
            | /*InvalidKeySpecException |*/ NoSuchPaddingException | InvalidKeyException ex) {
        throw new SystemStateException("The specified encryption provider or algorithm was not found", ex);
    }

    try {
        decryptedLicense = symmetriccipher.doFinal(encryptedLicense);
    } catch (IllegalBlockSizeException | BadPaddingException ex) {
        throw new SystemStateException("Could not encode to base64", ex);
    }
    T license;
    try {
        license = getDeserializedObject(decryptedLicense);
    } catch (IOException | ClassNotFoundException ex) {
        throw new SystemStateException("An error occurred while reading the license from the input byte array.",
                ex);
    }
    if (license.verifyLicense(senderSignatureKey)) {
        return license;
    } else
        throw new BadLicenseException(license,
                "The license could not be verified with the specified signature decryption key");

}

From source file:com.sapienter.jbilling.common.JBCryptoImpl.java

public String decrypt(String cryptedText) {
    Cipher cipher = getCipher();/*w w w  .  ja v a 2s  .  c  o  m*/
    byte[] crypted = useHexForBinary ? Util.stringToBinary(cryptedText)
            : Base64.decodeBase64(cryptedText.getBytes());
    byte[] result;
    try {
        cipher.init(Cipher.DECRYPT_MODE, mySecretKey, ourPBEParameters);
        result = cipher.doFinal(crypted);
    } catch (GeneralSecurityException e) {
        throw new IllegalArgumentException("Can not decrypt:" + cryptedText, e);
    }
    return new String(result, UTF8);
}

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

private static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) {
    try {//from  ww  w. jav  a2s.c o 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.yukthi.utils.CryptoUtils.java

/**
 * Decrypts specified input. Input should be encrypted and hex string (the way it is generated
 * by {@link #encrypt(String, String)} method
 * @param secretKey Secret key to use for decryption
 * @param input Input hex string to be decrypted
 * @return Decrypted value/*from  w w w  . j a  v  a2s.co  m*/
 */
public static String decrypt(String secretKey, String input) {
    if (secretKey.length() != 16) {
        throw new InvalidArgumentException("Secret key should be of length 16. Specified secrey key length - ",
                secretKey.length());
    }

    try {
        //decode input string hex string
        byte decodedBytes[] = Hex.decodeHex(input.toCharArray());

        //decrypt decoded bytes
        byte decryptedBytes[] = doCrypto(Cipher.DECRYPT_MODE, secretKey, decodedBytes);

        //convert and return decrypted bytes into string
        return new String(decryptedBytes);
    } catch (Exception ex) {
        throw new InvalidArgumentException(ex, "An error occurred while decrypting string - {}", input);
    }
}

From source file:de.thischwa.pmcms.tool.DESCryptor.java

public String decrypt(String encryptedTxt) throws CryptorException {
    if (encryptedTxt == null || encryptedTxt.trim().length() == 0)
        return null;
    if (key == null)
        key = buildKey();/*from  ww  w .  j  a  va  2 s . co m*/
    try {
        byte[] encrypedPwdBytes = Base64.decodeBase64(encryptedTxt);
        Cipher cipher = Cipher.getInstance(algorithm); // cipher is not thread safe
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainTextPwdBytes = cipher.doFinal(encrypedPwdBytes);
        return new String(plainTextPwdBytes);
    } catch (Exception e) {
        throw new CryptorException(e);
    }
}

From source file:com.intel.chimera.codec.JceAesCtrCryptoCodec.java

@Override
public Decryptor createDecryptor() throws GeneralSecurityException {
    return new JceAesCtrCipher(Cipher.DECRYPT_MODE, provider);
}