Help with AES decryption from MYSQL database to C#

Locked Help with AES decryption from MYSQL database to C#

  • Monday, April 30, 2012 2:50 AM
     
     

    I can not get the correct information from a database from a C# web service using AES_Decrypt. If I run the following code from the MYSQL application, I get the correct data.

        Select  AES_DECRYPT(lastName,'thePassword') as lastName from customers where customerID = 1

    That will work. But if I run the same thing from C# code in a web service I get an error. (The web service in C# returns <xs:element name="lastName" type="xs:base64Binary" minOccurs="0" />  as the datatype instead of a string.) The actual lastName value is "HiddenLastName" in  but the web service returns "SGlkZGVubGFzdE5hbWU". The code is below. Thanks for any help. I have read for days and can not find an answer.

        public DataTable DecryptTest()
        {

            string theKey = "thePassword";
            sql = "SELECT AES_DECRYPT('lastName','" + theKey + "') as lastName from customers where customerID = 1";

           .......

            return ds.Tables["tblCustomers"];

         }



    • Edited by kjpell Monday, May 07, 2012 12:14 PM
    •  

All Replies

  • Monday, May 07, 2012 12:14 PM
     
     
    Bump. I still have made no progress on this. Any help would be appreciated.
  • Wednesday, May 09, 2012 1:17 PM
     
     Answered Has Code

    That will work. But if I run the same thing from C# code in a web service I get an error. (The web service in C# returns <xs:element name="lastName" type="xs:base64Binary" minOccurs="0" />  as the datatype instead of a string.)

    The actual lastName value is "HiddenLastName" in  but the web service returns "SGlkZGVubGFzdE5hbWU". The code is below. Thanks for any help. I have read for days and can not find an answer.




    The Convert class has the FromBase64String method overload that converts a string to a byte array.  Using that method on your string, "SGlkZGVubGFzdE5hbWU", returns  an "invalid base64 string length" error message.  Base64 strings must have a length that is an even multiple of 4; i.e. 4, 8, 12, 16, etc.  Every 4 bytes represents 3 input bytes.

    The following code also returns a similar error because of the string content.

            public static string UseSoap(string s)
            {
                System.Runtime.Remoting.Metadata.W3cXsd2001.SoapBase64Binary base64Binary =
                    System.Runtime.Remoting.Metadata.W3cXsd2001.SoapBase64Binary.Parse(s);     
                return base64Binary.ToString();
            }

    Consult your web service API.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/