Hello, so I have a more complex class in another file, but I have duplicated my trouble on a simpler class which I will show to you. I am trying to base64 encode a path via cipher text, either a good idea or a bad idea. Anyway, I am noticing that the cryptography decrypts, but the cipher text varies and is never the same twice. I was wondering if you could spot why it's doing it. My knee jerk reaction to this is that one of the input variables for the crypto method is changing between calls, but I can't see where it is. Any help would be appreciated, the data security books are to no avail.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
namespace EncryptionTest {
class Program {
static void Main(string[] args) {
String plainText = "SomeFolder";
for (int i = 0; i < 100; i++) {
byte[] encryptedA = EncryptBase64(plainText);
//Console.WriteLine("Cipher Text: " + Encoding.ASCII.GetString(encryptedA));
String plainTextB = DecryptBase64(encryptedA);
Console.WriteLine("Plaintext B: " + plainTextB);
Console.WriteLine();
}//end loop
Console.Write("Press any key to continue... ");
Console.ReadKey();
}//end method
/// <summary>
/// Gets the decrypted data.
/// Tested... Works...
/// </summary>
/// <returns></returns>
public static String DecryptBase64(byte[] cipherText) {
SymmetricAlgorithm crypt = System.Security.Cryptography.Aes.Create();
Encoding Encoding = System.Text.Encoding.UTF8;
PaddingMode PaddingMode = System.Security.Cryptography.PaddingMode.ISO10126;
byte[] key = Encoding.GetBytes("Madcat90".PadRight(crypt.Key.Length, '0').ToCharArray(), 0, crypt.Key.Length); ;
byte[] iv = Encoding.GetBytes("Madcat90".PadRight(crypt.IV.Length, '0').ToCharArray(), 0, crypt.IV.Length);
crypt.Padding = PaddingMode;
crypt.Mode = CipherMode.CBC;
crypt.Key = key;
crypt.IV = iv;
Console.WriteLine(crypt.GetType().Name);
Console.WriteLine(crypt.Padding.ToString());
Console.WriteLine(crypt.Mode.ToString());
Console.WriteLine(String.Format("Key: {0}", System.Text.Encoding.UTF8.GetString(key)));
Console.WriteLine(String.Format("IV: {0}", System.Text.Encoding.UTF8.GetString(iv)));
Console.WriteLine(String.Format("Cipher Text: {0}", System.Text.Encoding.UTF8.GetString(cipherText)));
byte[] decrypted = new byte[cipherText.Length];
String result = null;
MemoryStream cipherStream = new MemoryStream(cipherText);
ICryptoTransform transform = new FromBase64Transform();
CryptoStream csDecode = new CryptoStream(cipherStream, transform, CryptoStreamMode.Read);
CryptoStream crypto = new CryptoStream(csDecode, crypt.CreateDecryptor(crypt.Key, crypt.IV), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(crypto);
result = reader.ReadToEnd();
reader.Close();
crypto.Close();
crypto.Clear();
return result;
}//end method
/// <summary>
/// Encrypt to base64 byte array.
/// Tested... Works...
/// </summary>
/// <param name="plainText"></param>
/// <returns></returns>
public static byte[] EncryptBase64(String plainText) {
SymmetricAlgorithm crypt = System.Security.Cryptography.Aes.Create();
Encoding Encoding = System.Text.Encoding.UTF8;
PaddingMode PaddingMode = System.Security.Cryptography.PaddingMode.ISO10126;
byte[] key = Encoding.GetBytes("Madcat90".PadRight(crypt.Key.Length, '0').ToCharArray(), 0, crypt.Key.Length); ;
byte[] iv = Encoding.GetBytes("Madcat90".PadRight(crypt.IV.Length, '0').ToCharArray(), 0, crypt.IV.Length);
crypt.Padding = PaddingMode;
crypt.Mode = CipherMode.CBC;
Console.WriteLine(crypt.GetType().Name);
Console.WriteLine(crypt.Padding.ToString());
Console.WriteLine(crypt.Mode.ToString());
Console.WriteLine(String.Format("Key: {0}", System.Text.Encoding.UTF8.GetString(key)));
Console.WriteLine(String.Format("IV: {0}", System.Text.Encoding.UTF8.GetString(iv)));
Console.WriteLine(String.Format("Plain Text: {0}", plainText));
MemoryStream plainTextStream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(plainText));
crypt.Padding = PaddingMode;
byte[] plainTextBytes = Encoding.GetBytes(plainText);
byte[] encrypted = null;
crypt.Key = key;
crypt.IV = iv;
// encrypt the data using a CryptoStream
MemoryStream encryptedStream = new MemoryStream();
ICryptoTransform encoder = new ToBase64Transform();
CryptoStream csEncoder = new CryptoStream(encryptedStream, encoder, CryptoStreamMode.Write);
ICryptoTransform encryptor = crypt.CreateEncryptor(crypt.Key, crypt.IV);
CryptoStream crypto = new CryptoStream(csEncoder, encryptor, CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(crypto, Encoding);
writer.Write(plainText);
writer.Flush();
crypto.FlushFinalBlock();
encryptedStream.Position = 0;
encrypted = encryptedStream.ToArray();
crypto.Clear();
return encrypted;
}//end method
}//end class
}//end namespace