?
Solved

Receiving Error for Base-64 Char Array

Posted on 2008-10-02
3
Medium Priority
?
1,341 Views
Last Modified: 2013-12-17
I had a question here about encrypting data (in a basic way).  Anyway it was all working fine, however I decided to have my XML data encrypted so that I could have it slightly more secure, then using a Recovery form I could view what the pin is during development.  However I get the following error:
Invalid length for a Base-64 char array.

It happens when the following code is called to get all the IDs, Pins, DOBs etc:
           EncDecrypt decrypt = new EncDecrypt(key, iv);
            XmlDocument myDoc = new XmlDocument();
            myDoc.Load(@"C:\Projects\myXML.xml");
         

            XmlNodeList list = myDoc.SelectNodes("//Accounts/Account");
            XmlNode node;
            for (int i = 0; i < list.Count; i++)
            {
                node = list[i];
                string id = node.Attributes.GetNamedItem("ID").Value.ToString();
                string pin = node.Attributes.GetNamedItem("PIN").Value.ToString();
                string dob = node.Attributes.GetNamedItem("DOB").Value.ToString();

                string decryptedPin = decrypt.Decrypt(pin);  // The call to decrypt the string PIN
            }

Then it enters the encryption class:

        public string Decrypt(string text)
        {
            byte[] input = Convert.FromBase64String(text); // Where the error occurs
            byte[] output = Transform(input,
                            m_des.CreateDecryptor(m_key, m_iv));
            return m_utf8.GetString(output);
        }

I'm rather stuck on how to fix this.  I am able to encrypt/decrypt in my other forms but this one keeps throwing that error.  Can anybody shed any light on it for me?
0
Comment
Question by:angus_young_acdc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
tasky earned 1500 total points
ID: 22626440
Double check the value of PIN before calling Decrypt. I.e., output it to the console or debug window so you can see what's wrong.
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 22631887
I put a messagebox in to check, and the following values appear.  These are correct as they are what are stored in my XML file as the encrypted password.  

gtPiPkzuAgc
oxOqxV1UqC4=
APoBMHbBAOc
8FC7r4hmw5k

I can easily decrypt the pins if it is just searching for one of them, but when returning (and decrypting) all of the users in my XML it kicks up a fuss.

I have even tried creating a new data object to hold the info, adding each of them to a list, and then doing a foreach.  For example:

List<CustomerData> custData = new List<CustomerData>();
            EncDecrypt decrypt = new EncDecrypt(key, iv);
            XmlDocument myDoc = new XmlDocument();
            myDoc.Load(@"C:\Projects\Accounts.xml");
         
 
            XmlNodeList list = myDoc.SelectNodes("//Accounts/Account");
            XmlNode node;
            for (int i = 0; i < list.Count; i++)
            {
                node = list[i];
                string id = node.Attributes.GetNamedItem("ID").Value.ToString();
                string pin = node.Attributes.GetNamedItem("PIN").Value.ToString();
                string dob = node.Attributes.GetNamedItem("DOB").Value.ToString();
 
                CustomerData data = new CustomerData(id, dob, pin); // Takes 3 strings
                custData.Add(data);
            }
            foreach (CustomerData obj in custData)
            {
                string pass = decrypt.Decrypt(obj.PIN);
            }

Open in new window

0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 22631974
Ugh can't believe it, just figured out what it was by running a test whereby I changed the pin to one that I already know the encrypted string for, turned out when I copy/pasted them into the XML I missed the "=" at the end.  

So although reading the XML was correct (it output exactly what was there) the actual XML itself was incorrect, so outputting to console etc couldn't help.   Was only able to spot that by what I mentioned.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question