Search in sources :

Example 36 with CipherInputStream

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;
    }
}
Also used : AESEngine(org.bouncycastle.crypto.engines.AESEngine) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CipherInputStream(javax.crypto.CipherInputStream) CipherInputStream(javax.crypto.CipherInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) FileInputStream(java.io.FileInputStream) AEADParameters(org.bouncycastle.crypto.params.AEADParameters) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) GCMBlockCipher(org.bouncycastle.crypto.modes.GCMBlockCipher) AEADBlockCipher(org.bouncycastle.crypto.modes.AEADBlockCipher) Pair(android.util.Pair)

Example 37 with CipherInputStream

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);
    }
}
Also used : HTTPRequestMessage(APJP.HTTP11.HTTPRequestMessage) CipherOutputStream(javax.crypto.CipherOutputStream) HTTPRequest(APJP.HTTP11.HTTPRequest) CipherInputStream(javax.crypto.CipherInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) HTTPResponseMessage(APJP.HTTP11.HTTPResponseMessage) Cipher(javax.crypto.Cipher) HTTPMessageHeader(APJP.HTTP11.HTTPMessageHeader) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 38 with CipherInputStream

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);
    }
}
Also used : HTTPRequestMessage(APJP.HTTP11.HTTPRequestMessage) CipherOutputStream(javax.crypto.CipherOutputStream) HTTPSRequest(APJP.HTTP11.HTTPSRequest) CipherInputStream(javax.crypto.CipherInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) HTTPResponseMessage(APJP.HTTP11.HTTPResponseMessage) Cipher(javax.crypto.Cipher) HTTPMessageHeader(APJP.HTTP11.HTTPMessageHeader) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 39 with CipherInputStream

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);
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher)

Example 40 with CipherInputStream

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.");
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) NullCipher(javax.crypto.NullCipher)

Aggregations

CipherInputStream (javax.crypto.CipherInputStream)58 Cipher (javax.crypto.Cipher)33 IOException (java.io.IOException)21 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStream (java.io.InputStream)17 NullCipher (javax.crypto.NullCipher)10 RuntimeException (java.lang.RuntimeException)9 SecretKeySpec (javax.crypto.spec.SecretKeySpec)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)7 Key (java.security.Key)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 CipherOutputStream (javax.crypto.CipherOutputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 FileInputStream (java.io.FileInputStream)5 GeneralSecurityException (java.security.GeneralSecurityException)5 InvalidKeyException (java.security.InvalidKeyException)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4 BufferedInputStream (java.io.BufferedInputStream)3 DataInputStream (java.io.DataInputStream)3