Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    Criptare stringhe di testo

    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 :

    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
    messo in un modulo classe e poi in un modulo normale :

    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
    Nel form invece per la codifica ho scritto e funziona benissimo:

    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 :

    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
    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.")

    dove sbaglio ??
    DOMENICO FALCO

  2. #2
    Utente di HTML.it
    Registrato dal
    Jan 2011
    Messaggi
    237
    Quote Originariamente inviata da cavaliere123 Visualizza il messaggio
    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 :

    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
    messo in un modulo classe e poi in un modulo normale :

    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
    Nel form invece per la codifica ho scritto e funziona benissimo:

    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 :

    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
    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.")

    dove sbaglio ??
    Attento:
    codice:
     Dim wrapper As New Simple3Des(riga)
    Qua ci va la password, non il testo cryptato

  3. #3
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Nella TestDecoding devi passare la keyword

    codice:
        Sub TestDecoding(ByVal riga As String, ByVal k As String)
            Dim cipherText As String = riga
            Dim wrapper As New Simple3Des(k)
    così nel click del button

    codice:
            Dim leggi As System.IO.StreamReader
            Dim riga As String
    
    
            leggi = IO.File.OpenText("C:\...")
            riga = leggi.ReadLine()
            leggi.Close()
            Call TestDecoding(riga, My.Settings.pass)
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  4. #4
    Quell'avatar mi ricorda qualcosa....

    Vabbè taccio e ringrazio.
    DOMENICO FALCO

  5. #5
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Quote Originariamente inviata da cavaliere123 Visualizza il messaggio
    Quell'avatar mi ricorda qualcosa....

    Vabbè taccio e ringrazio.
    Il mondo è piccolo ...
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  6. #6
    No il mondo è immenso... buona notte.
    DOMENICO FALCO

  7. #7
    Buon giorno.... riguardandomi meglio il tutto.. ho notato che la password può essere una qualsiasi, chi ha sviluppato questa procedura ha previsto una password per il controllo sulle routine di criptaggio e decriptaggio che visto che a me non servono le sorvolo in questo modo senza passare un doppio parametro :

    codice:
    Imports prova_pass.Class1
    
    
    Module Module1
        Public risultato As String
        Private Property plainText As String
    
    
        Sub TestEncoding(ByVal scrittura As String)
    
    
            Dim plainText As String = scrittura
            Dim password As String = "criptare"
            Dim wrapper As New Simple3Des(password)
            Dim cipherText As String = wrapper.EncryptData(plainText)
            risultato = cipherText
        End Sub
    
    
        Sub TestDecoding(ByVal riga As String)
            Dim cipherText As String = riga
    
    
            Dim password As String = "criptare"
            Dim wrapper As New Simple3Des(password)
            ' DecryptData throws if the wrong password is used.
            Try
                Dim plainText As String = wrapper.DecryptData(cipherText)
                'MsgBox("The plain text is: " & plainText)
                risultato = plainText
            Catch ex As System.Security.Cryptography.CryptographicException
                'MsgBox("The data could not be decrypted with the password.")
            End Try
        End Sub
    End Module
    e ...

    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
    
    
    
    
        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.Peek <> -1
                riga = leggi.ReadLine()
                MsgBox(riga)
                Call TestDecoding(riga)
            End While
            leggi.Close()
            MsgBox(risultato)
        End Sub
    Ringrazio come sempre chi si è occupato di questo mio tread.
    DOMENICO FALCO

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.