title: Really simple encryption with C# and SymmetricAlgorithm
body: <p>I'm looking for a <strong>very</strong> 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).</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>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).</p>
  
  <p>Thanks.</p>