CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2009
    Posts
    144

    [RESOLVED] Sql insert query with encrypted value

    Hello, i have this code to encrypt the value of a password textbox

    Code:
    System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
    byte[] data = System.Text.Encoding.ASCII.GetBytes(password_textBox.Text);
    data = x.ComputeHash(data);
    string encryptedpassword = System.Text.Encoding.ASCII.GetString(data);
    and i'm trying to pass that value to my database with this code

    Code:
    string source = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database.accdb;Mode=ReadWrite;";
    string query = "Insert into users (name,password) values ('" + name_textBox.Text + "','" + encryptedpassword + "')";
    
                        OleDbConnection conn = new OleDbConnection(source);
                        OleDbCommand cmd = new OleDbCommand(query, conn);
                        cmd.Connection = conn;
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
    but i get an error because the encrypted password contains the character "?"

    is there a way to ignore what's inside query's values and just pass it to database ? or another (better) way to encrypt the textbox's text ?

    i tried to put a @ but i had no luck
    Code:
    string query = @"Insert into users (name,password) values ('" + name_textBox.Text + "','" + encryptedpassword + "')";

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Sql insert query with encrypted value

    What was the error?

  3. #3
    Join Date
    Jun 2009
    Posts
    144

    Re: Sql insert query with encrypted value

    translating from greek it says,

    syntax error missing operator in query " 'first value','second value' "

    first value = here is the encrypted password which starts with ?

  4. #4
    Join Date
    Jun 2009
    Posts
    144

    Re: Sql insert query with encrypted value

    i changed my encryption code to this and i solved my problem

    Code:
               System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] bs = System.Text.Encoding.UTF8.GetBytes(password_textBox.Text);
                bs = x.ComputeHash(bs);
                System.Text.StringBuilder encryptedpassword = new System.Text.StringBuilder();
                foreach (byte b in bs)
                {
                    encryptedpassword.Append(b.ToString("x2").ToLower());
                }
    and i get my encrypted password with encryptedpassword.ToString()

  5. #5
    Join Date
    Dec 2007
    Posts
    234

    Re: [RESOLVED] Sql insert query with encrypted value

    Instead of concatenating the SQL string together, use parameters... makes life a lot easier.

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  6. #6
    Join Date
    Jun 2009
    Posts
    144

    Re: [RESOLVED] Sql insert query with encrypted value

    i'll have it in mind thanks a lot

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] Sql insert query with encrypted value

    Or pass parameters to a stored procedure.

  8. #8
    Join Date
    Jun 2009
    Posts
    144

    Re: [RESOLVED] Sql insert query with encrypted value

    i need to read about them and find how it works for future projects, i know stored procedures are good when you want security

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] Sql insert query with encrypted value

    Quote Originally Posted by invader7 View Post
    i need to read about them and find how it works for future projects, i know stored procedures are good when you want security
    They're good for more than that as well. For example:

    1) Queries within stored procedures usually get a query plan generated for them - which means the queries are optimized and run faster.

    2) Stored procedures can be thought of as a form of 'interface' - meaning that you have flexibility to change the sql code within the store procedure without having to change the C# code that calls it. As long as the interface signature remains the same, you can change whatever you want beneath the covers.

    3) Bug fixes/feature improvements can often be made on the database layer without having to recompile/deploy C# code.

    4) Development is easier (IMO) when you use stored procedures. The reason is because you can develop and test the stored procedures on the sql side (so you know your db code works correctly) before calling the sproc(s) from the c# code. If you have trouble when call the sproc from C#, all you need to check is whether your passing the correct input to the sprocs. This is two pronged approach is much easier than trying to debug C# and sql query code all at once.
    Last edited by Arjay; December 3rd, 2010 at 11:07 AM.

  10. #10
    Join Date
    Jun 2009
    Posts
    144

    Re: [RESOLVED] Sql insert query with encrypted value

    i can see.... thanks for your advices, i'll study them

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured