1

I successfully encrypted an audio file on my sd card and placed that encrypted file again in my sd card. I used the below code to save my file in sd card.

SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), algorithm);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos = new CipherOutputStream(outputStream, cipher);
int b;
    byte[] d = new byte[8];
    while((b = inputStream.read(d)) != -1){
        cos.write(d, 0, b);

But now i want to encrypt and send that file to a web server without saving it in sd card. I tried using the above code, but it says

The constructor CipherOutputStream(HttpPost, Cipher) is undefined. Change type of httpPost to OutputStream.

How can i send it. Please help.

This is the method i use to send the original file to server:

public void uploadFile(String outfile) {
int count;
        try{

            // the URL where the file will be posted
            String postReceiverUrl = "The url where i want to send";

            // new HttpClient
            HttpClient httpClient = new DefaultHttpClient();

            // post header
            HttpPost httpPost = new HttpPost(postReceiverUrl);

             //outfile is the original file in sd card.
            File file = new File(outfile);
            FileBody fileBody = new FileBody(file);

            String file_name_de = "MA_"+u_name.subSequence(0, 3)+u_id;

            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

            reqEntity.addPart("frm_file", fileBody);
            reqEntity.addPart("frm_user_id", new StringBody(String.valueOf(u_id), Charset.forName("UTF-8")));

            reqEntity.addPart("frm_token", new StringBody(u_token));
            reqEntity.addPart("frm_file_name", new StringBody(file_name_de, Charset.forName("UTF-8")));
            httpPost.setEntity(reqEntity);

            // execute HTTP post request
            HttpResponse response = httpClient.execute(httpPost);

          //get the InputStream
            InputStream is=fileBody.getInputStream();

                byte[] data = baos.toByteArray();

            //create a buffer
            byte data[] = new byte[1024];//1024

            //this updates the progress bar
            long total=0;
            while((count=is.read(d))!=-1){
                total+=count;
                publishProgress((int)(total*100/file.length()));
            }

            HttpEntity resEntity = response.getEntity();

            if (resEntity != null) {

                @SuppressWarnings("unused")
                String responseStr = EntityUtils.toString(resEntity).trim();

                //Log.d("Response: ", responseStr);

            }

            file.delete();

        } catch (NullPointerException e) {
            //e.printStackTrace();
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }
Srijith
  • 1,695
  • 17
  • 29
  • 1
    Don't create a key from a password like that. Use a password derivation function, as described in [Java 256-bit AES Password-Based Encryption](http://stackoverflow.com/questions/992019/java-256-bit-aes-password-based-encryption?rq=1). – Duncan Jones Aug 04 '14 at 11:39

1 Answers1

1

You probably want to wrap the CipherOutputStream in a ByteArrayOutputStream like this:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
CipherOutputStream cos = new CipherOutputStream(baos, cipher);
int b;
    byte[] d = new byte[BUFFER_SIZE];
    while((b = inputStream.read(d)) != -1){
        cos.write(d, 0, b);
 }

byte[] data = baos.toByteArray();

// now send data to server

This way you have the encrypted data packaged in a byte[], ready for you to shoot off to the server.

David Xu
  • 5,555
  • 3
  • 28
  • 50
  • This approach requires you to store the entire encrypted data in RAM, which seems a bit pointless if you want to use a stream-based API. Better that you send the data to the server as you encrypt it. – Duncan Jones Aug 04 '14 at 11:43
  • I have no idea what HTTP API he's using so I can't provide a streaming solution. This one will work with all HTTP APIs that let you send a byte[] as multipart post. – David Xu Aug 04 '14 at 11:45
  • I appreciate that not enough info was provided in the question to allow you to show a streaming solution. I was just highlighting that there is little benefit to using this code example over, say, just doing the encryption with `Cipher`. – Duncan Jones Aug 04 '14 at 11:47