Click here to Skip to main content
15,881,678 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
foreach (DataRow row in dt.Rows)
{
   username = row["USERNAME"].ToString();
   byte[] bytePassword = (byte[])row["PASSWORD"];
   decryptPassword = Encoding.Default.GetString(bytePassword, 0, bytePassword.Length-1);

   password = row["PASSWORD"].ToString();                    
}

string result = "This is my "+ username +" action..." + password + " : " + decryptPassword;
The result is
This is my TEST action...System.Byte[] : ‡½³wã.ç r…)©K‡n‰‚ò¹5y ÌE_0Ây™ âGâ)÷XXp@hdž‘,MÛÀy(ÓÊÕþB
I cannot seem to decode the password using System.Text.Encoding

I need to decrypt the PASSWORD then encrypt it again just so it could be saved in the database.

What I have tried:

stackoverflow.com
social.msdn.microsoft.com
stackoverflow.com
Posted
Updated 4-Mar-19 4:57am
v3
Comments
[no name] 4-Mar-19 10:21am    
You have no business "decrypting" passwords. In your situation, you should simply be copying whatever is there "to save it in the database".
Michelle Anne Rigor 4-Mar-19 10:58am    
As you can see in the code, when I get the PASSWORD at the DB it is displayed as System.byte[]. And there is a major security issue when the password is not encrypted in the DB.

 
Share this answer
 
What makes you think that a password is encrypted?
It shouldn't be: that's insecure, nearly as bad as storing it in plain text.
And System.Text.Encoding has nothing whatsoever to do with encryption - it's a translation, is all.
Passwords should be hashed, not encrypted - specifically so thay cannot be "reversed" back to the original input.
See here: Password Storage: How to do it.[^]
 
Share this answer
 
v2
Comments
Michelle Anne Rigor 4-Mar-19 10:59am    
The password is encrypted in the SQL Server. I needed a way to fetch the data as is and decrypt it in the C# code.
OriginalGriff 4-Mar-19 11:03am    
Who encrypted it?
Dave Kreskowiak 4-Mar-19 11:16am    
Password should NEVER be encrypted, only hashed.

You have the bytes from the database, but if you don't know what alogirthm(s) were used to encrypt the passwords, and any keys involved in the process, you have no hope of decrypting them.

Using an Encoder is not going to magically decrypt them. Encoding just specifies how the bytes are interpreted to generate a string from them.
OriginalGriff 4-Mar-19 11:21am    
I suspect it is hashed: the data length he shows is suspiciously close to 512 bits, which could imply SHA512 hashing.
Why do I suspect nefarious intent here? :laugh:
Dave Kreskowiak 4-Mar-19 11:24am    
I have the same suspicions. That number of bytes in his example makes for a really long password, even in Unicode.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900