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
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...
Hallo,
verwende stattdessen lieber System.Security.Cryptography.RNGCryptoServiceProvider.
Gruss
tscherno
verwende stattdessen lieber System.Security.Cryptography.RNGCryptoServiceProvider.
Gesagt, getan! Männo jetzt ist der Code noch kürzer 😉