Riferimento: Perchè da questo problema?
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Public Class Dencri
Dim FileToD As String
Dim OutD As String
Dim fInput As System.IO.FileStream
Dim fOutput As System.IO.FileStream
#Region "Criptazione"
Private Enum CripAzione
AzioneC = 1
AzioneD = 2
End Enum
Private Sub Cript(ByVal sInputFile As String, ByVal strOutFile As String, ByVal byKey() As Byte, ByVal byIV() As Byte, ByVal MM As CripAzione)
Try
fInput = New System.IO.FileStream(sInputFile, FileMode.Open, FileAccess.Read)
fOutput = New System.IO.FileStream(strOutFile, FileMode.OpenOrCreate, FileAccess.Write)
fOutput.SetLength(0)
Dim bBuffer(4096) As Byte
Dim lBytesProcess As Long = 0
Dim lFileLeng As Long = fInput.Length
Dim iBites As Integer
Dim cCrip As CryptoStream
Dim cspRi As New System.Security.Cryptography.RijndaelManaged
StaoP.Value = 0
StaoP.Maximum = 100
Select Case MM
Case CripAzione.AzioneC
cCrip = New CryptoStream(fOutput, cspRi.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write)
Case CripAzione.AzioneD
cCrip = New CryptoStream(fOutput, cspRi.CreateDecryptor(byKey, byIV), CryptoStreamMode.Write)
End Select
While lBytesProcess < lFileLeng
iBites = fInput.Read(bBuffer, 0, 4096)
cCrip.Write(bBuffer, 0, iBites)
lBytesProcess = lBytesProcess + CLng(iBites)
StaoP.Value = CInt((lBytesProcess / lFileLeng) * 100)
End While
cCrip.Close()
fInput.Close()
fOutput.Close()
If MM = CripAzione.AzioneC Then
Dim fileOr As New FileInfo(FileToD)
fileOr.Delete()
End If
If MM = CripAzione.AzioneD Then
Dim fileEn As New FileInfo(OutD)
fileEn.Delete()
End If
Dim Wra As String = Chr(13) + Chr(10)
If MM = CripAzione.AzioneC Then
MessageBox.Show("File criptato correttamente" + Wra + Wra + "Totali bytes criptati = " + lBytesProcess.ToString, "CryptFile", MessageBoxButtons.OK, MessageBoxIcon.Information)
StaoP.Value = 0
Pa.Text = "Clicca su ""..."" per caricare il file."
Me.Pa.Text = ""
Me.PaC.Text = ""
Me.Pd.Text = ""
Ch.Enabled = False
OK.Enabled = False
Else
MessageBox.Show("File decriptato correttamente" + Wra + Wra + "Totali bytes decriptati = " + lBytesProcess.ToString, "CryptFile", MessageBoxButtons.OK, MessageBoxIcon.Information)
StaoP.Value = 0
Pa.Text = "Clicca su ""..."" per caricare il file."
Me.Pa.Text = ""
Me.PaC.Text = ""
Me.Pd.Text = ""
Ch.Enabled = False
OK.Enabled = False
End If
Catch When Err.Number = 53
MessageBox.Show("File non trovato.", "CryptFile", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch
fInput.Close()
fOutput.Close()
If MM = CripAzione.AzioneD Then
Dim FileD As New FileInfo(Me.Pd.Text)
FileD.Delete()
StaoP.Value = 0
Me.Pa.Text = ""
PaC.Text = ""
MessageBox.Show("Perfavore inserisci la password corretta.", "CryptFile", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Dim FileD As New FileInfo(Me.Pd.Text)
FileD.Delete()
StaoP.Value = 0
Pa.Text = ""
PaC.Text = ""
MessageBox.Show("Impossibile decriptare il file", "CryptFile", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Try
End Sub
#End Region
#Region "Fienstra dialogo"
Private Sub Br_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Br.Click
Ope.FileName = ""
Ope.Title = "Selezionare il file da criptare"
Ope.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Ope.Filter = "File Criptati (*.enc)|*.enc"
If Ope.ShowDialog = DialogResult.OK Then
FileToD = Ope.FileName
Me.Pc.Text = FileToD
Dim Positio As Integer = 0
Dim i As Integer = 0
While FileToD.IndexOf("\"c, i) <> -1
Positio = FileToD.IndexOf("\"c, i)
i = Positio + 1
End While
OutD = FileToD.Substring(0, FileToD.Length - 4)
Dim K As String = FileToD.Substring(0, Positio + 1)
OutD = OutD.Substring((Positio + 1))
Pd.Text = K + OutD.Replace("_"c, "."c)
Me.OK.Enabled = True
Me.Ch.Enabled = True
End If
End Sub
Private Sub Ch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ch.Click
Cartelle.Description = "Seleziona una cartella di destinazione"
If Cartelle.ShowDialog = DialogResult.OK Then
Me.Pd.Text = Cartelle.SelectedPath + "\" + FileToD + ".enc"
End If
End Sub
#End Region
#Region "Creazione Kiavi"
Private Function CreazioneChiave(ByVal Password As String) As Byte()
Dim GDati() As Char = Password.ToCharArray
Dim Lengh As Integer = GDati.GetUpperBound(0)
Dim byDati(Lengh) As Byte
For i As Integer = 0 To GDati.GetUpperBound(0)
byDati(i) = CByte(Asc(GDati(i)))
Next
Dim SH12 As New System.Security.Cryptography.SHA512Managed
Dim Result As Byte() = SH12.ComputeHash(byDati)
Dim byKey(31) As Byte
For i As Integer = 0 To 31
byKey(i) = Result(i)
Next
Return byKey
End Function
#End Region
#Region "Creazione IV"
Private Function CreazioneIV(ByVal Pass As String) As Byte()
Dim Dati() As Char = Pass.ToCharArray
Dim Lengh As Integer = Dati.GetUpperBound(0)
Dim BDati(Lengh) As Byte
For i As Integer = 0 To Dati.GetUpperBound(0)
BDati(i) = CByte(Asc(Dati(i)))
Next
Dim SH12 As New System.Security.Cryptography.SHA512Managed
Dim Result As Byte() = SH12.ComputeHash(BDati)
Dim BIV(15) As Byte
For i As Integer = 32 To 47
BIV(i - 32) = Result(i)
Next
Return BIV
End Function
#End Region
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
If Pa.Text = PaC.Text Then
Dim byKey As Byte()
Dim byIV As Byte()
byKey = CreazioneChiave(Pa.Text)
byIV = CreazioneIV(Pa.Text)
Cript(FileToD, Pd.Text, byKey, byIV, CripAzione.AzioneD)
End If
End Sub
Private Sub Pa_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pa.TextChanged
PaC.Text = Pa.Text
End Sub
End Class