1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
Function aesencryptor(ByVal sourcefile As String, ByVal resultfile As String, ByVal key As String, ByVal IV As String, Optional ByVal file As Boolean = False)
Try
Dim myDESProvider As AesCryptoServiceProvider = New AesCryptoServiceProvider
myDESProvider.Key = ASCIIEncoding.ASCII.GetBytes(key)
myDESProvider.IV = ASCIIEncoding.ASCII.GetBytes(IV)
Dim myICryptoTransform As ICryptoTransform = myDESProvider.CreateEncryptor
If file = True Then
Dim ProcessFileStream As FileStream = New FileStream(sourcefile, FileMode.Open, FileAccess.Read)
Dim ResultFileStream As FileStream = New FileStream(resultfile, FileMode.Create, FileAccess.Write)
Dim myCryptoStream As CryptoStream = New CryptoStream(ResultFileStream, myICryptoTransform, CryptoStreamMode.Write)
Dim bytearrayinput(ProcessFileStream.Length - 1) As Byte
ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length)
myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length)
myCryptoStream.Close()
ProcessFileStream.Close()
ResultFileStream.Close()
Else
Dim ResultFileStream As FileStream = New FileStream(resultfile, FileMode.Create, FileAccess.Write)
Dim myCryptoStream As CryptoStream = New CryptoStream(ResultFileStream, myICryptoTransform, CryptoStreamMode.Write)
myCryptoStream.Write(ASCIIEncoding.ASCII.GetBytes(sourcefile), 0, ASCIIEncoding.ASCII.GetBytes(sourcefile).Length)
myCryptoStream.Close()
ResultFileStream.Close()
End If
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Function
Function aesdecryptor(ByVal sourcefile As String, ByVal resultfile As String, ByVal key As String, ByVal IV As String, Optional ByVal file As Boolean = False)
Try
Dim myDESProvider As AesCryptoServiceProvider = New AesCryptoServiceProvider
myDESProvider.Key = ASCIIEncoding.ASCII.GetBytes(key)
myDESProvider.IV = ASCIIEncoding.ASCII.GetBytes(IV)
Dim myICryptoTransform As ICryptoTransform = myDESProvider.CreateDecryptor
If file = True Then
Dim ProcessFileStream As FileStream = New FileStream(sourcefile, FileMode.Open, FileAccess.Read)
Dim ResultFileStream As FileStream = New FileStream(resultfile, FileMode.Create, FileAccess.Write)
Dim myCryptoStream As CryptoStream = New CryptoStream(ResultFileStream, myICryptoTransform, CryptoStreamMode.Write)
Dim bytearrayinput(ProcessFileStream.Length - 1) As Byte
ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length)
myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length)
myCryptoStream.Close()
ProcessFileStream.Close()
ResultFileStream.Close()
Else
Dim ProcessFileStream As FileStream = New FileStream(sourcefile, FileMode.Open, FileAccess.Read)
Dim fileflux As Stream = New MemoryStream()
Dim myCryptoStream As CryptoStream = New CryptoStream(fileflux, myICryptoTransform, CryptoStreamMode.Write)
Dim bytearrayinput(ProcessFileStream.Length - 1) As Byte
ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length)
MsgBox(bytearrayinput.Length) 'Résultat : 16
myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length)
myCryptoStream.FlushFinalBlock()
Dim plain(fileflux.Length - 1) As Byte
fileflux.Read(plain, 0, plain.Length)
MsgBox(plain.Length) 'Résultat : 13
MsgBox(ASCIIEncoding.ASCII.GetString(plain)) 'Mais rien ici, msgbox vide
ProcessFileStream.Close()
fileflux.Close()
Return ASCIIEncoding.ASCII.GetString(plain)
End If
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Function |
Partager