本文整理汇总了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;
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
}
示例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;
}
示例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());
}
}
示例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());
}
}
示例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());
}
}
示例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();
}
示例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();
}
示例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);
}
}
示例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;
}
}
示例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);
}
}
示例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.");
}
}
示例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.");
}
}