Hi. I am created a Base64 string using two functions I found online
- public static string Encode(string plainText) {
- var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
- return System.Convert.ToBase64String(plainTextBytes);
- }
-
- public static string Decode(string base64EncodedData) {
- var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
- return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
- }
So I do string encoded = Encode("test^string"); but if I Console.WriteLine the encoded string I see "teststring". How can I encode it and preserve the carrot? I also tried two crypto functions I found online:
- public static string EncryptString(string Message, string Passphrase) {
- byte[] Results;
- System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
- MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
- byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
- TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
- TDESAlgorithm.Key = TDESKey;
- TDESAlgorithm.Mode = CipherMode.ECB;
- TDESAlgorithm.Padding = PaddingMode.PKCS7;
- byte[] DataToEncrypt = UTF8.GetBytes(Message);
- try {
- ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
- Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
- } finally {
- TDESAlgorithm.Clear();
- HashProvider.Clear();
- }
- return Convert.ToBase64String(Results);
- }
-
- public static string DecryptString(string Message, string Passphrase) {
- byte[] Results;
- System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
- MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
- byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
- TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
- TDESAlgorithm.Key = TDESKey;
- TDESAlgorithm.Mode = CipherMode.ECB;
- TDESAlgorithm.Padding = PaddingMode.PKCS7;
- byte[] DataToDecrypt = Convert.FromBase64String(Message);
- try {
- ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
- Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
- } finally {
- TDESAlgorithm.Clear();
- HashProvider.Clear();
- }
- return UTF8.GetString(Results);
- }
I did string encoded = EncryptString("test^string", "somepassphrase"); then when I output it I get the same "teststring" as the other function. Augh!