Click here to Skip to main content
15,921,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing as application where I need to insert user name and password after encryption using RSA. It is inserting, but when I need to validate the user name and password after passing to the database for validation using RSA then it gives runtime error.

Please help me.

The table is something like this:
SQL
create table usertable(userid image,password image)

Code is something like this:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Data.SqlClient;
using System.Data;


namespace Memory_Card
{
    class Program
    {
        
        RSACryptoServiceProvider r = new RSACryptoServiceProvider();
        void insert()
        {
            Console.WriteLine("Enter name:");
            string name = Console.ReadLine();
            Console.WriteLine("Enter Password:");
            string password = Console.ReadLine();


            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True;");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert usertable values(@username,@password)", con);
            byte[] b = Encoding.ASCII.GetBytes(name);
            cmd.Parameters.AddWithValue("@username", r.Encrypt(b, true));
            b = Encoding.ASCII.GetBytes(password);
            cmd.Parameters.AddWithValue("@password", r.Encrypt(b, true));
            cmd.ExecuteNonQuery();
            Console.WriteLine("inserted");
        }
        void validate()
        {

           
            Console.WriteLine("Enter name:");
            string name = Console.ReadLine();
            Console.WriteLine("Enter Password:");
            string password = Console.ReadLine();


            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True;");
            con.Open();
            SqlCommand cmd=new SqlCommand("select count(*) from usertable where userid=@username and password=@password",con);
            byte[] b = Encoding.ASCII.GetBytes(name);
            cmd.Parameters.AddWithValue("@username", r.Encrypt(b, true));
            byte [] b1 = Encoding.ASCII.GetBytes(password);
            cmd.Parameters.AddWithValue("@password", r.Encrypt(b1, true));
            DataSet ds=new DataSet();
            SqlDataAdapter da=new SqlDataAdapter(cmd);
            da.Fill(ds);
            if(ds.Tables[0].Rows.Count==0)
         
            {
            
                Console.WriteLine("Invalid");
            }
            else
            {
               Console.WriteLine("Valid");
            }

        }
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine("1. Insert");
            Console.WriteLine("2. Validate");
            Console.WriteLine("Enter Ur Choice:");
            int choice = Convert.ToInt32(Console.ReadLine());
            switch (choice)
            {
                case 1:

                   
                    p.insert();
                    break;
                case 2:
                    p.validate();
                    break;
            }
            Console.Read();
        }
    }
}

and the exception is getting is
The data types image and varbinary are incompatible in the equal to operator.
Posted
Updated 3-May-13 22:35pm
v3

1 solution

Never encrypt passwords - hash them instead: Password Storage: How to do it.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900