Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
5
Reply
Encoding and Decoding Password in login Form from Database
Nilesh Jadav
Sep 12 2016 8:57 AM
353
Reply
I am making a Registration and a login Form with encoded password saved to database, I can encode the password using encodin.utf8.
Problem is I register with some username and password and the data get saved to database with encoded password, but when I login with the same data it shows me this below error.
Invalid length for a Base-64 char array or string.
Can anyone give me the described solution for this. Below I am attaching my encoded and decoded methods :
Encoded :
private
string
encryption(
string
clearText)
{
string
encryptkey =
"123"
;
byte
[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using
(Aes encrypt = Aes.Create())
{
Rfc2898DeriveBytes rdb =
new
Rfc2898DeriveBytes(encryptkey,
new
byte
[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encrypt.Key = rdb.GetBytes(32);
encrypt.IV = rdb.GetBytes(16);
using
(MemoryStream ms =
new
MemoryStream())
{
using
(CryptoStream cst =
new
CryptoStream(ms, encrypt.CreateEncryptor(), CryptoStreamMode.Write))
{
cst.Write(clearBytes, 0, clearBytes.Length);
cst.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return
clearText;
Decoded :
private
string
decryp(
string
cipherText)
{
cipherText = cipherText.Replace(
" "
,
"+"
);
string
decryptkey =
"123"
;
byte
[] cipherBytes = Convert.FromBase64String(cipherText.Replace(
" "
,
"+"
));
using
(Aes encrypt = Aes.Create())
{
Rfc2898DeriveBytes rdb =
new
Rfc2898DeriveBytes(decryptkey,
new
byte
[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encrypt.Key = rdb.GetBytes(32);
encrypt.IV = rdb.GetBytes(16);
using
(MemoryStream ms =
new
MemoryStream())
{
using
(CryptoStream cst =
new
CryptoStream(ms, encrypt.CreateDecryptor(), CryptoStreamMode.Write))
{
cst.Write(cipherBytes, 0, cipherBytes.Length);
cst.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return
cipherText;
Any help will be appreciated,thank you !
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
5
)
Next Recommended Forum
Create Bill in c# windows application
What is the use of the property