本文整理汇总了Java中javax.crypto.CipherInputStream.close方法的典型用法代码示例。如果您正苦于以下问题:Java CipherInputStream.close方法的具体用法?Java CipherInputStream.close怎么用?Java CipherInputStream.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.crypto.CipherInputStream
的用法示例。
在下文中一共展示了CipherInputStream.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: gcm_suppressUnreadCorrupt
import javax.crypto.CipherInputStream; //导入方法依赖的package包/类
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());
}
}
示例2: 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());
}
}
示例3: 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());
}
}
示例4: 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());
}
}
示例5: 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();
}
示例6: 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();
}
示例7: 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;
}
}
示例8: testInputStream
import javax.crypto.CipherInputStream; //导入方法依赖的package包/类
@Test
public void testInputStream() throws Exception {
AES aes = new AES();
String orig = "I'm a password, really";
ByteArrayInputStream bais = new ByteArrayInputStream(orig.getBytes());
CipherInputStream cis = aes.inputStream(bais, true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Symm.base64.encode(cis, baos);
cis.close();
byte[] b64encrypted;
System.out.println(new String(b64encrypted=baos.toByteArray()));
baos.reset();
CipherOutputStream cos = aes.outputStream(baos, false);
Symm.base64.decode(new ByteArrayInputStream(b64encrypted),cos);
cos.close();
Assert.assertEquals(orig, new String(baos.toByteArray()));
}
示例9: writeTo
import javax.crypto.CipherInputStream; //导入方法依赖的package包/类
@Override
public void writeTo(BufferedSink sink) throws IOException {
//The only time this can cause issues is if the body has disappeared since construction. Don't worry about that, since
//it'll get caught when we initialize.
CipherInputStream cis = new CipherInputStream(new FileInputStream(file), cipher);
try {
StreamsUtil.writeFromInputToOutputUnmanaged(cis, sink.outputStream());
} catch (InputIOException iioe) {
//Here we want to retain the fundamental problem of the _input_ being responsible for the issue
//so we can differentiate between bad reads and bad network
throw iioe;
} catch (OutputIOException oe) {
//We want the original exception here.
throw oe.getWrapped();
} finally {
cis.close();
}
}
示例10: decode
import javax.crypto.CipherInputStream; //导入方法依赖的package包/类
public static byte[] decode(InputStream is, String pass) throws Exception
{
Cipher c = Cipher.getInstance("DES");
Key k = new SecretKeySpec(pass.getBytes(), "DES");
c.init(Cipher.DECRYPT_MODE, k);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
CipherInputStream cis = new CipherInputStream(is, c);
for (int b; (b = cis.read()) != -1; b++) {
bos.write(b);
}
cis.close();
return bos.toByteArray();
}
示例11: gcm_AEADBadTag
import javax.crypto.CipherInputStream; //导入方法依赖的package包/类
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();
}
}
示例12: gcm_shortReadAEAD
import javax.crypto.CipherInputStream; //导入方法依赖的package包/类
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.");
}
示例13: encrypt
import javax.crypto.CipherInputStream; //导入方法依赖的package包/类
private static byte[] encrypt(byte[] key, byte[] IV, byte[] data) throws InvalidAlgorithmParameterException, InvalidKeyException, IOException
{
SecretKey aesKey = new SecretKeySpec(key, 0, key.length, "AES");
// Encrypt cipher
Cipher encryptCipher = null;
try {
encryptCipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
} catch (Exception e) {
e.printStackTrace();
return null;
}
encryptCipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(IV));
ByteArrayInputStream inStream = new ByteArrayInputStream(data);
CipherInputStream cipherInputStream = new CipherInputStream(inStream, encryptCipher);
byte[] buf = new byte[1024];
int bytesRead;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while ((bytesRead = cipherInputStream.read(buf)) > 0) {
outputStream.write(buf, 0, bytesRead);
}
byte[] res = outputStream.toByteArray();
cipherInputStream.close();
inStream.close();
outputStream.close();
return res;
}
示例14: decrypt
import javax.crypto.CipherInputStream; //导入方法依赖的package包/类
private static byte[] decrypt(byte[] key, byte[] IV, byte[] encryptedBytes) throws InvalidAlgorithmParameterException, InvalidKeyException, IOException
{
SecretKey aesKey = new SecretKeySpec(key, 0, key.length, "AES");
// Decrypt cipher
Cipher decryptCipher = null;
try {
decryptCipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
} catch (Exception e) {
e.printStackTrace();
return null;
}
decryptCipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(IV));
// Decrypt
ByteArrayInputStream inStream = new ByteArrayInputStream(encryptedBytes);
CipherInputStream cipherInputStream = new CipherInputStream(inStream, decryptCipher);
byte[] buf = new byte[1024];
int bytesRead;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while ((bytesRead = cipherInputStream.read(buf)) > 0) {
outputStream.write(buf, 0, bytesRead);
}
byte[] res = outputStream.toByteArray();
cipherInputStream.close();
inStream.close();
outputStream.close();
return res;
}
示例15: decode
import javax.crypto.CipherInputStream; //导入方法依赖的package包/类
public static byte[] decode(InputStream is, String pass) throws Exception {
Cipher c = Cipher.getInstance("DES");
Key k = new SecretKeySpec(pass.getBytes(), "DES");
c.init(Cipher.DECRYPT_MODE, k);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
CipherInputStream cis = new CipherInputStream(is, c);
for (int b = 0; b != - 1; b = cis.read()) {
bos.write(b);
}
cis.close();
return bos.toByteArray();
}