Search in sources :

Example 31 with Cipher

use of javax.crypto.Cipher in project wycheproof by google.

the class AesGcmTest method testByteBufferTooShort.

public void testByteBufferTooShort() throws Exception {
    for (GcmTestVector test : getTestVectors()) {
        // Encryption
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        ByteBuffer ptBuffer = ByteBuffer.wrap(test.pt);
        ByteBuffer ctBuffer = ByteBuffer.allocate(test.ct.length - 1);
        cipher.init(Cipher.ENCRYPT_MODE, test.key, test.parameters);
        cipher.updateAAD(test.aad);
        try {
            cipher.doFinal(ptBuffer, ctBuffer);
            fail("This should not work");
        } catch (ShortBufferException ex) {
        // expected
        }
        // Decryption
        ctBuffer = ByteBuffer.wrap(test.ct);
        ByteBuffer decrypted = ByteBuffer.allocate(test.pt.length - 1);
        cipher.init(Cipher.DECRYPT_MODE, test.key, test.parameters);
        cipher.updateAAD(test.aad);
        try {
            cipher.doFinal(ctBuffer, decrypted);
            fail("This should not work");
        } catch (ShortBufferException ex) {
        // expected
        }
    }
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) Cipher(javax.crypto.Cipher) ByteBuffer(java.nio.ByteBuffer)

Example 32 with Cipher

use of javax.crypto.Cipher in project wycheproof by google.

the class CipherInputStreamTest method testDecrypt.

/** JDK-8016249: CipherInputStream in decrypt mode fails on close with AEAD ciphers */
@SuppressWarnings("InsecureCryptoUsage")
public void testDecrypt(Iterable<TestVector> tests) throws Exception {
    for (TestVector t : tests) {
        Cipher cipher = Cipher.getInstance(t.algorithm);
        cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
        cipher.updateAAD(t.aad);
        InputStream is = new ByteArrayInputStream(t.ct);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        byte[] result = new byte[t.pt.length];
        int totalLength = 0;
        int length = 0;
        do {
            length = cis.read(result, totalLength, result.length - totalLength);
            if (length > 0) {
                totalLength += length;
            }
        } while (length >= 0 && totalLength != result.length);
        assertEquals(-1, cis.read());
        cis.close();
        assertEquals(TestUtil.bytesToHex(t.pt), TestUtil.bytesToHex(result));
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Cipher(javax.crypto.Cipher)

Example 33 with Cipher

use of javax.crypto.Cipher in project wycheproof by google.

the class CipherOutputStreamTest method testCorruptDecryptEmpty.

@SuppressWarnings("InsecureCryptoUsage")
public void testCorruptDecryptEmpty(Iterable<TestVector> tests) throws Exception {
    for (TestVector t : tests) {
        Cipher cipher = Cipher.getInstance(t.algorithm);
        cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
        cipher.updateAAD(t.aad);
        byte[] ct = Arrays.copyOf(t.ct, t.ct.length);
        ct[ct.length - 1] ^= (byte) 1;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        cos.write(ct);
        try {
            // cos.close() should call cipher.doFinal().
            cos.close();
            byte[] decrypted = os.toByteArray();
            fail("this should fail; decrypted:" + TestUtil.bytesToHex(decrypted) + " pt: " + TestUtil.bytesToHex(t.pt));
        } catch (IOException ex) {
        // expected
        }
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) Cipher(javax.crypto.Cipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 34 with Cipher

use of javax.crypto.Cipher in project keywhiz by square.

the class ContentCryptographer method gcm.

private byte[] gcm(Mode mode, String info, byte[] nonce, byte[] data) {
    try {
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, encryptionProvider);
        SecretKey derivedKey = deriveKey(cipher.getBlockSize(), info);
        GCMParameterSpec gcmParameters = new GCMParameterSpec(TAG_BITS, nonce);
        cipher.init(mode.cipherMode, derivedKey, gcmParameters);
        return cipher.doFinal(data);
    } catch (IllegalBlockSizeException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException e) {
        throw Throwables.propagate(e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 35 with Cipher

use of javax.crypto.Cipher in project android-delicious by lexs.

the class ObscuredSharedPreferences method decrypt.

protected String decrypt(String value) {
    try {
        final byte[] bytes = value != null ? Base64.decode(value, Base64.DEFAULT) : new byte[0];
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(secret));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(getSalt(), 20));
        return new String(pbeCipher.doFinal(bytes), UTF8);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

Cipher (javax.crypto.Cipher)700 SecretKeySpec (javax.crypto.spec.SecretKeySpec)193 SecretKey (javax.crypto.SecretKey)183 IvParameterSpec (javax.crypto.spec.IvParameterSpec)150 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)126 InvalidKeyException (java.security.InvalidKeyException)84 IOException (java.io.IOException)79 SecretKeyFactory (javax.crypto.SecretKeyFactory)76 GeneralSecurityException (java.security.GeneralSecurityException)69 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)65 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)62 Key (java.security.Key)60 BadPaddingException (javax.crypto.BadPaddingException)57 SecureRandom (java.security.SecureRandom)45 KeyGenerator (javax.crypto.KeyGenerator)45 UnsupportedEncodingException (java.io.UnsupportedEncodingException)43 PrivateKey (java.security.PrivateKey)43 PublicKey (java.security.PublicKey)41 PBEKeySpec (javax.crypto.spec.PBEKeySpec)38 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)36