Buona sera a tutti
Sto iniziando ad usare Visual Basic 2010 express per piccoli programmini
Vado subito al dunque, essendo neofita del visual basic sto imparando tutto ex novo chiaramente.
Ho visto vari tutorial per criptare del testo e anche questo sito :
http://msdn.microsoft.com/it-it/library/ms172831.aspx
ed ho scelto di seguire tale procedura descritta.
Bene ed allora ecco il codice che ho fatto :
messo in un modulo classe e poi in un modulo normale :codice:Imports System.Security.Cryptography Public Class Class1 Public NotInheritable Class Simple3Des Private TripleDes As New TripleDESCryptoServiceProvider Private Function TruncateHash( ByVal key As String, ByVal length As Integer) As Byte() Dim sha1 As New SHA1CryptoServiceProvider ' Hash the key. Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key) Dim hash() As Byte = sha1.ComputeHash(keyBytes) ' Truncate or pad the hash. ReDim Preserve hash(length - 1) Return hash End Function Sub New(ByVal key As String) ' Initialize the crypto provider. TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8) TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8) End Sub Public Function EncryptData( ByVal plaintext As String) As String ' Convert the plaintext string to a byte array. Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext) ' Create the stream. Dim ms As New System.IO.MemoryStream ' Create the encoder to write to the stream. Dim encStream As New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write) ' Use the crypto stream to write the byte array to the stream. encStream.Write(plaintextBytes, 0, plaintextBytes.Length) encStream.FlushFinalBlock() ' Convert the encrypted stream to a printable string. Return Convert.ToBase64String(ms.ToArray) End Function Public Function DecryptData( ByVal encryptedtext As String) As String ' Convert the encrypted text string to a byte array. Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext) ' Create the stream. Dim ms As New System.IO.MemoryStream ' Create the decoder to write to the stream. Dim decStream As New CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write) ' Use the crypto stream to write the byte array to the stream. decStream.Write(encryptedBytes, 0, encryptedBytes.Length) decStream.FlushFinalBlock() ' Convert the plaintext stream to a string. Return System.Text.Encoding.Unicode.GetString(ms.ToArray) End Function End Class End Class
Nel form invece per la codifica ho scritto e funziona benissimo:codice:Imports prova_pass.Class1 Module Module1 Public risultato As String Private Property plainText As String Sub TestEncoding(ByVal scrittura As String) Dim wrapper As New Simple3Des(scrittura) Dim cipherText As String = wrapper.EncryptData(scrittura) risultato = cipherText End Sub Sub TestDecoding(ByVal riga As String) Dim cipherText As String = riga Dim wrapper As New Simple3Des(riga) ' DecryptData throws if the wrong password is used. Try Dim plainText As String = wrapper.DecryptData(cipherText) MsgBox("The plain text is: " & plainText) Catch ex As System.Security.Cryptography.CryptographicException MsgBox("The data could not be decrypted with the password.") End Try End Sub End Module
codice:Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim scrivi As System.IO.StreamWriter Dim scrittura As String My.Settings.pass = TextBox2.Text My.Settings.Save() My.Settings.Reload() TextBox2.Visible = False Button2.Visible = False scrittura = My.Settings.pass Call TestEncoding(scrittura) scrivi = IO.File.CreateText("C:\Users\admin\Desktop\ESEMPI VISUAL BASIC\prova pass\prova.text") scrivi.WriteLine(risultato) scrivi.Close() End Sub
e poi per la decodifica invece :
che invece quest'ultima procedura non mi funziona, mi esce il messaggio della routine TestDecoding che dice : MsgBox("The data could not be decrypted with the password.")codice:Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim leggi As System.IO.StreamReader Dim riga As String leggi = IO.File.OpenText("C:\Users\admin\Desktop\ESEMPI VISUAL BASIC\prova pass\prova.text") While (leggi.EndOfStream) 'leggi.Peek <> -1 riga = leggi.ReadLine() End While leggi.Close() Call TestDecoding(riga) MsgBox(risultato) End Sub End Class
dove sbaglio ??


Rispondi quotando