Search in sources :

Example 31 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project robovm by robovm.

the class CipherOutputStream1Test method testWrite2.

/**
     * write(byte[] b) method testing. Tests that method writes correct values
     * to the underlying output stream.
     */
public void testWrite2() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestOutputStream tos = new TestOutputStream();
    CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
    cos.write(data);
    cos.flush();
    byte[] result = tos.toByteArray();
    if (!Arrays.equals(result, data)) {
        fail("CipherOutputStream wrote incorrect data.");
    }
    try {
        cos.write(null);
        fail("NullPointerException expected");
    } catch (NullPointerException e) {
    //expected
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) NullCipher(javax.crypto.NullCipher)

Example 32 with CipherOutputStream

use of javax.crypto.CipherOutputStream in project robovm by robovm.

the class CipherOutputStreamTest method test_close.

/**
     * javax.crypto.CipherOutputStream#close()
     */
public void test_close() throws Exception {
    // regression test for HARMONY-1139
    try {
        new CipherOutputStream((OutputStream) null, Cipher.getInstance("DES/CBC/PKCS5Padding")).close();
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
    // expected
    }
    CipherOutputStream ch = new CipherOutputStream((OutputStream) null) {
    };
    try {
        new CipherOutputStream(ch, Cipher.getInstance("DES/CBC/PKCS5Padding")).close();
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
    // expected
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) OutputStream(java.io.OutputStream) CipherOutputStream(javax.crypto.CipherOutputStream)

Example 33 with CipherOutputStream

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

the class CipherHelper method encryptData.

void encryptData(WrapToken token, byte[] confounder, byte[] plaintext, int start, int len, byte[] padding, OutputStream os) throws GSSException, IOException {
    switch(sealAlg) {
        case MessageToken.SEAL_ALG_DES:
            // Encrypt on the fly and write
            Cipher des = getInitializedDes(true, getDesEncryptionKey(keybytes), ZERO_IV);
            CipherOutputStream cos = new CipherOutputStream(os, des);
            // debug(getHexBytes(confounder, confounder.length));
            cos.write(confounder);
            // debug(" " + getHexBytes(plaintext, start, len));
            cos.write(plaintext, start, len);
            // debug(" " + getHexBytes(padding, padding.length));
            cos.write(padding);
            break;
        case MessageToken.SEAL_ALG_DES3_KD:
            byte[] ctext = des3KdEncrypt(confounder, plaintext, start, len, padding);
            // Write to stream
            os.write(ctext);
            break;
        case MessageToken.SEAL_ALG_ARCFOUR_HMAC:
            byte[] ciphertext = arcFourEncrypt(token, confounder, plaintext, start, len, padding);
            // Write to stream
            os.write(ciphertext);
            break;
        default:
            throw new GSSException(GSSException.FAILURE, -1, "Unsupported seal algorithm: " + sealAlg);
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) Cipher(javax.crypto.Cipher)

Example 34 with CipherOutputStream

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

the class WrongAAD method decryptWithWrongAAD.

/*
     * Attempt to decrypt the cipher text using Cipher object
     * initialized with some fake AAD.
     */
private void decryptWithWrongAAD() throws Exception {
    System.out.println("decrypt with wrong AAD");
    // initialize it with wrong AAD to get an exception during decryption
    Cipher decryptCipher = createCipher(Cipher.DECRYPT_MODE, encryptCipher.getParameters());
    byte[] someAAD = Helper.generateBytes(AAD_SIZE + 1);
    decryptCipher.updateAAD(someAAD);
    // init output stream
    try (ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
        CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decryptCipher)) {
        if (decrypt(ciOutput, baOutput)) {
            throw new RuntimeException("A decryption has been perfomed successfully in" + " spite of the decrypt Cipher has been" + " initialized with fake AAD");
        }
    }
    System.out.println("Passed");
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) Cipher(javax.crypto.Cipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 35 with CipherOutputStream

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

the class WrongAAD method decryptWithEmptyAAD.

/*
     * Attempt to decrypt a cipher text using Cipher object
     * initialized without AAD used for encryption.
     */
private void decryptWithEmptyAAD() throws Exception {
    System.out.println("decryptWithEmptyAAD() started");
    // initialize it with empty AAD to get exception during decryption
    Cipher decryptCipher = createCipher(Cipher.DECRYPT_MODE, encryptCipher.getParameters());
    try (ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
        CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decryptCipher)) {
        if (decrypt(ciOutput, baOutput)) {
            throw new RuntimeException("Decryption has been perfomed successfully in" + " spite of the decrypt Cipher has NOT been" + " initialized with AAD");
        }
    }
    System.out.println("decryptWithEmptyAAD() passed");
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) Cipher(javax.crypto.Cipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

CipherOutputStream (javax.crypto.CipherOutputStream)39 Cipher (javax.crypto.Cipher)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 IOException (java.io.IOException)13 SecretKeySpec (javax.crypto.spec.SecretKeySpec)9 CipherInputStream (javax.crypto.CipherInputStream)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)7 FileOutputStream (java.io.FileOutputStream)6 SecretKey (javax.crypto.SecretKey)6 OutputStream (java.io.OutputStream)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 NullCipher (javax.crypto.NullCipher)5 InvalidKeyException (java.security.InvalidKeyException)4 KeyGenerator (javax.crypto.KeyGenerator)4 BufferedOutputStream (java.io.BufferedOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 AEADBlockCipher (org.bouncycastle.crypto.modes.AEADBlockCipher)3 GCMBlockCipher (org.bouncycastle.crypto.modes.GCMBlockCipher)3