gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:45:28 24 4
gpt4 key购买 nike

我成功地加密了我 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);

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

The constructor CipherOutputStream(HttpPost, Cipher) is undefined. Change type of httpPost to 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

通过这种方式,您可以将加密数据打包到一个字节 [] 中,为您发送到服务器做好准备。

关于android - 如何将 aes 256 加密数据发送到 Web 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25117687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com