Example usage for javax.crypto Cipher doFinal

List of usage examples for javax.crypto Cipher doFinal

Introduction

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

Prototype

public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException 

Source Link

Document

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

Usage

From source file:net.duckling.ddl.web.agent.util.AuthUtil.java

private static String decodeAuth(String auth) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    SecretKeySpec spec = new SecretKeySpec(getKey(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, spec);
    byte[] result = cipher.doFinal(Base64.decodeBase64(auth));
    return new String(result, "UTF-8");
}

From source file:aes_encryption.AES_Encryption.java

public static String decrypt(String strToDecrypt) {

    try {//w  ww  .  j  a  v  a 2  s  . c  o  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");

        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt))));

    } catch (Exception e) {

        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}

From source file:Main.java

private static String encryptText(char[] plaintext, Cipher cipher) {
    byte[] plaintextBytes = charArrayToByteArray(plaintext);
    byte[] ciphertext = {};

    try {//from w ww .j a va2 s .  c  om
        ciphertext = cipher.doFinal(plaintextBytes);
    } catch (Exception e) {
        Log.d(LOG_TAG, "encryptText", e);
    }

    //turn into hex so storage as a string is possible (db)
    return toHexString(ciphertext);
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

public static String decryptUsingAES(SecretKey secretKey, String dataToDecrypt) throws NoSuchPaddingException,
        NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher aesCipher = Cipher.getInstance("AES");
    aesCipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] bytePlainText = aesCipher.doFinal(Base64.getDecoder().decode(dataToDecrypt));
    return new String(bytePlainText);
}

From source file:br.com.zeros.tipsandtricks.cryto.EncryptionStrings.java

public static String decrypt(String encryptedValue) throws Exception {
    Key key = generateKey();// w w  w.  j a  v  a 2s.c o m
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new Base64().decode(encryptedValue);
    byte[] decValue = c.doFinal(decordedValue);//////////LINE 50
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

public static String encryptUsingAES(SecretKey secretKey, String dataToEncrypt) throws NoSuchPaddingException,
        NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher aesCipher = Cipher.getInstance("AES");
    aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] byteCipherText = aesCipher.doFinal(dataToEncrypt.getBytes());
    return Base64.getEncoder().encodeToString(byteCipherText);
}

From source file:com.vmware.bdd.security.EncryptionGuard.java

/**
 * Encrypt the clear text against given secret key.
 * //from ww w  .  jav  a  2  s.com
 * @param clearText
 *           the clear string
 * @return the encrypted string, or null if the clear string is null
 * @throws CommonException
 *            if input arguments is null
 */
public static String encode(String clearText) throws GeneralSecurityException, UnsupportedEncodingException {
    if (clearText == null) {
        return null;
    }

    Key key = GuardKeyStore.getEncryptionKey();
    String salt = SaltGenerator.genRandomString(SALT_SIZE);

    String inputText = salt + clearText; // add salt
    byte[] clearBytes = inputText.getBytes(UTF8_ENCODING);

    Cipher cipher = getCiperInternal(Cipher.ENCRYPT_MODE, key);
    byte[] encryptedBytes = cipher.doFinal(clearBytes);

    Base64 base64 = new Base64(0); // 0 - no chunking
    return salt + base64.encodeToString(encryptedBytes);
}

From source file:com.lingxiang2014.util.RSAUtils.java

public static byte[] decrypt(PrivateKey privateKey, byte[] data) {
    Assert.notNull(privateKey);/*from   w w  w  .  ja  va  2 s  . c o m*/
    Assert.notNull(data);
    try {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", PROVIDER);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        return null;
    }
}

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

public static String encrypt(String key, String data) {
    byte[] encryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {//from   w  w w .  j  ava  2  s  .c  o  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        encryptedData = cipher.doFinal(data.getBytes());

        byte[] encr64 = Base64.encodeBase64(encryptedData);

        //System.out.println(new String(encr64));

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

    return null;
}

From source file:com.vmware.bdd.security.EncryptionGuard.java

/**
 * Decrypt the encrypted text against given secret key.
 * /*from  w  w  w  .  j  a  v a2  s. c om*/
 * @param encodedText
 *           the encrypted string
 * @return the clear string, or null if encrypted string is null
 * @throws CommonException
 *            if input arguments is null
 */
public static String decode(String encodedText) throws GeneralSecurityException, UnsupportedEncodingException {
    if (encodedText == null) {
        return null;
    }

    if (encodedText.length() < SALT_SIZE) {
        throw EncryptionException.SHORT_ENCRYPTED_STRING(encodedText);
    }

    Key key = GuardKeyStore.getEncryptionKey();
    String salt = encodedText.substring(0, SALT_SIZE);
    String encryptedText = encodedText.substring(SALT_SIZE);

    Base64 base64 = new Base64(0); // 0 - no chunking
    byte[] encryptedBytes = base64.decode(encryptedText);

    Cipher cipher = getCiperInternal(Cipher.DECRYPT_MODE, key);
    byte[] outputBytes = cipher.doFinal(encryptedBytes);

    String outputText = new String(outputBytes, UTF8_ENCODING);
    AuAssert.check(salt.equals(outputText.substring(0, SALT_SIZE))); // Assert salt

    return outputText.substring(SALT_SIZE);
}