Click here to Skip to main content
15,887,240 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi I am using a function to encrypte data with SHA256
C#
public static byte[] encryptByte(string pwd)
        {
            byte[] arrbyte = new byte[pwd.Length];
            SHA256 hash = new SHA256CryptoServiceProvider();
            arrbyte = hash.ComputeHash(Encoding.UTF8.GetBytes(pwd));
            hash.Dispose();
            return arrbyte;
        }

is there any chances to retrive the original data form it
thanks
Posted

SHA256 Class provides mechanism to encrypt any input to a fixed length hashed data based on SHA-256 algorithm. Hashing is a one way cryptographic function, it cannot be decrypted back to the original text.

If your requirement is to encrypt your password and at some point of time see Original password, you can use AES algorithm (AES Class), RSA Algorithm (RSACryptoServiceProvidervider Class). Below is a sample.

C#
using System;
using System.Security.Cryptography;
using System.Text;

class RSACSPSample
{
    static void Main()
    {
        try
        {
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
            byte[] encryptedData;
            byte[] decryptedData;

            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {
                encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);
                decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
                Console.WriteLine("Plain Text: {0}", ByteConverter.GetString(dataToEncrypt));
                Console.WriteLine("Encrypted plaintext: {0}", ByteConverter.GetString(encryptedData));
                Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
            }
        }
        catch (ArgumentNullException)
        {
            //Catch this exception in case the encryption did
            //not succeed.
            Console.WriteLine("Encryption failed.");
        }
    }

    static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
    {
        byte[] encryptedData;
        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
        {
            RSA.ImportParameters(RSAKeyInfo);
            encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
        }
        return encryptedData;
    }

    static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
    {
        byte[] decryptedData;
        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
        {
            RSA.ImportParameters(RSAKeyInfo);
            decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
        }
        return decryptedData;
    }
}


For more information Visit:
1. http://msdn.microsoft.com/en-us/library/system.security.cryptography.aes.aspx[^]

2. http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx[^]
 
Share this answer
 
v2
Comments
ziad imad 13-Oct-13 7:39am    
thank y
i'll try it
You cannot decrypt a one way hash.
 
Share this answer
 
Comments
ziad imad 12-Oct-13 3:39am    
how can I display the original password
any ideas
thanks
phil.o 12-Oct-13 4:44am    
You CANNOT : SHA-256 is not a cryptographic function, it is a hash algorithm. It means there is no way to get back to the original string once it has been hashed.
ziad imad 12-Oct-13 7:26am    
ok got it
thanks

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