use of javax.crypto.CipherOutputStream in project robovm by robovm.
the class CipherOutputStream1Test method testWrite2.
/**
* write(byte[] b) method testing. Tests that method writes correct values
* to the underlying output stream.
*/
public void testWrite2() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestOutputStream tos = new TestOutputStream();
CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
cos.write(data);
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream wrote incorrect data.");
}
try {
cos.write(null);
fail("NullPointerException expected");
} catch (NullPointerException e) {
//expected
}
}
use of javax.crypto.CipherOutputStream in project robovm by robovm.
the class CipherOutputStreamTest method test_close.
/**
* javax.crypto.CipherOutputStream#close()
*/
public void test_close() throws Exception {
// regression test for HARMONY-1139
try {
new CipherOutputStream((OutputStream) null, Cipher.getInstance("DES/CBC/PKCS5Padding")).close();
fail("IllegalStateException expected");
} catch (IllegalStateException e) {
// expected
}
CipherOutputStream ch = new CipherOutputStream((OutputStream) null) {
};
try {
new CipherOutputStream(ch, Cipher.getInstance("DES/CBC/PKCS5Padding")).close();
fail("IllegalStateException expected");
} catch (IllegalStateException e) {
// expected
}
}
use of javax.crypto.CipherOutputStream in project jdk8u_jdk by JetBrains.
the class CipherHelper method encryptData.
void encryptData(WrapToken token, byte[] confounder, byte[] plaintext, int start, int len, byte[] padding, OutputStream os) throws GSSException, IOException {
switch(sealAlg) {
case MessageToken.SEAL_ALG_DES:
// Encrypt on the fly and write
Cipher des = getInitializedDes(true, getDesEncryptionKey(keybytes), ZERO_IV);
CipherOutputStream cos = new CipherOutputStream(os, des);
// debug(getHexBytes(confounder, confounder.length));
cos.write(confounder);
// debug(" " + getHexBytes(plaintext, start, len));
cos.write(plaintext, start, len);
// debug(" " + getHexBytes(padding, padding.length));
cos.write(padding);
break;
case MessageToken.SEAL_ALG_DES3_KD:
byte[] ctext = des3KdEncrypt(confounder, plaintext, start, len, padding);
// Write to stream
os.write(ctext);
break;
case MessageToken.SEAL_ALG_ARCFOUR_HMAC:
byte[] ciphertext = arcFourEncrypt(token, confounder, plaintext, start, len, padding);
// Write to stream
os.write(ciphertext);
break;
default:
throw new GSSException(GSSException.FAILURE, -1, "Unsupported seal algorithm: " + sealAlg);
}
}
use of javax.crypto.CipherOutputStream in project jdk8u_jdk by JetBrains.
the class WrongAAD method decryptWithWrongAAD.
/*
* Attempt to decrypt the cipher text using Cipher object
* initialized with some fake AAD.
*/
private void decryptWithWrongAAD() throws Exception {
System.out.println("decrypt with wrong AAD");
// initialize it with wrong AAD to get an exception during decryption
Cipher decryptCipher = createCipher(Cipher.DECRYPT_MODE, encryptCipher.getParameters());
byte[] someAAD = Helper.generateBytes(AAD_SIZE + 1);
decryptCipher.updateAAD(someAAD);
// init output stream
try (ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decryptCipher)) {
if (decrypt(ciOutput, baOutput)) {
throw new RuntimeException("A decryption has been perfomed successfully in" + " spite of the decrypt Cipher has been" + " initialized with fake AAD");
}
}
System.out.println("Passed");
}
use of javax.crypto.CipherOutputStream in project jdk8u_jdk by JetBrains.
the class WrongAAD method decryptWithEmptyAAD.
/*
* Attempt to decrypt a cipher text using Cipher object
* initialized without AAD used for encryption.
*/
private void decryptWithEmptyAAD() throws Exception {
System.out.println("decryptWithEmptyAAD() started");
// initialize it with empty AAD to get exception during decryption
Cipher decryptCipher = createCipher(Cipher.DECRYPT_MODE, encryptCipher.getParameters());
try (ByteArrayOutputStream baOutput = new ByteArrayOutputStream();
CipherOutputStream ciOutput = new CipherOutputStream(baOutput, decryptCipher)) {
if (decrypt(ciOutput, baOutput)) {
throw new RuntimeException("Decryption has been perfomed successfully in" + " spite of the decrypt Cipher has NOT been" + " initialized with AAD");
}
}
System.out.println("decryptWithEmptyAAD() passed");
}
Aggregations