しばらく前に書いたライブラリがあり、以下のクラスコードと同じロジックを使用していますが、ファイルを復号化すると、ディスク上で復号化され、暗号化されたバージョンを削除するかどうかを選択できます。ファイルをメモリに復号化するオプションを追加して、ファイルをbyte()としてキャプチャし、バイトをサービスに送信できるようにしようとしています。私は明らかにファイルをディスクに復号化し、ファイルストリームを読み取り、byte()に変換し、復号化されたバージョンを削除することができます(ディスク上のファイルを暗号化しておく必要があるため)...削除するファイルを何度も作成します、とりわけディスクの断片化を引き起こす可能性があるので、ファイルをメモリに復号化したいと思います。とにかく、これはファイルをメモリストリーム変数に正常に復号化しますが、ファイルビューアにバイトをダムすると、ファイル形式が認識されないことを通知します。
これが私がこれまでに持っているコードです:
Public Class EncryptionFactory
Private Shared fsInput As FileStream
Public Shared Function GetDecryptedFile(ByVal password As String, ByVal encryptedFilePath As String) As Byte()
Return DecryptFile(encryptedFilePath, Key(password), IV(password))
End Function
Private Shared Function DecryptFile(ByVal strInputFile As String, ByVal bytKey As Byte(), ByVal bytIV As Byte()) As Byte()
fsInput = New FileStream(strInputFile, FileMode.Open, FileAccess.Read)
Dim memoryStream As MemoryStream
memoryStream = New MemoryStream()
Dim array As Byte() = New Byte(&H1001 - 1) {}
Dim num2 As Long = 0
Dim length As Long = fsInput.Length
Dim managed As New RijndaelManaged
Dim stream As New CryptoStream(memoryStream, managed.CreateDecryptor(bytKey, bytIV), CryptoStreamMode.Write)
Do While (num2 < length)
Dim count As Integer = fsInput.Read(array, 0, &H1000)
stream.Write(array, 0, count)
num2 = (num2 + count)
Loop
Dim data As Byte() = memoryStream.ToByte()
fsInput.Close()
fsInput.Dispose()
memoryStream.Close()
memoryStream.Dispose()
Return data
End Function
Private Shared Function Key(ByVal strPassword As String) As Byte()
Dim num5 As Integer
Dim chArray As Char() = strPassword.ToCharArray
Dim buffer As Byte() = New Byte((chArray.GetUpperBound(0) + 1) - 1) {}
Dim upperBound As Integer = chArray.GetUpperBound(0)
Dim i As Integer = 0
Do While (i <= upperBound)
buffer(i) = CByte(Strings.Asc(chArray(i)))
i += 1
Loop
Dim buffer3 As Byte() = New SHA512Managed().ComputeHash(buffer)
Dim buffer2 As Byte() = New Byte(&H20 - 1) {}
Dim index As Integer = 0
Do
buffer2(index) = buffer3(index)
index += 1
num5 = &H1F
Loop While (index <= num5)
Return buffer2
End Function
Private Shared Function IV(ByVal strPassword As String) As Byte()
Dim num5 As Integer
Dim chArray As Char() = strPassword.ToCharArray
Dim buffer As Byte() = New Byte((chArray.GetUpperBound(0) + 1) - 1) {}
Dim upperBound As Integer = chArray.GetUpperBound(0)
Dim i As Integer = 0
Do While (i <= upperBound)
buffer(i) = CByte(Strings.Asc(chArray(i)))
i += 1
Loop
Dim buffer3 As Byte() = New SHA512Managed().ComputeHash(buffer)
Dim buffer2 As Byte() = New Byte(&H10 - 1) {}
Dim index As Integer = &H20
Do
buffer2((index - &H20)) = buffer3(index)
index += 1
num5 = &H2F
Loop While (index <= num5)
Return buffer2
End Function
End Class
アップデート:
cryptostream.FlushFinalBlock()を追加しました
これはまだ機能していません...ストリームを最後まで読んでいないように感じます。
Private Shared Function DecryptFile(ByVal strInputFile As String, ByVal bytKey As Byte(), ByVal bytIV As Byte()) As Byte()
fsInput = New FileStream(strInputFile, FileMode.Open, FileAccess.Read)
Dim memoryStream As MemoryStream
memoryStream = New MemoryStream()
Dim array As Byte() = New Byte(&H1001 - 1) {}
Dim num2 As Long = 0
Dim length As Long = fsInput.Length
Dim managed As New RijndaelManaged
Dim stream As New CryptoStream(memoryStream, managed.CreateDecryptor(bytKey, bytIV), CryptoStreamMode.Write)
Do While (num2 < length)
Dim count As Integer = fsInput.Read(array, 0, &H1000)
stream.Write(array, 0, count)
num2 = (num2 + count)
Loop
stream.FlushFinalBlock()
Dim data As Byte() = memoryStream.ToByte()
fsInput.Close()
fsInput.Dispose()
memoryStream.Close()
memoryStream.Dispose()
Return data
End Function
アップデート:
これが私の「暗号化」メソッドで、復号化と同じIVメソッドとキーメソッドを使用しています...
Friend Sub EncryptFile(ByVal strInputFile As String, ByVal strOutputFile As String, ByVal bytKey As Byte(), ByVal bytIV As Byte(), ByVal deleteOrig As Boolean)
Me.fsInput = New FileStream(strInputFile, FileMode.Open, FileAccess.Read)
Me.fsOutput = New FileStream(strOutputFile, FileMode.OpenOrCreate, FileAccess.Write)
Me.fsOutput.SetLength(0)
Dim array As Byte() = New Byte(&H1001 - 1) {}
Dim num2 As Long = 0
Dim length As Long = Me.fsInput.Length
Dim managed As New RijndaelManaged
Dim stream As New CryptoStream(Me.fsOutput, managed.CreateEncryptor(bytKey, bytIV), CryptoStreamMode.Write)
Do While (num2 < length)
Dim count As Integer = Me.fsInput.Read(array, 0, &H1000)
stream.Write(array, 0, count)
num2 = (num2 + count)
Loop
stream.Close
Me.fsInput.Close
Me.fsOutput.Close
If deleteOrig Then
Dim info As New FileInfo(strInputFile)
If ((info.Attributes And FileAttributes.ReadOnly) > 0) Then
info.Attributes = (info.Attributes Xor FileAttributes.ReadOnly)
File.Delete(strInputFile)
Else
info.Delete
End If
End If
End Sub
最終更新:
成功したメモリコードへのファイルの復号化は次のとおりです。
Private Shared Function DecryptFile(ByVal strInputFile As String, ByVal bytKey As Byte(), ByVal bytIV As Byte()) As Byte()
fsInput = New FileStream(strInputFile, FileMode.Open, FileAccess.Read)
Dim memoryStream As MemoryStream
memoryStream = New MemoryStream()
Dim array As Byte() = New Byte(&H1001 - 1) {}
Dim num2 As Long = 0
Dim length As Long = fsInput.Length
Dim managed As New RijndaelManaged
Dim stream As New CryptoStream(memoryStream, managed.CreateDecryptor(bytKey, bytIV), CryptoStreamMode.Write)
Do While (num2 < length)
Dim count As Integer = fsInput.Read(array, 0, &H1000)
stream.Write(array, 0, count)
num2 = (num2 + count)
Loop
stream.FlushFinalBlock()
stream.Dispose()
Dim data As Byte() = memoryStream.ToArray()
fsInput.Close()
fsInput.Dispose()
memoryStream.Close()
memoryStream.Dispose()
Return data
End Function