use of javax.crypto.CipherInputStream in project Conversations by siacs.
the class AbstractConnectionManager method createInputStream.
public static Pair<InputStream, Integer> createInputStream(DownloadableFile file, boolean gcm) throws FileNotFoundException {
FileInputStream is;
int size;
is = new FileInputStream(file);
size = (int) file.getSize();
if (file.getKey() == null) {
return new Pair<InputStream, Integer>(is, size);
}
try {
if (gcm) {
AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
InputStream cis = new org.bouncycastle.crypto.io.CipherInputStream(is, cipher);
return new Pair<>(cis, cipher.getOutputSize(size));
} else {
IvParameterSpec ips = new IvParameterSpec(file.getIv());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
Log.d(Config.LOGTAG, "opening encrypted input stream");
final int s = Config.REPORT_WRONG_FILESIZE_IN_OTR_JINGLE ? size : (size / 16 + 1) * 16;
return new Pair<InputStream, Integer>(new CipherInputStream(is, cipher), s);
}
} catch (InvalidKeyException e) {
return null;
} catch (NoSuchAlgorithmException e) {
return null;
} catch (NoSuchPaddingException e) {
return null;
} catch (InvalidAlgorithmParameterException e) {
return null;
}
}
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 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 robovm by robovm.
the class CipherInputStream1Test method testAvailable.
/**
* available() method testing. Tests that the method always return 0.
*/
public void testAvailable() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestInputStream tis = new TestInputStream(data);
CipherInputStream cis = new CipherInputStream(tis, new NullCipher());
assertEquals("The returned by available() method value " + "should be 0.", cis.available(), 0);
}
use of javax.crypto.CipherInputStream in project robovm by robovm.
the class CipherInputStream1Test method testRead1.
/**
* read() method testing. Tests that method returns the correct value
* (related to the InputStream) and that it returns -1 at the end of stream.
*/
public void testRead1() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestInputStream tis = new TestInputStream(data);
CipherInputStream cis = new CipherInputStream(tis, new NullCipher());
byte res;
for (int i = 0; i < data.length; i++) {
if ((res = (byte) cis.read()) != data[i]) {
fail("read() returned the incorrect value. " + "Expected: " + data[i] + ", Got: " + res + ".");
}
}
if (cis.read() != -1) {
fail("read() should return -1 at the end of the stream.");
}
}
Aggregations