use of javax.crypto.CipherOutputStream 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.CipherOutputStream 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.CipherOutputStream in project robovm by robovm.
the class CipherOutputStream1Test method testWrite1.
/**
* write(int b) method testing. Tests that method writes correct values to
* the underlying output stream.
*/
public void testWrite1() 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());
for (int i = 0; i < data.length; i++) {
cos.write(data[i]);
}
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream wrote incorrect data.");
}
}
use of javax.crypto.CipherOutputStream in project robovm by robovm.
the class CipherOutputStream1Test method testWrite5.
/**
* write(byte[] b, int off, int len)
*/
public void testWrite5() throws Exception {
//Regression for HARMONY-758
Cipher cf = Cipher.getInstance("DES/CBC/PKCS5Padding");
NullCipher nc = new NullCipher();
CipherOutputStream stream1 = new CipherOutputStream(new BufferedOutputStream((OutputStream) null), nc);
CipherOutputStream stream2 = new CipherOutputStream(stream1, cf);
CipherOutputStream stream3 = new CipherOutputStream(stream2, nc);
stream3.write(new byte[] { 0 }, 0, 0);
//no exception expected
}
use of javax.crypto.CipherOutputStream in project robovm by robovm.
the class CipherOutputStream1Test method testClose.
/**
* close() method testing. Tests that the method calls the close() method of
* the underlying input stream.
*/
public void testClose() 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.close();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream did not flush the data.");
}
assertTrue("The close() method should call the close() method " + "of its underlying output stream.", tos.wasClosed());
}
Aggregations