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:authentication.AES.java

public static String encrypt(String textopuro) throws Exception {
    Cipher encripta = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(chaveencriptacao.getBytes("UTF-8"), "AES");
    encripta.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
    byte[] a = encripta.doFinal(textopuro.getBytes("UTF-8"));
    return StringUtils.newStringUtf8(Base64.encodeBase64(a, false));
}

From source file:com.amazonaws.cognito.sync.demo.client.server.AESEncryption.java

private static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) throws Exception {
    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);
}

From source file:com.wabacus.util.DesEncryptTools.java

public static String encrypt(String originalString) {
    if (originalString == null || originalString.trim().equals(""))
        return "";
    if (KEY_OBJ == null) {
        log.warn("");
        return originalString;
    }/*  w w  w. j a  v a 2  s.  com*/
    try {
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.ENCRYPT_MODE, KEY_OBJ);
        return base64Encode(c1.doFinal(originalString.getBytes()));
    } catch (Exception e) {
        log.error("" + originalString + "", e);
        return null;
    }
}

From source file:Main.java

public static byte[] encrypt2(byte[] data, String key) throws Exception {
    try {//from www  .  j  ava  2  s  .co  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        byte[] base64Key = Base64.decode(key, Base64.DEFAULT);
        SecretKeySpec keyspec = new SecretKeySpec(base64Key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keyspec);
        byte[] encrypted = cipher.doFinal(data);
        return encrypted;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String encrypt(String data, String key) throws Exception {
    try {/*from  w  ww .j  a  v  a  2  s. c o m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        byte[] base64Key = Base64.decode(key, Base64.DEFAULT);
        SecretKeySpec keyspec = new SecretKeySpec(base64Key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keyspec);
        byte[] encrypted = cipher.doFinal(data.getBytes("UTF-8"));
        return Base64.encodeToString(encrypted, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.cl.roadshow.crypto.AESCtr.java

/**
 * Generate encryption key based on provided keystring.
 * //  w  w  w.  j  av a2s .  co  m
 * @param keystring
 * @param bits
 * @return Encryption key
 * @throws Exception
 */
private static Key generateKey(String keystring, int bits) throws Exception {
    byte[] keyBytes = new byte[bits];
    byte[] key = new byte[bits];
    for (int i = 0; i < bits; i++) {
        //keyBytes[i] = (byte) keystring.codePointAt(i);
        // ??http://www.movable-type.co.uk/scripts/aes.html ?key
        keyBytes[i] = (byte) 0;
    }
    SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    key = cipher.doFinal(keyBytes);
    // Expand key to original length of keybytes
    for (int i = 0; i < bits - 16; i++) {
        key[16 + i] = key[i];
    }

    return new SecretKeySpec(key, "AES");
}

From source file:de.uzk.hki.da.passwordEncryptor.passwordEncryptor.java

private static String encryptPasswordForContentBroker(String password) {

    byte key[] = "394z57f4".getBytes();
    byte encryptedPassword[];

    try {//from  w ww .ja  v a  2 s . c o m
        SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key));

        Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
        encrypt.init(Cipher.ENCRYPT_MODE, secretKey,
                new IvParameterSpec(new byte[] { 0x01, 0x02, 0x04, 0x10, 0x01, 0x02, 0x04, 0x10 }));
        encryptedPassword = encrypt.doFinal(password.getBytes());
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Couldn't encrypt password " + password + e);
    }

    return new String(Base64.encodeBase64(encryptedPassword));
}

From source file:Main.java

/**
 * Computes an Ephemeral ID./*ww  w .  j  a  v  a  2  s .c  o m*/
 * @param key                 AES key (Advertiser Identity Key). The first 16 bytes are used.
 * @param timeCounter         Advertiser time counter
 * @param rotationExponent    Advertiser rotation exponent (0 to 15)
 * @return Final ephemeral key of 16 bytes, of which only the first 8 bytes should be used.
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
@NonNull
public static byte[] computeEID(byte[] key, int timeCounter, byte rotationExponent)
        throws GeneralSecurityException {
    //        String transformation = "AES/CBC/PKCS5Padding";
    String transformation = "AES/ECB/NoPadding";
    @SuppressLint("GetInstance")
    // spec says it has to be ECB, ignore lint warning
    Cipher aes = Cipher.getInstance(transformation);
    aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, 0, 16, "AES"));

    byte[] tempKey = aes
            .doFinal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff,
                    0x00, 0x00, (byte) ((timeCounter >>> 24) & 0xff), (byte) ((timeCounter >>> 16) & 0xff) });

    // clear K lowest bits
    timeCounter = timeCounter >>> rotationExponent << rotationExponent;

    // reset cipher with a new encryption key
    aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(tempKey, "AES"));
    return aes.doFinal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            rotationExponent, (byte) ((timeCounter >>> 24) & 0xff), (byte) ((timeCounter >>> 16) & 0xff),
            (byte) ((timeCounter >>> 8) & 0xff), (byte) (timeCounter & 0xff) });
}

From source file:Main.java

/**
 * More flexible AES decrypt that doesn't encode
 *
 * @param key AES key typically 128, 192 or 256 bit
 * @param iv Initiation Vector/*from   www.ja  v a  2  s .  co m*/
 * @param decodedCipherText in bytes (assumed it's already been decoded)
 * @return Decrypted message cipher text (not encoded)
 * @throws GeneralSecurityException if something goes wrong during encryption
 */
public static byte[] decrypt(final SecretKeySpec key, final byte[] iv, final byte[] decodedCipherText)
        throws GeneralSecurityException {
    final Cipher cipher = Cipher.getInstance(AES_MODE);
    //IvParameterSpec ivSpec = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decryptedBytes = cipher.doFinal(decodedCipherText);

    log("decryptedBytes", decryptedBytes);

    return decryptedBytes;
}

From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java

/**
 * /*from   w  ww . j  a v a2s. c om*/
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return byte[] ?
 */
public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception {

    // 
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    // ?
    // ???
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
    // 
    PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
    // ?
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, pubKey);
    return cipher.doFinal(data);
}