如何将256个加密数据发送到Web服务器?

时间:2014-08-04 11:31:54

标签: android encryption http-post aes

我成功加密了我的SD卡上的音频文件,并将该加密文件再次放入我的SD卡中。我使用下面的代码将我的文件保存在SD卡中。

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);

但是现在我想加密并将该文件发送到Web服务器而不将其保存在SD卡中。我尝试使用上面的代码,但它说

  

构造函数CipherOutputStream(HttpPost,Cipher)未定义。将httpPost的类型更改为OutputStream。

我该如何发送。请帮忙。

这是我用来将原始文件发送到服务器的方法:

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();
        }
    }

1 个答案:

答案 0 :(得分:1)

您可能希望将CipherOutputStream包装在ByteArrayOutputStream中,如下所示:

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

这样你就可以将加密数据打包成一个byte [],随时可以射入服务器。