當前位置: 首頁>>代碼示例>>Java>>正文


Java CipherInputStream.read方法代碼示例

本文整理匯總了Java中javax.crypto.CipherInputStream.read方法的典型用法代碼示例。如果您正苦於以下問題:Java CipherInputStream.read方法的具體用法?Java CipherInputStream.read怎麽用?Java CipherInputStream.read使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.crypto.CipherInputStream的用法示例。


在下文中一共展示了CipherInputStream.read方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: readByte

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
@Override
int readByte(CipherInputStream ciIn2, byte[] outputText, int save,
        int index) throws IOException {
    int len1 = ciIn2.read(outputText, index, save);
    out.println("Init: index=" + index + ",len=" + len1);
    // read more until save bytes
    index += len1;
    int len2 = 0;
    while (len1 != save && len2 != -1) {
        len2 = ciIn2.read(outputText, index, save - len1);
        out.println("Cont: index=" + index + ",len=" + len2);
        len1 += len2;
        index += len2;
    }
    return index;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:17,代碼來源:CICOSkipTest.java

示例2: proceedSkipTestUsingByteArrayBufferingType

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
/**
 * Implements byte array buffering type test case of the CICO SKIP test.
 *
 * @param blockNum block number to read.
 */
private void proceedSkipTestUsingByteArrayBufferingType(
        CipherInputStream ciIn2, int blockNum) throws IOException {
    int index = blockNum * SAVE;
    int len1 = ciIn2.read(outputText, index, SAVE);
    // read more until SAVE bytes
    index += len1;
    int len2 = 0;
    int totalRead = len1;
    while (len1 != SAVE && len2 != -1) {
        len2 = ciIn2.read(outputText, index, SAVE - len1);
        len1 += len2;
        index += len2;
        totalRead += len2;
    }
    if (totalRead != SAVE) {
        throw new RuntimeException("Read bytes number " + totalRead
                + " does not equal to given number " + SAVE);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:25,代碼來源:CICO_PBE_SKIP_Test.java

示例3: proceedSkipTestUsingIntBufferingType

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
/**
 * Implements int buffering type test case of the CICO SKIP test.
 *
 * @param blockNum block number to read.
 */
private void proceedSkipTestUsingIntBufferingType(CipherInputStream ciIn2,
        int blockNum) throws IOException {
    int index = blockNum * SAVE;
    int totalRead = 0;
    for (int j = 0; j < SAVE; j++, index++) {
        int buffer0 = ciIn2.read();
        if (buffer0 != -1) {
            outputText[index] = (byte) buffer0;
            totalRead++;
        } else {
            break;
        }
    }
    if (totalRead != SAVE) {
        throw new RuntimeException("Read bytes number " + totalRead
                + " does not equal to given number " + SAVE);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:24,代碼來源:CICO_PBE_SKIP_Test.java

示例4: gcm_oneReadByteCorrupt

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
static void gcm_oneReadByteCorrupt() throws Exception {

        System.out.println("Running gcm_oneReadByteCorrupt 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.read();
            System.out.println("  Fail. No exception thrown.");
        } 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);
            }
        }
    }
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,代碼來源:CipherInputStreamExceptions.java

示例5: enfile_read

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
public static String enfile_read(String file_name, byte[] key) throws Exception {
    file = new File(file_name);
    bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
    
    secretKey = new SecretKeySpec(key, "AES");
    cipher = Cipher.getInstance(CIPHER_ALOGORTHM);
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    cipherInputStream = new CipherInputStream(bufferedInputStream, cipher);

    String str = "";

    while((length = cipherInputStream.read(cache)) > 0) {
        str += new String(cache, 0, length);
    }
    return str;
}
 
開發者ID:48763,項目名稱:File-Cipher,代碼行數:17,代碼來源:FileCipherStream.java

示例6: cbc_shortRead600

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
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());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:CipherInputStreamExceptions.java

示例7: cbc_shortStream

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
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());
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:26,代碼來源:CipherInputStreamExceptions.java

示例8: cbc_shortRead400

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
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());
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:18,代碼來源:CipherInputStreamExceptions.java

示例9: decryptLarge

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
public static void decryptLarge(String password, File in, File out) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(password), "AES");
    Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);

    FileInputStream inputStream = new FileInputStream(in);
    FileOutputStream fileOutputStream = new FileOutputStream(out);

    int read;
    byte[] buffer = new byte[4096];

    CipherInputStream cis = new CipherInputStream(inputStream, cipher);

    while ((read = cis.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, read);
    }
    fileOutputStream.close();
    cis.close();
    in.delete();
}
 
開發者ID:alepacheco,項目名稱:AndroRW,代碼行數:21,代碼來源:Aes.java

示例10: encryptLarge

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
public static void encryptLarge(String password, File in, File out) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(password), "AES");
    Cipher cipher = Cipher.getInstance("AES/CTR/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    FileInputStream inputStream = new FileInputStream(in);
    FileOutputStream fileOutputStream = new FileOutputStream(out);

    int read;
    byte[] buffer = new byte[4096];

    CipherInputStream cis = new CipherInputStream(inputStream, cipher);

    while ((read = cis.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, read);
    }
    fileOutputStream.close();
    cis.close();
    in.delete();
}
 
