Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Really simple encryption with C# and SymmetricAlgorithm

I'm looking for a very simple crypt / decrypt method. I will be using always the same static key. I'm aware of the risks of this approach. Currently I'm using the following code but it does not generate the same result after crypting and decripting the same string (there is some garbage in the middle of the string).

public static string Crypt(this string text) {     string result = null;      if (!String.IsNullOrEmpty(text))     {         byte[] plaintextBytes = Encoding.Unicode.GetBytes(text);          SymmetricAlgorithm symmetricAlgorithm = DES.Create();         symmetricAlgorithm.Key = new byte[8] {1, 2, 3, 4, 5, 6, 7, 8};         using (MemoryStream memoryStream = new MemoryStream())         {             using (CryptoStream cryptoStream = new CryptoStream(memoryStream, symmetricAlgorithm.CreateEncryptor(), CryptoStreamMode.Write))             {                 cryptoStream.Write(plaintextBytes, 0, plaintextBytes.Length);             }              result = Encoding.Unicode.GetString(memoryStream.ToArray());         }     }      return result; }  public static string Decrypt(this string text) {     string result = null;      if (!String.IsNullOrEmpty(text))     {         byte[] encryptedBytes = Encoding.Unicode.GetBytes(text);          SymmetricAlgorithm symmetricAlgorithm = DES.Create();         symmetricAlgorithm.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };         using (MemoryStream memoryStream = new MemoryStream(encryptedBytes))         {             using (CryptoStream cryptoStream = new CryptoStream(memoryStream, symmetricAlgorithm.CreateDecryptor(), CryptoStreamMode.Read))             {                 byte[] decryptedBytes = new byte[encryptedBytes.Length];                 cryptoStream.Read(decryptedBytes, 0, decryptedBytes.Length);                 result = Encoding.Unicode.GetString(decryptedBytes);             }         }     }      return result; } 

I can change whatever is needed, no limits (but I want just to have on method to crypt and another one to decrypt without sharing variables between them).

Thanks.

like image 202
Ignacio Soler Garcia Avatar asked Jan 27 '12 09:01

Ignacio Soler Garcia


People also ask

What is the simplest encryption?

Asymmetric Encryption A key pair is used for encryption and decryption. These keys are known as public key and private key. As it uses only one key, it's a simpler method of encryption.

What is encrypt in C?

The encrypt() function uses an array of 16 48-bit keys produced by the setkey() function to encode bytes specified by the block argument according to the Data Encryption Standard (DES) encryption algorithm or to decode argument bytes according to the DES decryption algorithm.

What is the fastest encryption?

Twofish is considered among the fastest encryption standards and is hence favoured for usage among hardware and software enterprises. It is freely available and hence makes it popular. The keys used in this algorithm may be up to 256 bits in length and only one key is needed.


1 Answers

If you don't want to handle keys yourself then let the operating system do it for your. E.g. use Windows Data Protection (DPAPI).

You can write your own, string-based, version of System.Security.Cryptography.ProtectedData.Protect and Unprotect methods by using something like:

public static string Crypt (this string text) {     return Convert.ToBase64String (         ProtectedData.Protect (             Encoding.Unicode.GetBytes (text) ) ); }  public static string Decrypt (this string text) {     return Encoding.Unicode.GetString (         ProtectedData.Unprotect (              Convert.FromBase64String (text) ) ); } 
like image 52
poupou Avatar answered Sep 20 '22 15:09

poupou