use of javax.crypto.CipherOutputStream in project robovm by robovm.
the class CipherOutputStream1Test method testCipherOutputStream.
/**
* CipherOutputStream(OutputStream os) method testing. Tests that
* CipherOutputStream uses NullCipher if Cipher is not specified
* in the constructor.
*/
public void testCipherOutputStream() 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) {
};
cos.write(data);
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("NullCipher should be used " + "if Cipher is not specified.");
}
}
use of javax.crypto.CipherOutputStream in project robovm by robovm.
the class CipherOutputStream1Test method test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher.
public void test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56, new SecureRandom());
Key key = kg.generateKey();
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cos = new CipherOutputStream(baos, c);
assertNotNull(cos);
}
use of javax.crypto.CipherOutputStream in project AndroidChromium by JackyAndroid.
the class TabState method saveState.
/**
* Writes the TabState to disk. This method may be called on either the UI or background thread.
* @param file File to write the tab's state to.
* @param state State object obtained from from {@link Tab#getState()}.
* @param encrypted Whether or not the TabState should be encrypted.
*/
public static void saveState(File file, TabState state, boolean encrypted) {
if (state == null || state.contentsState == null) {
return;
}
// Create the byte array from contentsState before opening the FileOutputStream, in case
// contentsState.buffer is an instance of MappedByteBuffer that is mapped to
// the tab state file.
byte[] contentsStateBytes = new byte[state.contentsState.buffer().limit()];
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
state.contentsState.buffer().rewind();
state.contentsState.buffer().get(contentsStateBytes);
} else {
// http://b.android.com/53637.
for (int i = 0; i < state.contentsState.buffer().limit(); i++) {
contentsStateBytes[i] = state.contentsState.buffer().get(i);
}
}
DataOutputStream dataOutputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
if (encrypted) {
Cipher cipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MODE);
if (cipher != null) {
dataOutputStream = new DataOutputStream(new CipherOutputStream(fileOutputStream, cipher));
} else {
// better than failing to provide the guarantee of Incognito Mode.
return;
}
} else {
dataOutputStream = new DataOutputStream(fileOutputStream);
}
if (encrypted) {
dataOutputStream.writeLong(KEY_CHECKER);
}
dataOutputStream.writeLong(state.timestampMillis);
dataOutputStream.writeInt(contentsStateBytes.length);
dataOutputStream.write(contentsStateBytes);
dataOutputStream.writeInt(state.parentId);
dataOutputStream.writeUTF(state.openerAppId != null ? state.openerAppId : "");
dataOutputStream.writeInt(state.contentsState.version());
dataOutputStream.writeLong(state.syncId);
dataOutputStream.writeBoolean(state.shouldPreserve);
dataOutputStream.writeInt(state.themeColor);
} catch (FileNotFoundException e) {
Log.w(TAG, "FileNotFoundException while attempting to save TabState.");
} catch (IOException e) {
Log.w(TAG, "IOException while attempting to save TabState.");
} finally {
StreamUtil.closeQuietly(dataOutputStream);
StreamUtil.closeQuietly(fileOutputStream);
}
}
use of javax.crypto.CipherOutputStream in project AndroidChromium by JackyAndroid.
the class CookiesFetcher method saveFetchedCookiesToDisk.
private void saveFetchedCookiesToDisk(CanonicalCookie[] cookies) {
DataOutputStream out = null;
try {
Cipher cipher = CipherFactory.getInstance().getCipher(Cipher.ENCRYPT_MODE);
if (cipher == null) {
// Something is wrong. Can't encrypt, don't save cookies.
return;
}
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
CipherOutputStream cipherOut = new CipherOutputStream(byteOut, cipher);
out = new DataOutputStream(cipherOut);
CanonicalCookie.saveListToStream(out, cookies);
out.close();
ImportantFileWriterAndroid.writeFileAtomically(fetchFileName(mContext), byteOut.toByteArray());
out = null;
} catch (IOException e) {
Log.w(TAG, "IOException during Cookie Fetch");
} catch (Throwable t) {
Log.w(TAG, "Error storing cookies.", t);
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
Log.w(TAG, "IOException during Cookie Fetch");
}
}
}
use of javax.crypto.CipherOutputStream in project jdk8u_jdk by JetBrains.
the class CipherStreamClose method streamEncrypt.
public static byte[] streamEncrypt(String message, SecretKey key, MessageDigest digest) throws Exception {
byte[] data;
Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
encCipher.init(Cipher.ENCRYPT_MODE, key);
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
DigestOutputStream dos = new DigestOutputStream(bos, digest);
CipherOutputStream cos = new CipherOutputStream(dos, encCipher)) {
try (ObjectOutputStream oos = new ObjectOutputStream(cos)) {
oos.writeObject(message);
}
data = bos.toByteArray();
}
if (debug) {
System.out.println(DatatypeConverter.printHexBinary(data));
}
return data;
}
Aggregations