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:
create table usertable(userid image,password image)
Code is something like this:
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.