javax.crypto.CipherInputStream#close ( )源码实例Demo

下面列出了javax.crypto.CipherInputStream#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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());
    }
}
 
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());
    }
}
 
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());
    }
}
 
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());
    }
}
 
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());
    }
}
 
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());
    }
}
 
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());
    }
}
 
源代码8 项目: littleca   文件: AesUtil.java
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;
    }
}
 
源代码9 项目: YCAudioPlayer   文件: FileDESUtils.java
/**
 * 解密文件
 *
 * @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();
    }
}
 
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());
    }
}
 
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());
    }
}
 
源代码12 项目: jdk8u60   文件: CipherInputStreamExceptions.java
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());
    }
}
 
源代码13 项目: jdk8u60   文件: CipherInputStreamExceptions.java
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());
    }
}
 
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();
    }
}
 
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.");
}
 
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.");
}
 
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.");
}
 
源代码18 项目: jdk8u60   文件: CipherInputStreamExceptions.java
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();
    }
}
 
源代码19 项目: jdk8u60   文件: CipherInputStreamExceptions.java
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.");
}
 
源代码20 项目: weMessage   文件: AESCrypto.java
/**
 * 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();
    }
}
 
 同类方法