Java Code Examples for javax.crypto.CipherInputStream#close()

The following examples show how to use javax.crypto.CipherInputStream#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CipherInputStreamExceptions.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static void cbc_shortRead400() throws Exception {
    System.out.println("Running cbc_shortRead400");

    // Encrypt 400 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 400);
    // Create stream with encrypted data
    CipherInputStream in = getStream("CBC", ct);

    try {
        in.read();
        in.close();
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
Example 2
Source File: CipherInputStreamExceptions.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
static void cbc_shortRead600() throws Exception {
    System.out.println("Running cbc_shortRead600");

    // Encrypt 600 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 600);
    // Create stream with encrypted data
    CipherInputStream in = getStream("CBC", ct);

    try {
        in.read();
        in.close();
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
Example 3
Source File: CipherInputStreamExceptions.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
static void cbc_shortRead400() throws Exception {
    System.out.println("Running cbc_shortRead400");

    // Encrypt 400 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 400);
    // Create stream with encrypted data
    CipherInputStream in = getStream("CBC", ct);

    try {
        in.read();
        in.close();
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
Example 4
Source File: CipherInputStreamExceptions.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static void cbc_shortStream() throws Exception {
    Cipher c;
    AlgorithmParameters params;
    byte[] read = new byte[200];

    System.out.println("Running cbc_shortStream");

    // Encrypt 97 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 97);
    // Create stream with only 96 bytes of encrypted data
    CipherInputStream in = getStream("CBC", ct, 96);

    try {
        int size = in.read(read);
        in.close();
        if (size != 80) {
            throw new RuntimeException("Fail: CipherInputStream.read() " +
                    "returned " + size + ". Should have been 80");
        }
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
Example 5
Source File: CipherInputStreamExceptions.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static void cbc_shortRead600() throws Exception {
    System.out.println("Running cbc_shortRead600");

    // Encrypt 600 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 600);
    // Create stream with encrypted data
    CipherInputStream in = getStream("CBC", ct);

    try {
        in.read();
        in.close();
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
Example 6
Source File: FileDESUtils.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
/**
 * 解密文件
 *
 * @param in
 */
public void doDecryptFile(InputStream in, String path) {
    if (in == null) {
        System.out.println("inputstream is null");
        return;
    }
    try {
        CipherInputStream cin = new CipherInputStream(in, mDecryptCipher);
        OutputStream outputStream = new FileOutputStream(path);
        byte[] bytes = new byte[1024];
        int length = -1;
        while ((length = cin.read(bytes)) > 0) {
            outputStream.write(bytes, 0, length);
            outputStream.flush();
        }
        cin.close();
        in.close();
        System.out.println("解密成功");
    } catch (Exception e) {
        System.out.println("解密失败");
        e.printStackTrace();
    }
}
 
Example 7
Source File: AesUtil.java    From littleca with Apache License 2.0 6 votes vote down vote up
public static void encryptInputStream(InputStream inputStream, String password, OutputStream out) throws Exception {
    try {
        Cipher cipher = AesHelper.getAesCipher(iv, password.getBytes(), Cipher.ENCRYPT_MODE, CIPHER_MODE);

        CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);
        byte[] buf = new byte[BUFF_SIZE];
        int len = -1;
        while ((len = cipherInputStream.read(buf)) != -1) {
            out.write(buf, 0, len);
            out.flush();
        }
        out.close();
        cipherInputStream.close();
        inputStream.close();
    } catch (Exception e) {
        throw e;
    }
}
 
Example 8
Source File: CipherInputStreamExceptions.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static void cbc_shortRead600() throws Exception {
    System.out.println("Running cbc_shortRead600");

    // Encrypt 600 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 600);
    // Create stream with encrypted data
    CipherInputStream in = getStream("CBC", ct);

    try {
        in.read();
        in.close();
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
Example 9
Source File: CipherInputStreamExceptions.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static void cbc_shortStream() throws Exception {
    Cipher c;
    AlgorithmParameters params;
    byte[] read = new byte[200];

    System.out.println("Running cbc_shortStream");

    // Encrypt 97 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 97);
    // Create stream with only 96 bytes of encrypted data
    CipherInputStream in = getStream("CBC", ct, 96);

    try {
        int size = in.read(read);
        in.close();
        if (size != 80) {
            throw new RuntimeException("Fail: CipherInputStream.read() " +
                    "returned " + size + ". Should have been 80");
        }
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
Example 10
Source File: CipherInputStreamExceptions.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static void gcm_suppressUnreadCorrupt() throws Exception {
    Cipher c;
    byte[] read = new byte[200];

    System.out.println("Running supressUnreadCorrupt test");

    // Encrypt 100 bytes with AES/GCM/PKCS5Padding
    byte[] ct = encryptedText("GCM", 100);
    // Corrupt the encrypted message
    ct = corruptGCM(ct);
    // Create stream for decryption
    CipherInputStream in = getStream("GCM", ct);

    try {
        in.close();
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail: " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
Example 11
Source File: CipherInputStreamExceptions.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static void gcm_suppressUnreadCorrupt() throws Exception {
    Cipher c;
    byte[] read = new byte[200];

    System.out.println("Running supressUnreadCorrupt test");

    // Encrypt 100 bytes with AES/GCM/PKCS5Padding
    byte[] ct = encryptedText("GCM", 100);
    // Corrupt the encrypted message
    ct = corruptGCM(ct);
    // Create stream for decryption
    CipherInputStream in = getStream("GCM", ct);

    try {
        in.close();
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail: " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
Example 12
Source File: CipherInputStreamExceptions.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
static void cbc_shortStream() throws Exception {
    Cipher c;
    AlgorithmParameters params;
    byte[] read = new byte[200];

    System.out.println("Running cbc_shortStream");

    // Encrypt 97 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 97);
    // Create stream with only 96 bytes of encrypted data
    CipherInputStream in = getStream("CBC", ct, 96);

    try {
        int size = in.read(read);
        in.close();
        if (size != 80) {
            throw new RuntimeException("Fail: CipherInputStream.read() " +
                    "returned " + size + ". Should have been 80");
        }
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
Example 13
Source File: CipherInputStreamExceptions.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static void cbc_shortRead400() throws Exception {
    System.out.println("Running cbc_shortRead400");

    // Encrypt 400 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 400);
    // Create stream with encrypted data
    CipherInputStream in = getStream("CBC", ct);

    try {
        in.read();
        in.close();
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
Example 14
Source File: CipherInputStreamExceptions.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void gcm_AEADBadTag() throws Exception {
    Cipher c;
    byte[] read = new byte[200];

    System.out.println("Running gcm_AEADBadTag");

    // Encrypt 100 bytes with AES/GCM/PKCS5Padding
    byte[] ct = encryptedText("GCM", 100);
    // Corrupt the encrypted message
    ct = corruptGCM(ct);
    // Create stream for decryption
    CipherInputStream in = getStream("GCM", ct);

    try {
        int size = in.read(read);
        throw new RuntimeException("Fail: CipherInputStream.read() " +
                "returned " + size + " and didn't throw an exception.");
    } catch (IOException e) {
        Throwable ec = e.getCause();
        if (ec instanceof AEADBadTagException) {
            System.out.println("  Pass.");
        } else {
            System.out.println("  Fail: " + ec.getMessage());
            throw new RuntimeException(ec);
        }
    } finally {
        in.close();
    }
}
 
Example 15
Source File: CipherInputStreamExceptions.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example 16
Source File: CipherInputStreamExceptions.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example 17
Source File: CipherInputStreamExceptions.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static void gcm_AEADBadTag() throws Exception {
    Cipher c;
    byte[] read = new byte[200];

    System.out.println("Running gcm_AEADBadTag");

    // Encrypt 100 bytes with AES/GCM/PKCS5Padding
    byte[] ct = encryptedText("GCM", 100);
    // Corrupt the encrypted message
    ct = corruptGCM(ct);
    // Create stream for decryption
    CipherInputStream in = getStream("GCM", ct);

    try {
        int size = in.read(read);
        throw new RuntimeException("Fail: CipherInputStream.read() " +
                "returned " + size + " and didn't throw an exception.");
    } catch (IOException e) {
        Throwable ec = e.getCause();
        if (ec instanceof AEADBadTagException) {
            System.out.println("  Pass.");
        } else {
            System.out.println("  Fail: " + ec.getMessage());
            throw new RuntimeException(ec);
        }
    } finally {
        in.close();
    }
}
 
Example 18
Source File: CipherInputStreamExceptions.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example 19
Source File: CipherInputStreamExceptions.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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.");
}
 
Example 20
Source File: AESCrypto.java    From weMessage with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * AES CBC decryption of a file byte array
 *
 * @param byteArrayIv the ciphered byte array and iv
 * @param secretKeys the AES & HMAC keys
 * @return The raw decrypted bytes
 * @throws GeneralSecurityException if MACs don't match or AES is not implemented
 * @throws IOException if an error occurs while reading the bytes
 */

public static byte[] decryptFileBytes(CipherByteArrayIv byteArrayIv, SecretKeys secretKeys) throws GeneralSecurityException, IOException {
    Cipher aesCipherForDecryption = Cipher.getInstance(CIPHER_TRANSFORMATION);
    aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secretKeys.getConfidentialityKey(), new IvParameterSpec(byteArrayIv.getIv()));

    ByteArrayInputStream bais = new ByteArrayInputStream(byteArrayIv.getCipherBytes());
    CipherInputStream cipherInputStream = new CipherInputStream(bais, aesCipherForDecryption);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    long byteLength = byteArrayIv.getCipherBytes().length;
    byte[] b = new byte[1024];
    int bytesRead;

    boolean outOfMemTrigger = false;

    while ((bytesRead = cipherInputStream.read(b)) >= 0) {
        if (checkMemoryAvailability.get()) {
            Runtime runtime = Runtime.getRuntime();
            long usedMemory = (runtime.totalMemory() - runtime.freeMemory()) / 1048576L;
            long maxHeapSize = runtime.maxMemory() / 1048576L;
            long availableHeapSize = maxHeapSize - usedMemory;

            if ((byteLength / 1048576L) > (availableHeapSize - 10)) {
                outOfMemTrigger = true;
                break;
            }
        }

        baos.write(b, 0, bytesRead);
    }

    baos.close();
    bais.close();
    cipherInputStream.close();

    if (outOfMemTrigger){
        System.gc();

        byte[] outOfMemoryByteErrorCode = new byte[1];
        Arrays.fill(outOfMemoryByteErrorCode, (byte) Constants.CRYPTO_ERROR_MEMORY);

        return outOfMemoryByteErrorCode;
    }else {
        return baos.toByteArray();
    }
}