use of javax.crypto.CipherInputStream in project apjp by jvansteirteghem.
the class HTTPSServlet method doPost.
public void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
try {
httpServletResponse.setStatus(200);
for (int i = 0; i < APJP_REMOTE_HTTPS_SERVER_RESPONSE_PROPERTY_KEY.length; i = i + 1) {
if (APJP_REMOTE_HTTPS_SERVER_RESPONSE_PROPERTY_KEY[i].equalsIgnoreCase("") == false) {
httpServletResponse.addHeader(APJP_REMOTE_HTTPS_SERVER_RESPONSE_PROPERTY_KEY[i], APJP_REMOTE_HTTPS_SERVER_RESPONSE_PROPERTY_VALUE[i]);
}
}
SecretKeySpec secretKeySpec = new SecretKeySpec(APJP_KEY.getBytes(), "ARCFOUR");
Cipher inputStreamCipher = Cipher.getInstance("ARCFOUR");
inputStreamCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
CipherInputStream httpRequestInputStream = new CipherInputStream(httpServletRequest.getInputStream(), inputStreamCipher);
Cipher outputStreamCipher = Cipher.getInstance("ARCFOUR");
outputStreamCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
CipherOutputStream httpResponseOutputStream = new CipherOutputStream(httpServletResponse.getOutputStream(), outputStreamCipher);
HTTPRequestMessage httpRequestMessage1 = new HTTPRequestMessage(httpRequestInputStream);
httpRequestMessage1.read();
HTTPSRequest httpsRequest1 = new HTTPSRequest(httpRequestMessage1);
httpsRequest1.open();
try {
HTTPResponseMessage httpResponseMessage1 = httpsRequest1.getHTTPResponseMessage();
HTTPMessageHeader[] httpResponseMessage1Headers1 = httpResponseMessage1.getHTTPMessageHeaders();
HTTPMessageHeader httpResponseMessage1Header1 = httpResponseMessage1Headers1[0];
String httpResponseMessage1Header1Key1 = httpResponseMessage1Header1.getKey();
String httpResponseMessage1Header1Value1 = httpResponseMessage1Header1.getValue();
httpResponseOutputStream.write((httpResponseMessage1Header1Value1 + "\r\n").getBytes());
for (int i = 1; i < httpResponseMessage1Headers1.length; i = i + 1) {
httpResponseMessage1Header1 = httpResponseMessage1Headers1[i];
httpResponseMessage1Header1Key1 = httpResponseMessage1Header1.getKey();
httpResponseMessage1Header1Value1 = httpResponseMessage1Header1.getValue();
httpResponseOutputStream.write((httpResponseMessage1Header1Key1 + ": " + httpResponseMessage1Header1Value1 + "\r\n").getBytes());
}
httpResponseOutputStream.write(("\r\n").getBytes());
httpResponseMessage1.read(httpResponseOutputStream);
} catch (Exception e) {
throw e;
} finally {
try {
httpsRequest1.close();
} catch (Exception e) {
}
}
} catch (Exception e) {
logger.log(Level.INFO, "EXCEPTION", e);
httpServletResponse.setStatus(500);
}
}
use of javax.crypto.CipherInputStream in project apjp by jvansteirteghem.
the class HTTPServlet method doPost.
public void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
try {
httpServletResponse.setStatus(200);
for (int i = 0; i < APJP_REMOTE_HTTP_SERVER_RESPONSE_PROPERTY_KEY.length; i = i + 1) {
if (APJP_REMOTE_HTTP_SERVER_RESPONSE_PROPERTY_KEY[i].equalsIgnoreCase("") == false) {
httpServletResponse.addHeader(APJP_REMOTE_HTTP_SERVER_RESPONSE_PROPERTY_KEY[i], APJP_REMOTE_HTTP_SERVER_RESPONSE_PROPERTY_VALUE[i]);
}
}
SecretKeySpec secretKeySpec = new SecretKeySpec(APJP_KEY.getBytes(), "ARCFOUR");
Cipher inputStreamCipher = Cipher.getInstance("ARCFOUR");
inputStreamCipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
CipherInputStream httpRequestInputStream = new CipherInputStream(httpServletRequest.getInputStream(), inputStreamCipher);
Cipher outputStreamCipher = Cipher.getInstance("ARCFOUR");
outputStreamCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
CipherOutputStream httpResponseOutputStream = new CipherOutputStream(httpServletResponse.getOutputStream(), outputStreamCipher);
HTTPRequestMessage httpRequestMessage1 = new HTTPRequestMessage(httpRequestInputStream);
httpRequestMessage1.read();
HTTPRequest httpRequest1 = new HTTPRequest(httpRequestMessage1);
httpRequest1.open();
try {
HTTPResponseMessage httpResponseMessage1 = httpRequest1.getHTTPResponseMessage();
HTTPMessageHeader[] httpResponseMessage1Headers1 = httpResponseMessage1.getHTTPMessageHeaders();
HTTPMessageHeader httpResponseMessage1Header1 = httpResponseMessage1Headers1[0];
String httpResponseMessage1Header1Key1 = httpResponseMessage1Header1.getKey();
String httpResponseMessage1Header1Value1 = httpResponseMessage1Header1.getValue();
httpResponseOutputStream.write((httpResponseMessage1Header1Value1 + "\r\n").getBytes());
for (int i = 1; i < httpResponseMessage1Headers1.length; i = i + 1) {
httpResponseMessage1Header1 = httpResponseMessage1Headers1[i];
httpResponseMessage1Header1Key1 = httpResponseMessage1Header1.getKey();
httpResponseMessage1Header1Value1 = httpResponseMessage1Header1.getValue();
httpResponseOutputStream.write((httpResponseMessage1Header1Key1 + ": " + httpResponseMessage1Header1Value1 + "\r\n").getBytes());
}
httpResponseOutputStream.write(("\r\n").getBytes());
httpResponseMessage1.read(httpResponseOutputStream);
} catch (Exception e) {
throw e;
} finally {
try {
httpRequest1.close();
} catch (Exception e) {
}
}
} catch (Exception e) {
logger.log(Level.INFO, "EXCEPTION", e);
httpServletResponse.setStatus(500);
}
}
use of javax.crypto.CipherInputStream in project ontrack by nemerosa.
the class FileConfidentialStore method load.
/**
* Reverse operation of {@link #store(String, byte[])}
*
* @return null the data has not been previously persisted.
*/
@Override
public byte[] load(String key) throws IOException {
CipherInputStream cis = null;
FileInputStream fis = null;
try {
File f = getFileFor(key);
if (!f.exists())
return null;
Cipher sym = Cipher.getInstance("AES");
sym.init(Cipher.DECRYPT_MODE, masterKey);
cis = new CipherInputStream(fis = new FileInputStream(f), sym);
byte[] bytes = IOUtils.toByteArray(cis);
return verifyMagic(bytes);
} catch (GeneralSecurityException e) {
throw new IOException("Failed to persist the key: " + key, e);
} finally {
IOUtils.closeQuietly(cis);
IOUtils.closeQuietly(fis);
}
}
use of javax.crypto.CipherInputStream in project lobcder by skoulouzis.
the class DesEncrypter method decrypt.
public void decrypt(InputStream in, OutputStream out) throws IOException {
InputStream cipherIn = null;
try {
int read;
cipherIn = new CipherInputStream(in, dcipher);
byte[] copyBuffer = new byte[Constants.BUF_SIZE];
while ((read = cipherIn.read(copyBuffer, 0, copyBuffer.length)) != -1) {
out.write(copyBuffer, 0, read);
}
} finally {
try {
cipherIn.close();
} finally {
out.close();
}
}
// try {
// cipherIn = new CipherInputStream(in, dcipher);
// CircularStreamBufferTransferer cBuff = new CircularStreamBufferTransferer((Constants.BUF_SIZE), cipherIn, out);
// cBuff.startTransfer(new Long(-1));
// } catch (Exception ex) {
// throw new IOException(ex);
// } finally {
// if (out != null) {
// try {
// out.flush();
// out.close();
// } catch (java.io.IOException ex) {
// }
// }
// if (cipherIn != null) {
// cipherIn.close();
// }
// }
}
use of javax.crypto.CipherInputStream in project wycheproof by google.
the class CipherInputStreamTest method testCorruptDecrypt.
/**
* JDK-8016171 : CipherInputStream masks ciphertext tampering with AEAD ciphers in decrypt mode
* Further description of the bug is here:
* https://blog.heckel.xyz/2014/03/01/cipherinputstream-for-aead-modes-is-broken-in-jdk7-gcm/
* BouncyCastle claims that this bug is fixed in version 1.51. However, the test below still fails
* with BouncyCastle v 1.52. A possible explanation is that BouncyCastle has its own
* implemenatation of CipherInputStream (org.bouncycastle.crypto.io.CipherInputStream).
*
* @param tests an iterable with valid test vectors, that will be corrupted for the test
* @param acceptEmptyPlaintext determines whether an empty plaintext instead of an exception
* is acceptable.
*/
@SuppressWarnings("InsecureCryptoUsage")
public void testCorruptDecrypt(Iterable<TestVector> tests, boolean acceptEmptyPlaintext) 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;
InputStream is = new ByteArrayInputStream(ct);
CipherInputStream cis = new CipherInputStream(is, cipher);
try {
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);
cis.close();
if (result.length > 0) {
fail("this should fail; decrypted:" + TestUtil.bytesToHex(result) + " pt: " + TestUtil.bytesToHex(t.pt));
} else if (result.length == 0 && !acceptEmptyPlaintext) {
fail("Corrupted ciphertext returns empty plaintext");
}
} catch (IOException ex) {
// expected
}
}
}
Aggregations