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.aimluck.eip.util.ALCellularUtils.java

/**
 * Triple DES ?????/*from w  w w. j av a  2s .  c om*/
 * 
 * @param plain
 *          ?
 * @return ?
 * @throws Exception
 *           ??
 */
@SuppressWarnings("unused")
private static String decrypt3Des(String key, String plain) throws Exception {
    String KEY_CRPTY_ALGORITHM = "DESede";

    // 24????
    byte[] tripleDesKeyData = new byte[24];
    byte[] kyebyte = key.getBytes();
    int len = kyebyte.length;
    for (int i = 0; i < len; i++) {
        tripleDesKeyData[i] = kyebyte[i];
    }
    SecretKey secretKey = new SecretKeySpec(tripleDesKeyData, KEY_CRPTY_ALGORITHM);

    Cipher cipher = Cipher.getInstance(KEY_CRPTY_ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, secretKey);

    Base64 decoder = new Base64();
    byte[] decParam = decoder.decode(plain.trim().getBytes());

    return String.valueOf(cipher.doFinal(decParam));
}

From source file:Main.java

public static byte[] encryptMsg(String msg, RSAPublicKeySpec pubKeySpec) {
    if (msg != null && pubKeySpec != null && !msg.isEmpty()) {
        try {/*from w ww  . ja  v  a  2  s.  c o m*/
            Log.w(TAG, "msg is: " + msg + " with length " + msg.length());
            KeyFactory fact = KeyFactory.getInstance("RSA");

            PublicKey pubKey = fact.generatePublic(pubKeySpec);

            // TODO encrypt the message and send it
            // Cipher cipher = Cipher.getInstance("RSA/None/NoPadding");
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            // Cipher cipher =
            // Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding",
            // "BC");
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            Log.d(TAG, "cipher block size is " + cipher.getBlockSize());
            byte[] msgByteArray = msg.getBytes();
            byte[] cipherData = new byte[cipher.getOutputSize(msgByteArray.length)];
            cipherData = cipher.doFinal(msgByteArray);
            Log.d(TAG, "output size is " + cipher.getOutputSize(msgByteArray.length));
            Log.d(TAG, "is the measurement already broken into chunks here? " + (new String(cipherData)));
            return cipherData;

        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "RSA algorithm not available", e);
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "", e);
        } catch (NoSuchPaddingException e) {
            Log.e(TAG, "", e);
        } catch (InvalidKeyException e) {
            Log.e(TAG, "", e);
        } catch (BadPaddingException e) {
            Log.e(TAG, "", e);
        } catch (IllegalBlockSizeException e) {
            Log.e(TAG, "", e);
        } catch (Exception e) {
            Log.e(TAG, "", e);
        } /*
           * catch (NoSuchProviderException e) { Log.e(TAG, "", e); }
           */
    }
    return null;
}

From source file:com.blackducksoftware.tools.commonframework.core.encryption.Password.java

/**
 * Decrypt the given binary/encrypted password, and return the original text
 * password. The argument should have been encrypted using a valid
 * (according to isValidPassword() password encrypted by the
 * encryptStringToBinary() method.//w ww. ja  va2s . c o  m
 *
 * @param encryptedPasswordBinary
 * @return the original text password
 * @throws KeyStoreException
 * @throws NoSuchAlgorithmException
 * @throws CertificateException
 * @throws IOException
 * @throws UnrecoverableKeyException
 * @throws InvalidKeyException
 * @throws NoSuchPaddingException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 */
public static String decryptBinaryToString(final byte[] encryptedPasswordBinary) throws KeyStoreException,
        NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException,
        InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {

    // get key
    final Key key = getKey(KEY_PASSWORD);
    if (key == null) {
        throw new InvalidKeyException("The password decryption key is null");
    }

    // decrypt
    final Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decryptedBytes = cipher.doFinal(encryptedPasswordBinary);
    decryptedBytes = Arrays.copyOf(decryptedBytes, MAX_LENGTH);

    // convert byte array to String
    final String reconstitutedString = new String(decryptedBytes, UTF8).trim();
    return reconstitutedString;
}

From source file:com.eucalyptus.imaging.manifest.DownloadManifestFactory.java

private static EncryptedKey reEncryptKey(EncryptedKey in, PublicKey keyToUse) throws Exception {
    // Decrypt key and IV with Eucalyptus
    PrivateKey pk = SystemCredentials.lookup(Eucalyptus.class).getPrivateKey();
    Cipher cipher = Ciphers.RSA_PKCS1.get();
    cipher.init(Cipher.DECRYPT_MODE, pk, Crypto.getSecureRandomSupplier().get());
    byte[] key = cipher.doFinal(Hashes.hexToBytes(in.getKey()));
    byte[] iv = cipher.doFinal(Hashes.hexToBytes(in.getIV()));
    // Encrypt key and IV with NC
    cipher.init(Cipher.ENCRYPT_MODE, keyToUse, Crypto.getSecureRandomSupplier().get());
    return new EncryptedKey(Hashes.bytesToHex(cipher.doFinal(key)), Hashes.bytesToHex(cipher.doFinal(iv)));
}

From source file:com.dinochiesa.edgecallouts.AesCryptoCallout.java

public static byte[] aesDecrypt(String cipherName, byte[] key, byte[] iv, byte[] cipherText) throws Exception {
    Cipher cipher = Cipher.getInstance(cipherName);
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
    byte[] clearText = cipher.doFinal(cipherText);
    return clearText;
}