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:Main.java

private static byte[] getDesCode(Context mContext, byte[] byteD) {
    Cipher cipher;
    byte[] byteFina = null;
    try {/*from ww  w.ja va 2s  . c  om*/
        cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, getKey(mContext));
        byteFina = cipher.doFinal(byteD);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }
    return byteFina;
}

From source file:de.fhdo.helper.DES.java

public static String decrypt(String Text) {
    try {//  w  ww  .jav  a 2 s. c o  m
        DESKeySpec keySpec = new DESKeySpec("schluessel_stdrepository15".getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(keySpec);

        byte[] encrypedPwdBytes = Base64.decodeBase64(Text);
        Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe
        cipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes));

        return new String(plainTextPwdBytes);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return "";
}

From source file:aes_encryption.AES_Encryption.java

public static String encrypt(String strToEncrypt) {
    try {//from  w  w  w.jav a  2  s.com
        Cipher cipher = Cipher.getInstance("AES_Encryption/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))));
    } catch (Exception e) {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

From source file:com.rdonasco.security.utils.EncryptionUtil.java

public static String encryptWithPassword(String stringToEncrypt, String password) throws Exception {
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), SALT, COUNT);
    SecretKey pbeKey = getKeyFactory().generateSecret(pbeKeySpec);
    Cipher pbeCipher = Cipher.getInstance(CIPHER_KEY_SPEC);
    pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, PBE_PARAM_SPEC);
    byte[] encryptedBytes = pbeCipher.doFinal(stringToEncrypt.getBytes());
    return Base64.encodeBase64String(encryptedBytes);
}

From source file:com.rdonasco.security.utils.EncryptionUtil.java

public static String decryptWithPassword(String encryptedString, String password) throws Exception {
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), SALT, COUNT);
    SecretKey pbeKey = getKeyFactory().generateSecret(pbeKeySpec);
    Cipher pbeCipher = Cipher.getInstance(CIPHER_KEY_SPEC);
    pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, PBE_PARAM_SPEC);
    byte[] decryptedBytes = pbeCipher.doFinal(Base64.decodeBase64(encryptedString));
    return new String(decryptedBytes);
}

From source file:de.taimos.dvalin.interconnect.model.MessageCryptoUtil.java

/**
 *
 * @param data the data to encrypt/*ww w . java 2  s.c  om*/
 * @return the encrypted BASE64 data
 * @throws CryptoException on encryption error
 */
public static String crypt(final String data) throws CryptoException {
    if (data == null) {
        return null;
    }

    try {
        final Cipher cipher = MessageCryptoUtil.getCipher(Cipher.ENCRYPT_MODE);
        final byte[] encrypted = cipher.doFinal(data.getBytes(Charset.forName("UTF-8")));
        return Base64.encodeBase64String(encrypted);
    } catch (final Exception e) {
        throw new CryptoException("Encryption of data failed!", e);
    }
}

From source file:io.kahu.hawaii.util.encryption.CryptoUtil.java

public static String encrypt(String unencrypted, String key, String initVector) throws ServerException {
    try {//from w ww.  ja  va  2s. c  om
        Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, key, initVector);

        byte[] encrypted = cipher.doFinal(unencrypted.getBytes());
        return Base64.encodeBase64String(encrypted);
    } catch (GeneralSecurityException e) {
        throw new ServerException(ServerError.ENCRYPTION, e);
    }
}

From source file:io.kahu.hawaii.util.encryption.CryptoUtil.java

public static String decrypt(String encrypted, String key, String initVector) throws ServerException {
    try {/*from  w ww .  j  av a2  s. c  om*/
        Cipher cipher = initCipher(Cipher.DECRYPT_MODE, key, initVector);

        byte[] decrypted = cipher.doFinal(Base64.decodeBase64(encrypted));

        return new String(decrypted);
    } catch (GeneralSecurityException e) {
        throw new ServerException(ServerError.ENCRYPTION, e);
    }
}

From source file:Main.java

public static String encryption(String string) throws Exception {
    // TODO Auto-generated method stub

    //generate symmetric key
    KeyGenerator keygt = KeyGenerator.getInstance("AES");
    keygt.init(128);//from w w  w .  ja  v a  2 s. c om
    SecretKey symkey = keygt.generateKey();

    //get it encoded
    byte[] aes_ba = symkey.getEncoded();

    //create cipher
    SecretKeySpec skeySpec = new SecretKeySpec(aes_ba, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    //encryption
    byte[] EncSymbyteArray = cipher.doFinal(string.getBytes());

    //encrypt symKey with PublicKey
    //        Key pubKey = getPublicKey();

    Key pubKey = publicKey;

    //RSA cipher
    Cipher cipherAsm = Cipher.getInstance("RSA", "BC");
    cipherAsm.init(Cipher.ENCRYPT_MODE, pubKey);

    //RSA encryption
    byte[] asymEncsymKey = cipherAsm.doFinal(aes_ba);

    //           File f3 = new File(BASE_PATH,"enc.txt");
    //           File f3key = new File(BASE_PATH,"enckey.txt");
    //           File f3file = new File(BASE_PATH,"encfile.txt");
    //           writeToFile2(f3,f3key,f3file, asymEncsymKey, EncSymbyteArray);

    //byte != new String
    //return new String(byteMerger(asymEncsymKey, EncSymbyteArray));
    return Base64.encodeToString(byteMerger(asymEncsymKey, EncSymbyteArray), Base64.DEFAULT);

}

From source file:com.aast.encrypt.EncryptManager.java

public static String decryptAES(String key, String encrypted) {
    try {/*www  . j  a va  2s  .c o m*/
        byte[] keyBytes = getKeyFromString(key);
        IvParameterSpec iv = new IvParameterSpec(keyBytes);
        SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");

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

        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}