Laden...

OneTimePad Verschlüsselung

Erstellt von jaensen vor 16 Jahren Letzter Beitrag vor 16 Jahren 3.348 Views
jaensen Themenstarter:in
2.760 Beiträge seit 2006
vor 16 Jahren
OneTimePad Verschlüsselung

Beschreibung:

Eine extrem simple Implementierung einer OneTimePad-Verschlüsselung die sich zum verschlüsseln von Texten eignet.


   /// <summary>
   /// A simple implementation of the OneTimePad encryption.
   /// </summary>
   public static class OneTimePad
   {
      /// <summary>
      /// This method generates a random key with the supplied lenght.
      /// </summary>
      /// <param name="i8Lenght">The desired lenght</param>
      /// <returns>The key</returns>
      public static byte[] GenerateKey(long i8Lenght)
      {
         byte[] key = new byte[i8Lenght];
         RandomNumberGenerator rng = RNGCryptoServiceProvider.Create();
         rng.GetNonZeroBytes(key);
         return key;
      }

      /// <summary>
      /// This method encrypts the supplied data with the supplied key.
      /// </summary>
      /// <param name="aClearText">the data which to encrypt</param>
      /// <param name="aKey">The key</param>
      /// <returns>The chiffre</returns>
      public static byte[] Crypt(byte[] aClearText, byte[] aKey)
      {
         byte[] aChiffre = new byte[aClearText.Length];
         for (long i = 0; i < aChiffre.Length; i++)
         {
            aChiffre[i] = (byte)(aClearText[i] ^ aKey[i]);
         }
         return aChiffre;
      }
      /// <summary>
      /// Overload for text.
      /// </summary>
      public static byte[] Crypt(string text, byte[] aKey)
      {
         return Crypt(Encoding.Unicode.GetBytes(text), aKey);
      }

      /// <summary>
      /// This method decrypts the supplied chiffre using the supplied key.
      /// </summary>
      /// <param name="aChiffre">The chiffre</param>
      /// <param name="aKey">The key</param>
      /// <returns>The cleartext</returns>
      public static byte[] DeCrypt(byte[] aChiffre, byte[] aKey)
      {
         byte[] aClearText = new byte[aChiffre.Length];
         for (long i = 0; i < aChiffre.Length; i++)
         {
            aClearText[i] = (byte)(aChiffre[i] ^ aKey[i]);
         }
         return aClearText;
      }
      /// <summary>
      /// This method decrypts the supplied chiffre and returns it as string.
      /// </summary>
      public static string DeCryptString(byte[] aChiffre, byte[] aKey)
      {
         return Encoding.Unicode.GetString(DeCrypt(aChiffre, aKey));
      }
   }

Schlagwörter: Encryption, Verschlüsselung, One Time Pad

I
1.739 Beiträge seit 2005
vor 16 Jahren

Gute Idee, nur ist so eine GUID ein tolles Ding zur Schlüsselgenerierung von Datensätzen und Komponenten aber leider für OTP völlig ungeeignet. Das liegt ganz einfach am Generator für GUIDS...

630 Beiträge seit 2007
vor 16 Jahren

Hallo,

verwende stattdessen lieber System.Security.Cryptography.RNGCryptoServiceProvider.

Gruss
tscherno

To understand recursion you must first understand recursion

http://www.ilja-neumann.com
C# Gruppe bei last.fm

jaensen Themenstarter:in
2.760 Beiträge seit 2006
vor 16 Jahren

verwende stattdessen lieber System.Security.Cryptography.RNGCryptoServiceProvider.

Gesagt, getan! Männo jetzt ist der Code noch kürzer 😉