I am working on a C# visual studio windows form application. However, i was stuck halfway during my development.
I am trying to ask the user for his/her password before encrypting it with salted. How can i encrypted my password text with SHA 512 with salted and decrypted it later on? Is there any advise on how i can do it? Is SHA 512 secure enough as compare to other encryption methods?
SHA512 is NOT a form of encryption, it is a form of hashing. Hashing is one-way - i.e. it cannot be decrypted. The only way to find a value from a hash is by rainbow tables, which is not an exact science, to say the least.
As such, SHA512 is more secure than an encryption method when it comes to a password, as you are never storing something that can be easily decrypted, merely collided with.
Others say "SHA-512 is not encryption", but neglect to say that you can, and how you can, actually encrypt WITH it. Encryption requires a public key (for encrypting) & a private key (for decryption), if you are using an RSACryptoServiceProvider & asymmetric encryption, as shown below. Since you can create SHA512-based hashed keys, you can encrypt/decrypt against them. For doing the salt, you could potentially look at http://www.obviex.com/samples/EncryptionWithSalt.aspx. I don't go into that, here.
For those that may complain about this answer below & say "use PBKDF2":
Not every environment will have these restrictions-but to the pundits: don't assume you have a 1-size fits all solution. Service account passwords for database connections reside, ENCRYPTED, in things like web.config and app.config files for us-which is the angle to which I am answering this question. They cannot be domain accounts, which would allow you to do things like
Integrated Security=SSPI
orwebService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
. Do not assume these will only be in DBs - most databases actually have their own encryption algorithms to deal with. For certain connections & environments, developer community - get over yourselves! You may actually have to supply a password and have it stored somewhere for your application to work! And guess what-NIST doesn't prohibit this, or it would not have encryption standards you can meet (which SHA512 is encouraged for) to answer this call.So I'm going to piece together some research I did to show a couple of routes you can go for how to get this going using a key created using a SHA-512 hash, that you then encrypt/decrypt against. You can generate a SHA-512 key either by creating a certificate or letting the RSACryptoServiceContainer give you one.
Certificate method
Create your certificate with these lines on a command line:
Then import the certificate to the local root authority store and use this code:
Reference: https://social.msdn.microsoft.com/Forums/en-US/69e39ad0-13c2-4b5e-bb1b-972a614813fd/encrypt-with-certificate-sha512?forum=csharpgeneral
Using RSACryptoServiceProvider to generate the keys
These methods use a DLL called RSAx.DLL, built using the source code at https://www.codeproject.com/Articles/421656/RSA-Library-with-Private-Key-Encryption-in-Csharp , which is not mine (author: Arpan Jati), but I've used it and it is available to the developer community under CodeProject's Open Source License. You could also just bring in 3 classes from that project, instead: RSAx.cs, RSAxParameters.cs, RSAxUtils.cs
The code would take this post over the 30000 char limit, so I'll just post RSAx so you can see what's going on, but all 3 classes are required. You have to change the namespace and reference the System.Numerics assembly.
RSAx.cs