Search in sources :

Example 51 with Cipher

use of javax.crypto.Cipher in project jdk8u_jdk by JetBrains.

the class CipherInputStreamExceptions method gcm_shortReadAEAD.

/* Short read stream,
     * This test
     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
     *   2) Reads 100 bytes from stream to decrypt the message and closes
     *   3) Make sure no value is returned by read()
     *   4) Make sure no exception is thrown
     */
static void gcm_shortReadAEAD() throws Exception {
    Cipher c;
    byte[] read = new byte[100];
    System.out.println("Running gcm_shortReadAEAD");
    byte[] pt = new byte[600];
    pt[0] = 1;
    // Encrypt provided 600 bytes with AES/GCM/PKCS5Padding
    byte[] ct = encryptedText("GCM", pt);
    // Create stream for decryption
    CipherInputStream in = getStream("GCM", ct);
    int size = 0;
    try {
        size = in.read(read);
        in.close();
        if (read.length != 100) {
            throw new RuntimeException("Fail: read size = " + read.length + "should be 100.");
        }
        if (read[0] != 1) {
            throw new RuntimeException("Fail: The decrypted text does " + "not match the plaintext: '" + read[0] + "'");
        }
    } catch (IOException e) {
        System.out.println("  Fail: " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
    System.out.println("  Pass.");
}
Also used : RuntimeException(java.lang.RuntimeException) CipherInputStream(javax.crypto.CipherInputStream) Cipher(javax.crypto.Cipher) IOException(java.io.IOException)

Example 52 with Cipher

use of javax.crypto.Cipher in project jdk8u_jdk by JetBrains.

the class TestSealedObjectNull method main.

public static void main(String[] args) throws IOException, IllegalBlockSizeException, ClassNotFoundException, BadPaddingException {
    Cipher nullCipher = new NullCipher();
    // Seal
    SealedObject so = new SealedObject(SEAL_STR, nullCipher);
    // Unseal and compare
    if (!(SEAL_STR.equals(so.getObject(nullCipher)))) {
        throw new RuntimeException("Unseal and compare failed.");
    }
    System.out.println("Test passed.");
}
Also used : NullCipher(javax.crypto.NullCipher) SealedObject(javax.crypto.SealedObject) NullCipher(javax.crypto.NullCipher) Cipher(javax.crypto.Cipher)

Example 53 with Cipher

use of javax.crypto.Cipher in project jdk8u_jdk by JetBrains.

the class CipherStreamClose method streamEncrypt.

public static byte[] streamEncrypt(String message, SecretKey key, MessageDigest digest) throws Exception {
    byte[] data;
    Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    encCipher.init(Cipher.ENCRYPT_MODE, key);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DigestOutputStream dos = new DigestOutputStream(bos, digest);
        CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
        try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
            oos.writeObject(message);
        }
        data = bos.toByteArray();
    }
    if (debug) {
        System.out.println(DatatypeConverter.printHexBinary(data));
    }
    return data;
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) DigestOutputStream(java.security.DigestOutputStream) Cipher(javax.crypto.Cipher)

Example 54 with Cipher

use of javax.crypto.Cipher in project android_frameworks_base by DirtyUnicorns.

the class LockSettingsService method getDecryptedPasswordForTiedProfile.

private String getDecryptedPasswordForTiedProfile(int userId) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, CertificateException, IOException {
    if (DEBUG)
        Slog.v(TAG, "Get child profile decrytped key");
    byte[] storedData = mStorage.readChildProfileLock(userId);
    if (storedData == null) {
        throw new FileNotFoundException("Child profile lock file not found");
    }
    byte[] iv = Arrays.copyOfRange(storedData, 0, PROFILE_KEY_IV_SIZE);
    byte[] encryptedPassword = Arrays.copyOfRange(storedData, PROFILE_KEY_IV_SIZE, storedData.length);
    byte[] decryptionResult;
    java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore");
    keyStore.load(null);
    SecretKey decryptionKey = (SecretKey) keyStore.getKey(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT + userId, null);
    Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_GCM + "/" + KeyProperties.ENCRYPTION_PADDING_NONE);
    cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new GCMParameterSpec(128, iv));
    decryptionResult = cipher.doFinal(encryptedPassword);
    return new String(decryptionResult, StandardCharsets.UTF_8);
}
Also used : SecretKey(javax.crypto.SecretKey) FileNotFoundException(java.io.FileNotFoundException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 55 with Cipher

use of javax.crypto.Cipher in project android_frameworks_base by DirtyUnicorns.

the class CryptoHelper method decryptBundle.

@Nullable
/* default */
Bundle decryptBundle(@NonNull Bundle bundle) throws GeneralSecurityException {
    Preconditions.checkNotNull(bundle, "Cannot decrypt null bundle.");
    byte[] iv = bundle.getByteArray(KEY_IV);
    byte[] encryptedBytes = bundle.getByteArray(KEY_CIPHER);
    byte[] mac = bundle.getByteArray(KEY_MAC);
    if (!verifyMac(encryptedBytes, iv, mac)) {
        Log.w(TAG, "Escrow mac mismatched!");
        return null;
    }
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, mEncryptionKey, ivSpec);
    byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
    Parcel decryptedParcel = Parcel.obtain();
    decryptedParcel.unmarshall(decryptedBytes, 0, decryptedBytes.length);
    decryptedParcel.setDataPosition(0);
    Bundle decryptedBundle = new Bundle();
    decryptedBundle.readFromParcel(decryptedParcel);
    decryptedParcel.recycle();
    return decryptedBundle;
}
Also used : Parcel(android.os.Parcel) Bundle(android.os.Bundle) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) Nullable(android.annotation.Nullable)

Aggregations

Cipher (javax.crypto.Cipher)1531 SecretKeySpec (javax.crypto.spec.SecretKeySpec)516 SecretKey (javax.crypto.SecretKey)377 IvParameterSpec (javax.crypto.spec.IvParameterSpec)368 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)349 InvalidKeyException (java.security.InvalidKeyException)257 IOException (java.io.IOException)202 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)202 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)196 BadPaddingException (javax.crypto.BadPaddingException)183 GeneralSecurityException (java.security.GeneralSecurityException)161 SecretKeyFactory (javax.crypto.SecretKeyFactory)149 Key (java.security.Key)148 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)134 SecureRandom (java.security.SecureRandom)105 PrivateKey (java.security.PrivateKey)93 PublicKey (java.security.PublicKey)86 UnsupportedEncodingException (java.io.UnsupportedEncodingException)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)82 KeyGenerator (javax.crypto.KeyGenerator)78