Exemplo n.º 1
0
        public static byte[] DecryptData(byte[] protectedData, string strMachineKeysFilePath, string strValidationAlgorithm, string strDecryptionAlgorithm, string strTargetPagePath, string strIISAppPath, string strAntiCSRFToken)
        {
            byte[] clearData = null;
            if (File.Exists(strMachineKeysFilePath))
            {
                byte[] byteEncryptionIV = new byte[16];
                Buffer.BlockCopy(protectedData, 0, byteEncryptionIV, 0, byteEncryptionIV.Length);

                Console.Write("\n\nDecryption process start!!\n\n");

                string[] machineKeys = File.ReadAllLines(strMachineKeysFilePath);
                int      nIndex      = 1;
                foreach (string strLine in machineKeys)
                {
                    try
                    {
                        Console.ForegroundColor = ConsoleColor.Blue;
                        Console.Write("\rPocessing machinekeys : {0}/{1}....", nIndex++, machineKeys.Length);

                        string[] values           = strLine.Split(',');
                        string   strValidationKey = values[0];
                        string   strDecryptionKey = values[1];
                        Purpose  objPurpose       = null;
                        if (DefinePurpose.enumPurpose == EnumPurpose.VIEWSTATE)
                        {
                            objPurpose = DefinePurpose.GetViewStatePurpose(strTargetPagePath, strIISAppPath, strAntiCSRFToken);
                        }
                        else
                        {
                            objPurpose = DefinePurpose.GetPurpose();
                        }
                        AspNetCryptoServiceProvider obj = new AspNetCryptoServiceProvider(strValidationKey, strValidationAlgorithm, strDecryptionKey, strDecryptionAlgorithm);
                        ICryptoService cryptoService    = obj.GetCryptoService(objPurpose, CryptoServiceOptions.CacheableOutput);

                        clearData = cryptoService.Unprotect(protectedData);
                        if (clearData != null)
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("\n\nKeys found!!");
                            Console.WriteLine("------------");
                            Console.ForegroundColor = ConsoleColor.Blue;
                            Console.WriteLine("DecryptionKey:" + strDecryptionKey);
                            Console.WriteLine("ValidationKey:" + strValidationKey);
                            DataWriter.WriteKeysToFile(strValidationKey, strDecryptionKey, strValidationAlgorithm, strDecryptionAlgorithm, byteEncryptionIV);
                            Console.ResetColor();
                            break;
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Null data found for following keys!!");
                            Console.WriteLine("\n\nDecryptionKey:" + strDecryptionKey);
                            Console.WriteLine("ValidationKey:" + strValidationKey + "\n\n");
                            Console.ResetColor();
                        }
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            return(clearData);
        }