I'm trying to add some encryption/decryption capabilities to my project, encryption seems to work just fine but the decryption is giving me some hard time, I'll appreciated you guys could spare a minute or two to throw some light on this problem I'm stuck with.
Here's the code:
static
void Main(string[] args){
// // crypto - uncrypt // byte[] key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; byte[] iv = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; string thePhrase = "*R!Oxa5";TripleDESCryptoServiceProvider tdes =
new TripleDESCryptoServiceProvider();tdes.Mode = CipherMode.CBC;
try{
MemoryStream mStreamOut =
new MemoryStream(); //Create variables to help with read and write. byte[] byte_cipheredText = Convert.FromBase64String(thePhrase); //This is intermediate storage //for the encrypted text // I get the error Invalid characted in base64 string// move the ciphered text into the stream
mStreamOut.Write(byte_cipheredText,0,(
int)byte_cipheredText.Length); // setup the crypto for tdesCryptoStream csr =
new CryptoStream(mStreamOut,tdes.CreateDecryptor(key, iv),
CryptoStreamMode.Write);
byte[] byte_plainText = mStreamOut.ToArray(); string str_plainText = Convert.ToBase64String(byte_plainText);csr.Close();
}
catch (CryptographicException cex){
Console.WriteLine(cex.Message + " " + cex.ToString());
}
catch (IOException e){
Console.WriteLine("An IO Exception has been thrown!");
Console.WriteLine(e.ToString());
Console.ReadLine();
}
}
Thanks!

Help with Invalid character in Base-64 string
globelin
JoeHand,
the problem is that the string thePhrase is not encoded in Base64. It's pretty easy to recognize a Base64 string, as it will only be composed of characters 'A'..'Z', 'a'..'z', '0'..'9', '+', '/' and it is often padded at the end with up to three '='. Your string obviously doesn't qualify and this is why you get the exception.
I don't know how you got that string, so it's hard to try and give you an alternative... try to post the code you used to encrypt.
HTH
--mc
amdarden
I hope this post will really help you we discussed the issue you are facing in detail in that:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=775703&SiteID=1
If you need more specific help please post code for Encryption and Decryption together and what results/exceptions you are facing.
Wish you all the best!
Best Regards,
Muhammad Adnan
Here is the code from the encrypt.
{
// // Using Stream object to write // byte[] key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; byte[] iv = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}; string str64Key = Convert.ToBase64String(Encoding.ASCII.GetBytes("Seal on the sky")); try{
FileStream aFile =
new FileStream(@"c:\Program Files\Test\keytest.txt",FileMode.OpenOrCreate,FileAccess.ReadWrite);
StreamReader sr =
new StreamReader(aFile);StreamWriter sw =
new StreamWriter(aFile); // Write data to fileTripleDESCryptoServiceProvider tdes =
new TripleDESCryptoServiceProvider();CryptoStream csw =
new CryptoStream(aFile,tdes.CreateEncryptor(key, iv),
CryptoStreamMode.Write);
byte[] data = Encoding.ASCII.GetBytes(str64Key);csw.Write(data,0,data.Length);
csw.Close();
aFile.Close();
}
catch (IOException e){
Console.WriteLine("An IO Exception has been thrown!");
Console.WriteLine(e.ToString());
Console.ReadLine();
return;}
return; }Running this produced this result ---> .–|OQ %I ‥Z\EovS$\ a
Any thoughts
Computer Guy69146
Thanks I'll try a do that.
Redmanmc
Well, there is no need to convert your plain text to Base64 before encryption... even though this has an interesting side-effect.
If you are in control of the entire process, I would recommend that you rewrite the code according for instance to one of the very good examples on MSDN, or in the posts RizwanSharp suggested.
Anyway, it might help you see how the entire process works if we try to leave your encryption alone and we try to fix the decryption.
The first consideration to make is that you will need to do everything backwards. So, let's start analyzing your encryption code.
1) Get a plain text string.
2) Get its byte using the ASCII encoding
3) Convert the bytes to a Base64 string.
4) Write the Base64 string to a file, through a CryptoStream.
Ok. The reverse process will be something like this:
1) Read a Base64 string through a CryptoStream.
2) Get the plaintext bytes from the Base64 string
3) Convert the bytes to a string using the ASCII encoding
4) Write out the string.
So your code should become:
static void Decrypt () {
byte [] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
byte [] iv = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider ();
tdes.Mode = CipherMode.CBC;
try {
// Use a CryptoStream to read the bytes out of a file.
// since we don't know the length of the result, but we know that it's a text string,
// we can use a StreamReader with the ReadToEnd method... this is the nice side effect I mentioned before.
FileStream fs = new FileStream (@"c:\test\keytest.txt", FileMode.Open, FileAccess.Read);
CryptoStream csr = new CryptoStream (fs, tdes.CreateDecryptor (key, iv), CryptoStreamMode.Read); // we must Read from the CryptoStream!!
StreamReader rd = new StreamReader (csr);
string decodedText = rd.ReadToEnd (); // we now have the Base64 string
// convert from Base64 to bytes
byte [] plainBytes = Convert.FromBase64String (decodedText);
// convert the bytes to a string using the ASCII encoding.
string str_plainText = Encoding.ASCII.GetString (plainBytes);
// print out the result.
Console.WriteLine (str_plainText);
csr.Close ();
fs.Close ();
} catch (CryptographicException cex) {
Console.WriteLine (cex.Message + " " + cex.ToString ());
} catch (IOException e) {
Console.WriteLine ("An IO Exception has been thrown!");
Console.WriteLine (e.ToString ());
Console.ReadLine ();
}
}
HTH
--mc