Translate

Freitag, 8. Mai 2015

Visual Basic.net RSA-Verschlüsselung für Texte

'RSA
    ' Verschlüsseln
    Public Function RSAEncrypt(ByVal RSAKeySize As Int32, ByVal DataToEncrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            Dim encryptedData() As Byte
            'Create a new instance of RSACryptoServiceProvider.
            Using RSA As New RSACryptoServiceProvider(RSAKeySize)

                'Import the RSA Key information. This only needs
                'toinclude the public key information.
                RSA.ImportParameters(RSAKeyInfo)

                'Encrypt the passed byte array and specify OAEP padding.  
                'OAEP padding is only available on Microsoft Windows XP or
                'later. 
                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding)
            End Using
            Return encryptedData
            'Catch and display a CryptographicException  
            'to the console.
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return Nothing
        End Try
    End Function

    ' Entschlüsseln
    Public Function RSADecrypt(ByVal RSAKeySize As Int32, ByVal DataToDecrypt() As Byte, ByVal RSAKeyInfo As RSAParameters, ByVal DoOAEPPadding As Boolean) As Byte()
        Try
            Dim decryptedData() As Byte
            'Create a new instance of RSACryptoServiceProvider.
            Using RSA As New RSACryptoServiceProvider(RSAKeySize)
                'Import the RSA Key information. This needs
                'to include the private key information.
                RSA.ImportParameters(RSAKeyInfo)

                'Decrypt the passed byte array and specify OAEP padding.  
                'OAEP padding is only available on Microsoft Windows XP or
                'later. 
                decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding)
                'Catch and display a CryptographicException  
                'to the console.
            End Using
            Return decryptedData
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return Nothing
        End Try
    End Function
    'RSA



Visual Basic.net Textdatei schreiben

Try
            ' Datei öffnen
            Dim fs As FileStream = New FileStream(DATEINAME, FileMode.OpenOrCreate, FileAccess.Write)
            'Stream öffnen
            Dim w As StreamWriter = New StreamWriter(fs)
            'Anfügen am Ende
            w.BaseStream.Seek(0, SeekOrigin.End)
            'Zeilen schreiben
            w.Write("Test")
            w.WriteLine()
            'Writer und Stream schließen
            w.Close()
            fs.Close()
        Catch ex As Exception
            MessageBox.Show(ex.toString) 'Fehlermeldung ausgeben
        End Try



Visual Basic.net Textdatei lesen

Try
            'Datei öffnen
            Dim fs As FileStream = New FileStream(DATEINAME, FileMode.OpenOrCreate, FileAccess.ReadWrite)
            'Stream öffnen
            Dim r As StreamReader = New StreamReader(fs)
            'Zeiger auf den Anfang
            r.BaseStream.Seek(0, SeekOrigin.Begin)
            'Alle Zeilen lesen und an Console ausgeben
            While r.Peek() > -1
                MessageBox.Show((r.ReadLine()))
            End While
            'Reader und Stream schließen
            r.Close()
            fs.Close()
        Catch ex As Exception
            MessageBox.Show(ex.toString) 'Fehlermeldung ausgeben
        End Try