use of javax.crypto.Cipher in project wycheproof by google.
the class AesGcmTest method testByteBufferTooShort.
public void testByteBufferTooShort() throws Exception {
for (GcmTestVector test : getTestVectors()) {
// Encryption
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
ByteBuffer ptBuffer = ByteBuffer.wrap(test.pt);
ByteBuffer ctBuffer = ByteBuffer.allocate(test.ct.length - 1);
cipher.init(Cipher.ENCRYPT_MODE, test.key, test.parameters);
cipher.updateAAD(test.aad);
try {
cipher.doFinal(ptBuffer, ctBuffer);
fail("This should not work");
} catch (ShortBufferException ex) {
// expected
}
// Decryption
ctBuffer = ByteBuffer.wrap(test.ct);
ByteBuffer decrypted = ByteBuffer.allocate(test.pt.length - 1);
cipher.init(Cipher.DECRYPT_MODE, test.key, test.parameters);
cipher.updateAAD(test.aad);
try {
cipher.doFinal(ctBuffer, decrypted);
fail("This should not work");
} catch (ShortBufferException ex) {
// expected
}
}
}
use of javax.crypto.Cipher in project wycheproof by google.
the class CipherInputStreamTest method testDecrypt.
/** JDK-8016249: CipherInputStream in decrypt mode fails on close with AEAD ciphers */
@SuppressWarnings("InsecureCryptoUsage")
public void testDecrypt(Iterable<TestVector> tests) throws Exception {
for (TestVector t : tests) {
Cipher cipher = Cipher.getInstance(t.algorithm);
cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
cipher.updateAAD(t.aad);
InputStream is = new ByteArrayInputStream(t.ct);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] result = new byte[t.pt.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());
cis.close();
assertEquals(TestUtil.bytesToHex(t.pt), TestUtil.bytesToHex(result));
}
}
use of javax.crypto.Cipher in project wycheproof by google.
the class CipherOutputStreamTest method testCorruptDecryptEmpty.
@SuppressWarnings("InsecureCryptoUsage")
public void testCorruptDecryptEmpty(Iterable<TestVector> tests) throws Exception {
for (TestVector t : tests) {
Cipher cipher = Cipher.getInstance(t.algorithm);
cipher.init(Cipher.DECRYPT_MODE, t.key, t.params);
cipher.updateAAD(t.aad);
byte[] ct = Arrays.copyOf(t.ct, t.ct.length);
ct[ct.length - 1] ^= (byte) 1;
ByteArrayOutputStream os = new ByteArrayOutputStream();
CipherOutputStream cos = new CipherOutputStream(os, cipher);
cos.write(ct);
try {
// cos.close() should call cipher.doFinal().
cos.close();
byte[] decrypted = os.toByteArray();
fail("this should fail; decrypted:" + TestUtil.bytesToHex(decrypted) + " pt: " + TestUtil.bytesToHex(t.pt));
} catch (IOException ex) {
// expected
}
}
}
use of javax.crypto.Cipher in project keywhiz by square.
the class ContentCryptographer method gcm.
private byte[] gcm(Mode mode, String info, byte[] nonce, byte[] data) {
try {
Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, encryptionProvider);
SecretKey derivedKey = deriveKey(cipher.getBlockSize(), info);
GCMParameterSpec gcmParameters = new GCMParameterSpec(TAG_BITS, nonce);
cipher.init(mode.cipherMode, derivedKey, gcmParameters);
return cipher.doFinal(data);
} catch (IllegalBlockSizeException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException e) {
throw Throwables.propagate(e);
}
}
use of javax.crypto.Cipher in project android-delicious by lexs.
the class ObscuredSharedPreferences method decrypt.
protected String decrypt(String value) {
try {
final byte[] bytes = value != null ? Base64.decode(value, Base64.DEFAULT) : new byte[0];
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(secret));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(getSalt(), 20));
return new String(pbeCipher.doFinal(bytes), UTF8);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations