TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ken
NA
110
0
Why do carrots disappear when coding a string in Base64?
Mar 20 2016 1:49 PM
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!
Reply
Answers (
5
)
Asp.net windows form with c#
How to add word wrap in list box in c#