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:com.jaspersoft.jasperserver.jaxrs.client.core.EncryptionUtils.java

private static String getEncryptedPassword(PublicKey publicKey, String pwd) throws Exception {
    byte[] encryptedUtfPass;
    Cipher enc = Cipher.getInstance("RSA/NONE/NoPadding", new BouncyCastleProvider());
    enc.init(Cipher.ENCRYPT_MODE, publicKey);
    String utfPass = URLEncoder.encode(pwd, CharEncoding.UTF_8);
    encryptedUtfPass = enc.doFinal(utfPass.getBytes());

    return byteArrayToHexString(encryptedUtfPass);
}

From source file:com.tremolosecurity.unison.u2f.util.U2fUtil.java

public static String encode(List<SecurityKeyData> devices, String encyrptionKeyName) throws Exception {
    ArrayList<KeyHolder> keys = new ArrayList<KeyHolder>();
    for (SecurityKeyData dr : devices) {
        KeyHolder kh = new KeyHolder();
        kh.setCounter(dr.getCounter());//from   www  .j  a  va2 s .  c o m
        kh.setEnrollmentTime(dr.getEnrollmentTime());
        kh.setKeyHandle(dr.getKeyHandle());
        kh.setPublicKey(dr.getPublicKey());
        kh.setTransports(dr.getTransports());
        keys.add(kh);
    }

    String json = gson.toJson(keys);
    EncryptedMessage msg = new EncryptedMessage();

    SecretKey key = GlobalEntries.getGlobalEntries().getConfigManager().getSecretKey(encyrptionKeyName);
    if (key == null) {
        throw new Exception("Queue message encryption key not found");
    }

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    msg.setMsg(cipher.doFinal(json.getBytes("UTF-8")));
    msg.setIv(cipher.getIV());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DeflaterOutputStream compressor = new DeflaterOutputStream(baos,
            new Deflater(Deflater.BEST_COMPRESSION, true));

    compressor.write(gson.toJson(msg).getBytes("UTF-8"));
    compressor.flush();
    compressor.close();

    String b64 = new String(Base64.encodeBase64(baos.toByteArray()));

    return b64;

}

From source file:Main.java

static String encryptRsaB64(byte[] bytes, Key pubRsaKey) {
    try {//  ww  w  .j  a  va2s . c  om
        Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");
        // cipher.init(Cipher.ENCRYPT_MODE, pubRsaKey, new SecureRandom());
        SecureRandom sc = null;
        cipher.init(Cipher.ENCRYPT_MODE, pubRsaKey, sc);
        byte[] cipherText = cipher.doFinal(bytes);
        return encodeB64(cipherText);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.alliander.osgp.oslp.OslpUtils.java

private static byte[] createEncryptedHash(final byte[] message, final PrivateKey privateKey)
        throws GeneralSecurityException {

    final byte[] hash = createHash(message);

    // Encrypt the hash
    final Cipher cipher = Cipher.getInstance(FALLBACK_CIPHER);
    cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    return cipher.doFinal(hash);
}

From source file:com.avbravo.avbravoutils.crypto.Encriptador.java

public static String encrypt(String key, String cleartext) throws Exception {

    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] encrypted = cipher.doFinal(cleartext.getBytes());
    return new String(encodeBase64(encrypted));
}

From source file:com.sshutils.utils.CryptHelper.java

public static String decrypt(String strToDecrypt) {
    try {/*from   w w w  . j ava2s  .  com*/

        //                for (Provider provider: Security.getProviders()) {
        //  log.info(provider.getName());
        //  for (String key: provider.stringPropertyNames())
        //   log.info("\t" + key + "\t" + provider.getProperty(key));
        //}

        Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE, "BC");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
        return decryptedString;
    } catch (Exception e) {
        log.error("Error while decrypting", e);

    }
    return null;
}

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static byte[] decrypt(byte[] aesKeyEncrypted, PrivateKey privKey) {
    byte[] aesKey = null;
    try {/*  ww  w .j  a  v  a 2  s .  c  o  m*/
        Cipher cipher = Cipher.getInstance(ALGORITHM_ASYM);
        cipher.init(Cipher.DECRYPT_MODE, privKey);
        aesKey = cipher.doFinal(aesKeyEncrypted);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return aesKey;
}

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

/**
 * Decrypt a cipher in bytes using the specified key
 * // w w w .  j  av  a  2s  .c  om
 * @param cipherBytes encrypted bytes
 * @param key the key used in decryption
 * @param iv
 * @return
 * @throws Exception
 */
public 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.greenline.hrs.admin.util.encrypt.DESUtil.java

/**
 * Description ?/*from   ww w  .ja v  a  2 s . c om*/
 *
 * @param data
 * @param key  byte
 * @return
 * @throws Exception
 */
private static byte[] decrypt(byte[] data, byte[] key) throws GeneralSecurityException {
    // ????
    SecureRandom sr = new SecureRandom();

    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);

    // ?DESKeySpec??SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);

    // Cipher??
    Cipher cipher = Cipher.getInstance(DES);

    // ?Cipher
    cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

    return cipher.doFinal(data);
}

From source file:com.os.util.PasswordDecoderEncoder.java

public static String decrypt(String encryptPassword) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    key = convertHexToBytes(keyst);//from   w w w  . jav  a2  s .  co m
    final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    String encryptedString = new String(cipher.doFinal(Base64.decodeBase64(encryptPassword.getBytes())),
            "UTF-8");
    System.out.println(encryptedString);
    String passwordDecrypted = encryptedString.trim();

    return passwordDecrypted;

}