I have tried the following code below to decrypt my file which is being downloaded from azure. Unfortunatly the decrypt code is not working fully as it decrypts a file from local files not straight from azure therefore the path of the file is not correct.
how would I go about changing my decrypt code to work for this situation?
any help on this would be great!
many thanks
'--DOWNLOAD FROM AZURE
Protected Sub DwnLd_Click(ByVal sender As Object, ByVal e As EventArgs)
")
'--get id in gridview row
Dim lb As LinkButton = CType(sender, LinkButton)
Dim row As GridViewRow = CType(lb.NamingContainer, GridViewRow)
If row IsNot Nothing Then
Dim index As Integer = row.RowIndex
If index <> -1 Then
Dim id = GridView1.DataKeys(index).Values(0).ToString()
Dim path2 = "file\path\" & id.ToString
Dim StorageConnStr As String = "xxxxxxxxxxxxxx" Dim containerName = "xxxxxxxxxxxx"
Dim BlobstorageConnection As String = CloudConfigurationManager.GetSetting(StorageConnStr)
Dim MycloudStorageAccount As CloudStorageAccount = CloudStorageAccount.Parse(StorageConnStr)
Dim MyblobClient As CloudBlobClient = MycloudStorageAccount.CreateCloudBlobClient()
Dim MycloudBlobContainer As CloudBlobContainer = MyblobClient.GetContainerReference(containerName)
Dim MYblockBlob As CloudBlockBlob = MycloudBlobContainer.GetBlockBlobReference(path2)
'--Decrypt file here before download
'Build the File Path for the original (input) and the decrypted (output) file
Dim input As String = path2
Dim output As String = path2 & "_dec"
'Save the Input File, Decrypt it and save the decrypted file in output path.
MsgBox(output.ToString)
Me.Decrypt(input, output)
'Dim newMYblockBlob As CloudBlockBlob = MycloudBlobContainer.GetBlockBlobReference(output)
Dim memStream As MemoryStream = New MemoryStream()
MYblockBlob.DownloadToStream(memStream)
HttpContext.Current.Response.ContentType = MYblockBlob.Properties.ContentType.ToString()
HttpContext.Current.Response.AddHeader("Content-Disposition", "Attachment; filename=" & MYblockBlob.ToString())
HttpContext.Current.Response.AddHeader("Content-Length", MYblockBlob.Properties.Length.ToString())
HttpContext.Current.Response.BinaryWrite(memStream.ToArray())
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.Close()
Response.Redirect("~/")
End If
End If
End Sub
'----------Decrypt----------
Private Sub Decrypt(inputFilePath As String, outputfilePath As String)
Dim EncryptionKey As String = ConfigurationManager.AppSettings("Encrypt")
Using encryptor As Aes = Aes.Create()
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using fs As New FileStream(inputFilePath, FileMode.Open)
Using cs As New CryptoStream(fs, encryptor.CreateDecryptor(), CryptoStreamMode.Read)
Using fsOutput As New FileStream(outputfilePath, FileMode.Create)
Dim data As Integer
While (Assign(data, cs.ReadByte())) <> -1
fsOutput.WriteByte(CByte(data))
End While
End Using
End Using
End Using
End Using
End Sub
As far as I know, we couldn't use azure blob path to decript the file. We need firstly download it and then decript it.
The right way is you directly download the file to a path and then use that path instead of azure blob file path directly.
Best Regards,
Brando
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Thank you For your help, I will
try this method. How should I code this ? I have code that downloads the encrypted file from azure but how should I go about saving the enc file to a (temp
local)path ?
Member
51 Points
186 Posts
decrypt file being downloaded from azure
Feb 24, 2020 08:41 AM|E.RU|LINK
I have tried the following code below to decrypt my file which is being downloaded from azure. Unfortunatly the decrypt code is not working fully as it decrypts a file from local files not straight from azure therefore the path of the file is not correct. how would I go about changing my decrypt code to work for this situation?
any help on this would be great!
many thanks
Star
9831 Points
3120 Posts
Re: decrypt file being downloaded from azure
Feb 25, 2020 02:45 AM|Brando ZWZ|LINK
Hi E.RU ,
As far as I know, we couldn't use azure blob path to decript the file. We need firstly download it and then decript it.
The right way is you directly download the file to a path and then use that path instead of azure blob file path directly.
Best Regards,
Brando
Member
51 Points
186 Posts
Re: decrypt file being downloaded from azure
Feb 25, 2020 08:38 AM|E.RU|LINK
Hi Brando
Thank you For your help, I will try this method. How should I code this ? I have code that downloads the encrypted file from azure but how should I go about saving the enc file to a (temp local)path ?
Many thanks