/// <summary> /// Decrypt a Product Settings string. /// </summary> /// <param name="string">The string to decrypt.</param> /// <returns>The decrypted string.</returns> public static string Decrypt(string @string) { if (String.IsNullOrEmpty(@string)) { return(@string); } string result; try { using (var dataStream = new MemoryStream(Convert.FromBase64String(@string))) using (var cryptoService = new XorCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(@"ClearCanvas"), IV = Encoding.UTF8.GetBytes(@"IsSoCool") }) using (var cryptoStream = new CryptoStream(dataStream, cryptoService.CreateDecryptor(), CryptoStreamMode.Read)) using (var reader = new StreamReader(cryptoStream, Encoding.UTF8)) { result = reader.ReadToEnd().TrimEnd('\0'); } } catch (Exception) { result = string.Empty; } return(result); }
private static void TestRoundtripEncryption(string inputText, string key, string iv, Encoding encoding) { inputText = inputText.TrimEnd('\0'); var keyBytes = encoding.GetBytes(key); var ivBytes = encoding.GetBytes(iv); var inputBytes = encoding.GetBytes(inputText); Console.WriteLine("======================================="); Console.WriteLine("{0,12}: {1}", "Input", Convert.ToBase64String(inputBytes)); using (var cryptoServiceProvider = new XorCryptoServiceProvider { Key = keyBytes, IV = ivBytes }) { byte[] cipherBytes; using (var memoryStream = new MemoryStream()) using (var cryptoStream = new CryptoStream(memoryStream, cryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write)) { cryptoStream.Write(inputBytes, 0, inputBytes.Length); cryptoStream.FlushFinalBlock(); cipherBytes = memoryStream.ToArray(); Console.WriteLine("{0,12}: {1}", "Encrypted", Convert.ToBase64String(cipherBytes)); Assert.AreNotEqual(inputText, encoding.GetString(cipherBytes), "cipher form should not be same as plaintext form"); Assert.LessOrEqual(inputBytes.Length * 8 / 3, GetBitDelta(inputBytes, cipherBytes), "cipher form should not be similar to plaintext form"); } using (var memoryStream = new MemoryStream(cipherBytes)) using (var cryptoStream = new CryptoStream(memoryStream, cryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Read)) using (var streamReader = new StreamReader(cryptoStream, encoding)) { var outputText = streamReader.ReadToEnd().TrimEnd('\0'); var outputBytes = encoding.GetBytes(outputText); Console.WriteLine("{0,12}: {1}", "Decrypted", Convert.ToBase64String(outputBytes)); Assert.AreEqual(inputText, outputText, "decrypted plaintext should be equal to original plaintext"); } } }
private static void TestRoundtripEncryption(string inputText, string key, string iv, Encoding encoding) { inputText = inputText.TrimEnd('\0'); var keyBytes = encoding.GetBytes(key); var ivBytes = encoding.GetBytes(iv); var inputBytes = encoding.GetBytes(inputText); Console.WriteLine("======================================="); Console.WriteLine("{0,12}: {1}", "Input", Convert.ToBase64String(inputBytes)); using (var cryptoServiceProvider = new XorCryptoServiceProvider {Key = keyBytes, IV = ivBytes}) { byte[] cipherBytes; using (var memoryStream = new MemoryStream()) using (var cryptoStream = new CryptoStream(memoryStream, cryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write)) { cryptoStream.Write(inputBytes, 0, inputBytes.Length); cryptoStream.FlushFinalBlock(); cipherBytes = memoryStream.ToArray(); Console.WriteLine("{0,12}: {1}", "Encrypted", Convert.ToBase64String(cipherBytes)); Assert.AreNotEqual(inputText, encoding.GetString(cipherBytes), "cipher form should not be same as plaintext form"); Assert.LessOrEqual(inputBytes.Length*8/3, GetBitDelta(inputBytes, cipherBytes), "cipher form should not be similar to plaintext form"); } using (var memoryStream = new MemoryStream(cipherBytes)) using (var cryptoStream = new CryptoStream(memoryStream, cryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Read)) using (var streamReader = new StreamReader(cryptoStream, encoding)) { var outputText = streamReader.ReadToEnd().TrimEnd('\0'); var outputBytes = encoding.GetBytes(outputText); Console.WriteLine("{0,12}: {1}", "Decrypted", Convert.ToBase64String(outputBytes)); Assert.AreEqual(inputText, outputText, "decrypted plaintext should be equal to original plaintext"); } } }