如何解密 Java 中的 ByteArrayOutputStream?
How to decrypt ByteArrayOutputStream in Java.?
我正在尝试解密从 Azure 存储中下载为 ByteArrayOutputStream 的文件。
我有一个 ByteArrayOutputStream 类型的流。
我如何解密它并 return 解密 ByteArrayOutputStream。?
我尝试过使用 CipherOutputStream,但我不确定如何使用它。
下面是我在上传前用来加密文件的代码(片段)
cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(INITIALIZATIO_VECTOR.getBytes("UTF-8")));
blob.upload(new CipherInputStream(file.getInputStream(), cipher), file.getSize());
下面是我的代码:
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;
}
首先创建 CipherOutputStream
,然后将 blob 下载到该输出中:
output = new CipherOutputStream(os, cipher);
blob.download(output);
我正在尝试解密从 Azure 存储中下载为 ByteArrayOutputStream 的文件。
我有一个 ByteArrayOutputStream 类型的流。 我如何解密它并 return 解密 ByteArrayOutputStream。?
我尝试过使用 CipherOutputStream,但我不确定如何使用它。
下面是我在上传前用来加密文件的代码(片段)
cipher.init(Cipher.ENCRYPT_MODE, key,new IvParameterSpec(INITIALIZATIO_VECTOR.getBytes("UTF-8")));
blob.upload(new CipherInputStream(file.getInputStream(), cipher), file.getSize());
下面是我的代码:
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;
}
首先创建 CipherOutputStream
,然后将 blob 下载到该输出中:
output = new CipherOutputStream(os, cipher);
blob.download(output);