開發者ID:alepacheco,項目名稱:AndroRW,代碼行數:21,代碼來源:Aes.java

示例11: crypt

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
private void crypt(InputStream in, OutputStream out, byte[] keyBytes, byte[] iv, int cryptMode) throws CryptoException {
    if (in == null) {
        throw new NullPointerException("InputStream argument cannot be null.");
    }
    if (out == null) {
        throw new NullPointerException("OutputStream argument cannot be null.");
    }

    javax.crypto.Cipher cipher = initNewCipher(cryptMode, keyBytes, iv, true);

    CipherInputStream cis = new CipherInputStream(in, cipher);

    int bufSize = getStreamingBufferSize();
    byte[] buffer = new byte[bufSize];

    int bytesRead;
    try {
        while ((bytesRead = cis.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        throw new CryptoException(e);
    }
}
 
開發者ID:xuegongzi,項目名稱:rabbitframework,代碼行數:25,代碼來源:JcaCipherService.java

示例12: encrypt

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
public boolean encrypt(String file, String destFile,Key key) {
    try {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        InputStream is = new FileInputStream(file);
        OutputStream out = new FileOutputStream(destFile);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        byte[] buffer = new byte[1024];
        int r;
        while ((r = cis.read(buffer)) > 0) {
            out.write(buffer, 0, r);
        }
        cis.close();
        is.close();
        out.close();
        return true;
    }catch (Exception e){
        e.printStackTrace();
        return false;
    }

}
 
開發者ID:tztztztztz,項目名稱:bookish-meme,代碼行數:23,代碼來源:FileEncryptAndDecrypt.java

示例13: decryptBytes

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
private String decryptBytes(Key key, byte[] bytes) throws CryptoFailedException {
    try {
        Cipher cipher = Cipher.getInstance(ENCRYPTION_TRANSFORMATION);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        // read the initialization vector from the beginning of the stream
        IvParameterSpec ivParams = readIvFromStream(inputStream);
        cipher.init(Cipher.DECRYPT_MODE, key, ivParams);
        // decrypt the bytes using a CipherInputStream
        CipherInputStream cipherInputStream = new CipherInputStream(
                inputStream, cipher);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        while (true) {
            int n = cipherInputStream.read(buffer, 0, buffer.length);
            if (n <= 0) {
                break;
            }
            output.write(buffer, 0, n);
        }
        return new String(output.toByteArray(), Charset.forName("UTF-8"));
    } catch (Exception e) {
        throw new CryptoFailedException("Could not decrypt bytes", e);
    }
}
 
開發者ID:oblador,項目名稱:react-native-keychain,代碼行數:25,代碼來源:CipherStorageKeystoreAESCBC.java

示例14: testCipherInputStream

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
/**
 * CipherInputStream(InputStream is) method testing. Tests that
 * CipherInputStream uses NullCipher if Cipher is not specified
 * in the constructor.
 */
@TestTargetNew(
    level = TestLevel.COMPLETE,
    notes = "",
    method = "CipherInputStream",
    args = {java.io.InputStream.class}
)
public void testCipherInputStream() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestInputStream tis = new TestInputStream(data);
    CipherInputStream cis = new CipherInputStream(tis){};

    for (int i = 0; i < data.length; i++) {
        if ((byte) cis.read() != data[i]) {
            fail("NullCipher should be used "
                    + "if Cipher is not specified.");
        }
    }
    if (cis.read() != -1) {
        fail("NullCipher should be used if Cipher is not specified.");
    }
}
 
開發者ID:keplersj,項目名稱:In-the-Box-Fork,代碼行數:27,代碼來源:CipherInputStream1Test.java

示例15: testRead1

import javax.crypto.CipherInputStream; //導入方法依賴的package包/類
/**
 * read() method testing. Tests that method returns the correct value
 * (related to the InputStream) and that it returns -1 at the end of stream.
 */
@TestTargetNew(
    level = TestLevel.COMPLETE,
    notes = "Can not check IOException.",
    method = "read",
    args = {}
)
public void testRead1() throws Exception {
    byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
    TestInputStream tis = new TestInputStream(data);
    CipherInputStream cis = new CipherInputStream(tis, new NullCipher());
    byte res;
    for (int i = 0; i < data.length; i++) {
        if ((res = (byte) cis.read()) != data[i]) {
            fail("read() returned the incorrect value. " + "Expected: "
                    + data[i] + ", Got: " + res + ".");
        }
    }
    if (cis.read() != -1) {
        fail("read() should return -1 at the end of the stream.");
    }
}
 
開發者ID:keplersj,項目名稱:In-the-Box-Fork,代碼行數:26,代碼來源:CipherInputStream1Test.java


注:本文中的javax.crypto.CipherInputStream.read方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。