使用RSA Cryptograpy验证用户名和密码 [英] validation of username and password using RSA Cryptograpy

查看:71
本文介绍了使用RSA Cryptograpy验证用户名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发应用程序,我需要在使用RSA加密后插入用户名和密码。它正在插入,但是当我需要在使用RSA传递到数据库进行验证后验证用户名和密码时,它会给出运行时错误。



请帮助我。



表格如下:

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.

推荐答案

从不加密密码 - 改为使用哈希值:密码存储:怎么做。 [ ^ ]
Never encrypt passwords - hash them instead: Password Storage: How to do it.[^]


这篇关于使用RSA Cryptograpy验证用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