Android 如何将aes 256加密数据发送到web服务器?

Android 如何将aes 256加密数据发送到web服务器?,android,encryption,http-post,aes,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

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

您可能希望将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

这样,您就可以将加密数据打包在一个字节[]中,随时可以发送到服务器。

不要用这样的密码创建密钥。使用密码派生函数,如中所述。这种方法要求将整个加密数据存储在RAM中,如果要使用基于流的API,这似乎有点毫无意义。最好在加密时将数据发送到服务器。我不知道他使用的HTTP API是什么,所以我无法提供流式解决方案。这将适用于所有允许您以多部分post方式发送字节[]的HTTP API。我理解问题中没有提供足够的信息来显示流式解决方案。我只是强调使用这个代码示例没有什么好处,比如说,只使用
Cipher
进行加密。