use of javax.crypto.CipherInputStream in project wycheproof by google.
the class CipherInputStreamTest method testEncrypt.
@SuppressWarnings("InsecureCryptoUsage")
public void testEncrypt(Iterable<TestVector> tests) throws Exception {
for (TestVector t : tests) {
Cipher cipher = Cipher.getInstance(t.algorithm);
cipher.init(Cipher.ENCRYPT_MODE, t.key, t.params);
cipher.updateAAD(t.aad);
InputStream is = new ByteArrayInputStream(t.pt);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] result = new byte[t.ct.length];
int totalLength = 0;
int length = 0;
do {
length = cis.read(result, totalLength, result.length - totalLength);
if (length > 0) {
totalLength += length;
}
} while (length >= 0 && totalLength != result.length);
assertEquals(-1, cis.read());
assertEquals(TestUtil.bytesToHex(t.ct), TestUtil.bytesToHex(result));
cis.close();
}
}
use of javax.crypto.CipherInputStream in project openolat by klemens.
the class PersistedProperties method loadPropertiesFromFile.
/**
* Load the persisted properties from disk. This can be useful when your
* code gets a PersistedPropertiesChangedEvent and you just want to reload
* the property instead of modifying the one you have already loaded.
*/
public void loadPropertiesFromFile() {
// Might get an event after beeing disposed. Should not be the case, but you never know with multi user events accross nodes.
if (propertiesChangedEventListener != null && configurationPropertiesFile.exists()) {
InputStream is;
try {
is = new FileInputStream(configurationPropertiesFile);
if (secured) {
SecretKey key = generateKey("rk6R9pQy7dg3usJk");
Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
cipher.init(Cipher.DECRYPT_MODE, key, generateIV(cipher), random);
is = new CipherInputStream(is, cipher);
}
configuredProperties.load(is);
is.close();
} catch (FileNotFoundException e) {
logError("Could not load config file from path::" + configurationPropertiesFile.getAbsolutePath(), e);
} catch (IOException e) {
logError("Could not load config file from path::" + configurationPropertiesFile.getAbsolutePath(), e);
} catch (Exception e) {
logError("Could not load config file from path::" + configurationPropertiesFile.getAbsolutePath(), e);
}
}
}
use of javax.crypto.CipherInputStream in project RxCache by VictorAlbertos.
the class BuiltInEncryptor method encrypt.
@Override
public void encrypt(String key, File decryptedFile, File encryptedFile) {
initCiphers(key);
try {
CipherInputStream cis = new CipherInputStream(new FileInputStream(decryptedFile), encryptCipher);
write(cis, new FileOutputStream(encryptedFile));
} catch (Exception e) {
e.printStackTrace();
}
}
use of javax.crypto.CipherInputStream in project Signal-Android by signalapp.
the class ModernDecryptingPartInputStream method createFor.
private static InputStream createFor(@NonNull AttachmentSecret attachmentSecret, @NonNull byte[] random, @NonNull InputStream inputStream, long offset) throws IOException {
try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(attachmentSecret.getModernKey(), "HmacSHA256"));
byte[] iv = new byte[16];
int remainder = (int) (offset % 16);
Conversions.longTo4ByteArray(iv, 12, offset / 16);
byte[] key = mac.doFinal(random);
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
long skipped = inputStream.skip(offset - remainder);
if (skipped != offset - remainder) {
throw new IOException("Skip failed: " + skipped + " vs " + (offset - remainder));
}
CipherInputStream cipherInputStream = new CipherInputStream(inputStream, cipher);
byte[] remainderBuffer = new byte[remainder];
readFully(cipherInputStream, remainderBuffer);
return cipherInputStream;
} catch (NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException e) {
throw new AssertionError(e);
}
}
use of javax.crypto.CipherInputStream in project Signal-Android by signalapp.
the class EncryptedCoder method createEncryptedInputStream.
CipherInputStream createEncryptedInputStream(@NonNull byte[] masterKey, @NonNull File file) throws IOException {
try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(masterKey, "HmacSHA256"));
FileInputStream fileInputStream = new FileInputStream(file);
byte[] theirMagic = new byte[MAGIC_BYTES.length];
byte[] theirRandom = new byte[32];
byte[] theirEncryptedMagic = new byte[MAGIC_BYTES.length];
StreamUtil.readFully(fileInputStream, theirMagic);
StreamUtil.readFully(fileInputStream, theirRandom);
if (!MessageDigest.isEqual(theirMagic, MAGIC_BYTES)) {
throw new IOException("Not an encrypted cache file!");
}
byte[] iv = new byte[16];
byte[] key = mac.doFinal(theirRandom);
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
CipherInputStream inputStream = new CipherInputStream(fileInputStream, cipher);
StreamUtil.readFully(inputStream, theirEncryptedMagic);
if (!MessageDigest.isEqual(theirEncryptedMagic, MAGIC_BYTES)) {
throw new IOException("Key change on encrypted cache file!");
}
return inputStream;
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException e) {
throw new AssertionError(e);
}
}
Aggregations