c# - .NET Encryption -
what trying when user registers password gets encrypted, , encrypted password gets saved in database, , when user logs in should decrypt password compare if user entered correct password, when try decrypt gives me "bad data" exception.
please guys. here code:
protected void btnlogin_click(object sender, eventargs e) { try { private cryptography crypt = new cryptography(); var registeruser = new test.model.user(); registeruser.emailaddress = txtemail.text; registeruser.password = txtpassword.text; //new test().getbyusername(registeruser); new test().getbyemail(registeruser, crypt); } catch (exception ex) { } } public void getbyemail(user user, cryptography crypt) { try { var repo = new userrepository(); var test = repo.getencryptedpasswrd(user); var o = repo.getprivatekey(user.emailaddress); crypt.privatekey = o; var j = repo.getpublickey(user.emailaddress); crypt.publickey = j; decryptpassword(test, o, crypt); } catch (exception ex) { } } public string decryptpassword(byte [] encryptedpassword, string privatekey, cryptography cry) { decrypted = cry.decrypt(encryptedpassword, privatekey); //return encoding.ascii.getstring(decrypted); return encoding.ascii.getstring(decrypted); } protected void btnregister_click(object sender, eventargs e) { cryptography crypt = new cryptography(); var registeruser = new test.model.user(); registeruser.name = txtname.text; registeruser.surname = txtsurname.text; registeruser.emailaddress = txtemailaddress.text; registeruser.password = txtpassword.text; registeruser.dateregisterd = datetime.now; new test().registeruser(registeruser, crypt.privatekey, crypt.publickey,crypt, encrypted); } public void registeruser(user user, string privatekey, string publickey, cryptography crypt, byte[] encrypted) { try { var repo = new userrepository(); byte[] plaintext = encoding.ascii.getbytes(user.password); encrypted = crypt.encrypt(plaintext, crypt.publickey); user.password = encoding.ascii.getstring(encrypted); user.privatekey = crypt.privatekey; user.publickkey = crypt.publickey; repo.add(user); } catch (exception ex) { } }
thanks in advance.
as said above comment should hash .
still if want encrypt example don't decrypt password . instead should encrypt password user , compare database .
you can consider simple option hash password . http://davidhayden.com/blog/dave/archive/2004/02/16/157.aspx .
Comments
Post a Comment