mit Cryptostream verschlüsselte xml in anderes Programm einlesen

  • VB.NET
  • .NET (FX) 4.5–4.8

    mit Cryptostream verschlüsselte xml in anderes Programm einlesen

    EDIT: Problem selbst gelöst.
    Habe das DataSet importiert, aber nichts weiter programmiert als nur den Import.
    Weil dann nichts passiert ist, habe ich den Button erneut geklickt und der readxml schlägt fehl, weils DataSet schon gefüllt ist.
    SORRY!
    Falls das zu löschen geht - bitte machen.

    Hallo
    Ich speichere in meinem Programm ein DataSet nach xml und nutze dafür eine Cryptostream Verschlüsselung:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub FrmMainForm_Closed(sender As Object, e As EventArgs) Handles Me.Closed
    2. SaveSettings()
    3. End Sub
    4. Public Sub SaveSettings()
    5. 'ggf. vorhandene SettingsFile löschen
    6. If File.Exists(_SettingsFile) Then File.Delete(_SettingsFile)
    7. 'Verschlüsselung starten und seichern der xml
    8. Using cS As CryptoStream = GetEncryptionStream()
    9. Me.DtsSettings.WriteXml(cS)
    10. End Using
    11. End Sub
    12. Private Function GetEncryptionStream() As CryptoStream
    13. Dim key As New Rfc2898DeriveBytes(_MasterPassword, New Byte() {10, 255, 255, 7, 14, 16, 23, 155})
    14. Dim x As New TripleDESCryptoServiceProvider()
    15. x.Key = key.GetBytes(x.KeySize \ 8)
    16. x.IV = key.GetBytes(x.BlockSize \ 8)
    17. Dim fS As FileStream = New FileStream(_SettingsFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None)
    18. Return New CryptoStream(fS, x.CreateEncryptor(), CryptoStreamMode.Write)
    19. End Function


    Diese xml Datei möchte ich nun in einem anderen Programm einlesen (ich möchte ein kleines Programm schreiben und EINMAL die Inhalte von zwei DataTable in eine neue xml zu kopieren.
    Hierzu habe ich im neuen Programm ebenfalls ein DataSet mit dem Namen DTSSettings erzeugt und die DataTables aus dem Programm hier reinkpoiert (Strg+a / Strg+C / Strg+V)
    Dann habe ich folgenden Code hinzugefügt: (Die Variablen _OldPW und _OldFile sind dabei korrekt gefüllt (Das habe ich getestet)

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub BTNStartImport_Click(sender As Object, e As EventArgs) Handles BTNStartImport.Click
    2. 'alte Daten Laden
    3. LoadSettings(_OldPW, _OldFile)
    4. End Sub
    5. Private Sub LoadSettings(Passwort As String, DataFile As String)
    6. If File.Exists(DataFile) Then
    7. Using cS As CryptoStream = GetDecryptionStream(Passwort, DataFile)
    8. DtsSettings.ReadXml(cS)
    9. End Using
    10. End If
    11. End Sub
    12. Private Function GetDecryptionStream(Passwort As String, DataFile As String) As CryptoStream
    13. Dim key As New Rfc2898DeriveBytes(Passwort, New Byte() {10, 255, 255, 7, 14, 16, 23, 155})
    14. Dim x As New TripleDESCryptoServiceProvider()
    15. x.Key = key.GetBytes(x.KeySize \ 8)
    16. x.IV = key.GetBytes(x.BlockSize \ 8)
    17. Dim fS As FileStream = New FileStream(DataFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
    18. Return New CryptoStream(fS, x.CreateDecryptor(), CryptoStreamMode.Read)
    19. End Function


    Ich erhalte aber nur den Fehler: Ungültige Daten auf Stammebene. Zeile 1, Position 1. beim Ausführen von Zeile 9
    Hier der gesamte Fehlertext:
    Spoiler anzeigen
    System.Xml.XmlException
    HResult=0x80131940
    Nachricht = Ungültige Daten auf Stammebene. Zeile 1, Position 1.
    Quelle = System.Xml
    Stapelüberwachung:
    at System.Xml.XmlTextReaderImpl.Throw(Exception e)
    at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
    at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
    at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
    at System.Xml.XmlTextReaderImpl.Read()
    at System.Xml.XmlTextReader.Read()
    at System.Xml.XmlReader.MoveToContent()
    at System.Data.DataSet.ReadXml(XmlReader reader, Boolean denyResolving)
    at System.Data.DataSet.ReadXml(Stream stream)
    at DasProgrammImporter.frmMain.LoadSettings(String Passwort, String DataFile) in C:\Users\flori\Documents\Visual Studio 2022\Programme\DasProgrammImporter\DasProgrammImporter\Form1.vb:line 61
    at DasProgrammImporter.frmMain.BTNStartImport_Click(Object sender, EventArgs e) in C:\Users\flori\Documents\Visual Studio 2022\Programme\DasProgrammImporter\DasProgrammImporter\Form1.vb:line 44
    at System.Windows.Forms.Control.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnClick(EventArgs e)
    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
    at System.Windows.Forms.Button.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    at DasProgrammImporter.My.MyApplication.Main(String[] Args) in :line 83


    Was mache ich falsch?

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „DerSmurf“ ()