TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Nilesh Jadav
79
23.2k
15.2m
Encoding and Decoding Password in login Form from Database
Sep 12 2016 8:57 AM
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 !
Reply
Answers (
5
)
Create Bill in c# windows application
What is the use of the property