我正在尝试从Firebase存储下载使用AES加密的图像。我使用以下方法成功上传了图片:
private async void BtnUpload_Clicked(object sender, EventArgs e)
{
var fileStream = FileEncrypt(file.Path);
var user = await GetUser(localEmail);
await firebaseHelper.UploadFile(fileStream.Result, Path.GetFileName(file.Path), user.UserID);
var downloadurl = await firebaseHelper.GetFile(Path.GetFileName(file.Path), user.UserID);
await firebaseHelper.UploadURL(Path.GetFileName(file.Path), downloadurl.ToString(), user.UserID);
await DisplayAlert("Success", "Uploaded", "OK");
}
private async Task<FileStream> FileEncrypt(string inputFile)
{
var user = await GetUser(localEmail);
FileStream fsCrypt = new FileStream(inputFile + ".aes", FileMode.Create);
//Set Rijndael symmetric encryption algorithm
RijndaelManaged AES = new RijndaelManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
AES.Padding = PaddingMode.PKCS7;
var key = new Rfc2898DeriveBytes(user.Key, user.Salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CFB;
// write salt to the begining of the output file
fsCrypt.Write(user.Salt, 0, user.Salt.Length);
CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
//create a buffer (1mb) so only this amount will allocate in the memory and not the whole file
//1048576 is 1MB in binary
byte[] buffer = new byte[1048576];
int read;
try
{
while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
{
//Application.DoEvents(); // -> for responsive GUI, using Task will be better!
cs.Write(buffer, 0, read);
}
//fsIn.Close();//causes error
}
catch (Exception ex)
{
DisplayAlert("Error", "Error: " + ex.Message, "Ok");
}
finally
{
cs.Close();
fsCrypt.Close();
}
return fsIn;
}
但是,当我尝试下载加密文件并保存到用户Android画廊时,它无法识别文件路径,并抛出一条错误消息,指出未找到文件路径的一部分。我已检查以确保文件路径正确。 (如果我上传未加密的图像,然后使用下载URL作为我拥有的图像预览框的源,则它会成功显示图像,因此我推断出我使用的下载URL是正确的。)>
任何人都可以帮忙吗?
下面的解密和下载方法:
private async void FileDecrypt(string inputFile, string outputFile) { var user = await GetUser(localEmail); FileStream fsCrypt = new FileStream(inputFile, FileMode.Open); fsCrypt.Read(user.Salt, 0, user.Salt.Length); RijndaelManaged AES = new RijndaelManaged(); AES.KeySize = 256; AES.BlockSize = 128; var key = new Rfc2898DeriveBytes(user.Key, user.Salt, 50000); AES.Key = key.GetBytes(AES.KeySize / 8); AES.IV = key.GetBytes(AES.BlockSize / 8); AES.Padding = PaddingMode.PKCS7; AES.Mode = CipherMode.CFB; CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read); FileStream fsOut = new FileStream(outputFile, FileMode.Create); int read; byte[] buffer = new byte[1048576]; try { while ((read = cs.Read(buffer, 0, buffer.Length)) > 0) { //Application.DoEvents(); fsOut.Write(buffer, 0, read); } } catch (CryptographicException ex_CryptographicException) { DisplayAlert("Error", "CryptographicException error: " + ex_CryptographicException.Message, "Ok"); } catch (Exception ex) { DisplayAlert("Error", "Error: " + ex.Message, "Ok"); } try { cs.Close(); } catch (Exception ex) { DisplayAlert("Error", "Error by closing CryptoStream: " + ex.Message, "Ok"); } finally { fsOut.Close(); fsCrypt.Close(); } }
private async void BtnDownload_Clicked(object sender, EventArgs e) { //output path string galleryPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath; string outputPath = Path.Combine(galleryPath, localDownloadUrl.FileName); FileDecrypt(localDownloadUrl.Url, outputPath); await DisplayAlert("Success", "Image saved to gallery", "OK"); await App.Current.MainPage.Navigation.PopModalAsync(); }
我正在尝试从Firebase存储下载使用AES加密的图像。我使用以下方法成功上传了图片:私有异步无效BtnUpload_Clicked(对象发送者,...
所以,我终于在@Jason的很多帮助下终于解决了这个问题(请参阅评论)。