[英]How to decrypt ByteArrayOutputStream in Java.?
I am trying to decrypt a file downloaded as ByteArrayOutputStream from Azure Storage.我正在尝试解密从 Azure 存储下载为 ByteArrayOutputStream 的文件。
I have a stream of type ByteArrayOutputStream.我有一个 ByteArrayOutputStream 类型的 stream。 How can I decrypt it and return decrypted ByteArrayOutputStream.?
如何解密它并返回解密的 ByteArrayOutputStream。?
I have tried using CipherOutputStream, but I'm not sure how to use it.我曾尝试使用 CipherOutputStream,但我不确定如何使用它。
Below is the code (snippet) I am using to encrypt the file before uploading下面是我在上传之前用来加密文件的代码(片段)
cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(INITIALIZATIO_VECTOR.getBytes("UTF-8")));
blob.upload(new CipherInputStream(file.getInputStream(), cipher), file.getSize());
Below is my code:下面是我的代码:
public ByteArrayOutputStream download(String fileName, Long id) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encrypkey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(INITIALIZATIO_VECTOR.getBytes("UTF-8")));
CipherOutputStream output = null;
container = getBlobClient().getContainerReference("container" + id.toString());
CloudBlockBlob blob = container.getBlockBlobReference(fileName);
if (blob.exists()) {
blob.download(os);
//output = new CipherOutputStream(outputStream, cipher);
} else {
logger.info("File does not exists on azure container");
}
} catch (StorageException e) {
logger.error("StorageException : {}" + e.getLocalizedMessage(), e);
} catch (Exception e) {
logger.error("Exception : {}" + e.getLocalizedMessage(), e);
}
return os;
}
First create the CipherOutputStream
, then download the blob into that output:首先创建
CipherOutputStream
,然后将 blob 下载到 output 中:
output = new CipherOutputStream(os, cipher);
blob.download(output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.