Dateien Verschlüsseln, nur Textformate?

  • VB.NET
  • .NET (FX) 3.0–3.5

Es gibt 7 Antworten in diesem Thema. Der letzte Beitrag () ist von Isaalalmani.

    Dateien Verschlüsseln, nur Textformate?

    Moin, also hab mir jetzt ein Source von Microsoft geladen und dort wird auch erwähnt das nur Textdokumente Verschlüsselt werden. Nur würde ich gerne verstehen wieso nur Textdokumente? Also gibt es keine Möglichkeit auch andere Formate(vor allem Programme bzw. .exe datein) zu verschlüsseln. Ich blick da nicht ganz durch, denn theoretisch müsste dieses Verfahren auch alle anderen formate verschlüsseln können. Oder werden hier nicht die Binären daten verschlüsselt und Entschlüsselt?

    VB.NET-Quellcode

    1. Imports System
    2. Imports System.IO
    3. Imports System.Security
    4. Imports System.Security.Cryptography
    5. Imports System.Runtime.InteropServices
    6. Imports System.Text
    7. Module Module1
    8. ' Call this function to remove the key from memory after it is used for security.
    9. <DllImport("kernel32.dll")> _
    10. Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As Integer)
    11. End Sub
    12. ' Function to generate a 64-bit key.
    13. Function GenerateKey() As String
    14. ' Create an instance of a symmetric algorithm. The key and the IV are generated automatically.
    15. Dim desCrypto As DESCryptoServiceProvider = DESCryptoServiceProvider.Create()
    16. ' Use the automatically generated key for encryption.
    17. Return ASCIIEncoding.ASCII.GetString(desCrypto.Key)
    18. End Function
    19. Sub EncryptFile(ByVal sInputFilename As String, _
    20. ByVal sOutputFilename As String, _
    21. ByVal sKey As String)
    22. Dim fsInput As New FileStream(sInputFilename, _
    23. FileMode.Open, FileAccess.Read)
    24. Dim fsEncrypted As New FileStream(sOutputFilename, _
    25. FileMode.Create, FileAccess.Write)
    26. Dim DES As New DESCryptoServiceProvider()
    27. 'Set secret key for DES algorithm.
    28. 'A 64-bit key and an IV are required for this provider.
    29. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    30. 'Set the initialization vector.
    31. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    32. 'Create the DES encryptor from this instance.
    33. Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
    34. 'Create the crypto stream that transforms the file stream by using DES encryption.
    35. Dim cryptostream As New CryptoStream(fsEncrypted, _
    36. desencrypt, _
    37. CryptoStreamMode.Write)
    38. 'Read the file text to the byte array.
    39. Dim bytearrayinput(fsInput.Length - 1) As Byte
    40. fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
    41. 'Write out the DES encrypted file.
    42. cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
    43. cryptostream.Close()
    44. End Sub
    45. Sub DecryptFile(ByVal sInputFilename As String, _
    46. ByVal sOutputFilename As String, _
    47. ByVal sKey As String)
    48. Dim DES As New DESCryptoServiceProvider()
    49. 'A 64-bit key and an IV are required for this provider.
    50. 'Set the secret key for the DES algorithm.
    51. DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
    52. 'Set the initialization vector.
    53. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    54. 'Create the file stream to read the encrypted file back.
    55. Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
    56. 'Create the DES decryptor from the DES instance.
    57. Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
    58. 'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
    59. Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
    60. 'Print out the contents of the decrypted file.
    61. Dim fsDecrypted As New StreamWriter(sOutputFilename)
    62. fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
    63. fsDecrypted.Flush()
    64. fsDecrypted.Close()
    65. End Sub
    66. Public Sub Main()
    67. 'Must be 64 bits, 8 bytes.
    68. Dim sSecretKey As String
    69. ' Get the key for the file to encrypt.
    70. ' You can distribute this key to the user who will decrypt the file.
    71. sSecretKey = GenerateKey()
    72. ' For additional security, pin the key.
    73. Dim gch As GCHandle = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)
    74. ' Encrypt the file.
    75. EncryptFile("%USERPROFILE%\MyData.txt", _
    76. "%USERPROFILE%\Encrypted.txt", _
    77. sSecretKey)
    78. ' Decrypt the file.
    79. DecryptFile("%USERPROFILE%\Encrypted.txt", _
    80. "%USERPROFILE%\Decrypted.txt", _
    81. sSecretKey)
    82. ' Remove the key from memory.
    83. ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
    84. gch.Free()
    85. End Sub
    86. End Module
    nö - wenn das wirklich bei MSDN steht, dass das nur für Text-Dateien taugt, dann lügen die.

    Ich sehe allerdings ein anderes Problem:
    Die Verschlüsselung ist nicht nach Stand der Technik, denn das Passwort wird gleichzeitig als Passwort und IV benutzt, und ein Salt wird garnet eingearbeitet.
    Sowas kann man in einer ernstzunehmenden Verschlüsselung eiglich nicht bringen.

    Die Thematik ist also leider wesentlich komplizierter - wenn wolle gugge Verschlüsseln und Autentifizieren

    (Allerdings sehr interessant finde ich den Trick, wie sie - wie's aussieht - das Passwort gleich aus dem Speicher entfernen, wenns nicht mehr gebraucht wird.
    Nur auch da hätte ich gedacht, gebe es eine Framework-Klasse für: SecureString - aber kenn ich mich nicht mit aus)

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

    Also bei Microsoft werden nur Textdokumente erwähnt:
    ​This article describes how to use the cryptography classes that are provided by the Microsoft .NET Framework. You can use the cryptography classes to encrypt a text file to an unreadable state. Then, you can decrypt that text file back to its original format.
    support.microsoft.com/en-us/kb/301070/

    Aber nungut, hat sich eig. auch erledigt. Weil ich das DES recht schwach ist darauf, eigentlich wollte ich denn darauf ein TDES machen. Geht aber nicht weil der die bestimmten Zeichen vom dern Verschlüssellung nicht noch einmal Verschlüsseln und Entschlüsseln kann. Denke mal das es was mit der Zeile zutuhn hat:

    VB.NET-Quellcode

    1. DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    2. 'Set the initialization vector.
    3. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)


    Naja hab mir überlegt denn vielleicht doch eine API zu nehmen oder Versuche es mit AES noch einmal.
    Du kannst Dir auch da den Quellcode runterladen und testen. Die AesCrypter-Klasse ist, wonach Du suchst.
    Hier hab ich mal erklärt, wie Ver- und Entschlüsseln grundsätzlich funktioniert.
    "Luckily luh... luckily it wasn't poi-"
    -- Brady in Wonderland, 23. Februar 2015, 1:56
    Desktop Pinner | ApplicationSettings | OnUtils