Exemplo n.º 1
0
        public static string PlainTextToHash(string plainTextPswd)
        {
            string encryptedPassword = "";

            byte[] decryptedBytes = TextString2ByteArray(plainTextPswd);

            try
            {
                uint   dwErrCode = 0;
                IntPtr hContext  = IntPtr.Zero;
                IntPtr hKey      = IntPtr.Zero;

                if ((dwErrCode = CryptoApiHelper.AcquireContext("OKmzdy", ref hContext)) == ERROR_SUCCESS)
                {
                    if ((dwErrCode = CryptoApiHelper.GenerateKey(ENCRYPT_ALGORITHM, MASTER_PHRASE, ref hKey, hContext)) == ERROR_SUCCESS)
                    {
                        uint data_size = Convert.ToUInt32(plainTextPswd.Length);

                        if (CryptoApiHelper.CryptEncrypt(hKey, IntPtr.Zero, true, 0, decryptedBytes, ref data_size, MAX_PSBUFFERLEN))
                        {
                            encryptedPassword = BinString2TextString(decryptedBytes, (int)data_size).ToUpper();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(string.Format("Exception loading file: {0}", ex.ToString()));
            }
            return(encryptedPassword);
        }
Exemplo n.º 2
0
        public static string HashToPlainText(string hashTextPswd)
        {
            string decryptedPassword = "";

            byte[] encryptedBytes = TextString2BinString(hashTextPswd);

            try
            {
                uint   dwErrCode = 0;
                IntPtr hContext  = IntPtr.Zero;
                IntPtr hKey      = IntPtr.Zero;

                if ((dwErrCode = CryptoApiHelper.AcquireContext("OKmzdy", ref hContext)) == ERROR_SUCCESS)
                {
                    if ((dwErrCode = CryptoApiHelper.GenerateKey(ENCRYPT_ALGORITHM, MASTER_PHRASE, ref hKey, hContext)) == ERROR_SUCCESS)
                    {
                        uint data_size = Convert.ToUInt32(Math.Min(hashTextPswd.Length / 2, MAX_PSBUFFERLEN));

                        if (CryptoApiHelper.CryptDecrypt(hKey, IntPtr.Zero, true, 0, encryptedBytes, ref data_size))
                        {
                            decryptedPassword = encFrom.GetString(encryptedBytes).Substring(0, (int)data_size);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(string.Format("Exception loading file: {0}", ex.ToString()));
            }
            return(decryptedPassword);
        }