5
Answers

Why do carrots disappear when coding a string in Base64?

Ken

Ken

8y
381
1
Hi.  I am created a Base64 string using two functions I found online 
  1. public static string Encode(string plainText) {  
  2.   var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);  
  3.   return System.Convert.ToBase64String(plainTextBytes);  
  4. }  
  5.   
  6. public static string Decode(string base64EncodedData) {  
  7.   var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);  
  8.   return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);  
  9. }  
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: 
  1. public static string EncryptString(string Message, string Passphrase) {  
  2.   byte[] Results;  
  3.   System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();  
  4.   MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();  
  5.   byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));  
  6.   TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();  
  7.   TDESAlgorithm.Key = TDESKey;  
  8.   TDESAlgorithm.Mode = CipherMode.ECB;  
  9.   TDESAlgorithm.Padding = PaddingMode.PKCS7;  
  10.   byte[] DataToEncrypt = UTF8.GetBytes(Message);  
  11.   try {  
  12.     ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();  
  13.     Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);  
  14.   } finally {  
  15.     TDESAlgorithm.Clear();  
  16.     HashProvider.Clear();  
  17.   }  
  18.   return Convert.ToBase64String(Results);  
  19. }  
  20.   
  21. public static string DecryptString(string Message, string Passphrase) {  
  22.   byte[] Results;  
  23.   System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();  
  24.   MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();  
  25.   byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));  
  26.   TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();  
  27.   TDESAlgorithm.Key = TDESKey;  
  28.   TDESAlgorithm.Mode = CipherMode.ECB;  
  29.   TDESAlgorithm.Padding = PaddingMode.PKCS7;  
  30.   byte[] DataToDecrypt = Convert.FromBase64String(Message);  
  31.   try {  
  32.     ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();  
  33.     Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);  
  34.   } finally {  
  35.     TDESAlgorithm.Clear();  
  36.     HashProvider.Clear();  
  37.   }  
  38.   return UTF8.GetString(Results);  
  39. }  
 I did string encoded = EncryptString("test^string", "somepassphrase"); then when I output it I get the same "teststring" as the other function.  Augh!
Answers (5)
0
Suthish Nair

Suthish Nair

NA 31.7k 4.6m 8y
Close the thread, if its resolved..
0
Ken

Ken

NA 67 0 8y
Thanks again!
0
Suthish Nair

Suthish Nair

NA 31.7k 4.6m 8y
Use System.Net.WebUtility.HtmlDecode instead of Decode.
0
Ken

Ken

NA 67 0 8y
.NET 4 Client Profile.  Actually, I just found the problem.  When you said it worked fine for you, I thought maybe I should try it as basic as possible, not as part of my actual program so in my main function I put Console.WriteLine(Decode(Encode("test^string"))); and it actually outputted it correctly.
 
After wracking my brain, the only difference I saw was that when it fails in the normal part of my code, it's being passed as a command line argument so I had it echo the input rather than the encoded output and sure enough, it was coming in as teststring.
 
Once I encapsulated the command line argument with double quotes, the problem was solved.  Thanks.
0
Suthish Nair

Suthish Nair

NA 31.7k 4.6m 8y
Code working fine for me. Which framework are you using?