Esempio n. 1
0
        public FormsAuthenticationCookie Unprotect(string protectedText)
        {
            if (protectedText == null)
            {
                throw new ArgumentNullException(nameof(protectedText));
            }

            FormsAuthenticationCookie _unprotect()
            {
                var bBlob = CryptoUtil.HexToBinary(protectedText);

                var cryptoProvider = AspNetCryptoServiceProvider.GetCryptoServiceProvider(_options);
                var cryptoService  = cryptoProvider.GetCryptoService();

                byte[] unprotectedData = cryptoService.Unprotect(bBlob);

                return(ConvertToAuthenticationTicket(unprotectedData));
            }

            if (_options.CacheTimeout != null)
            {
                return(_options.Cache.GetOrCreate($"Auth-Cookie-{protectedText}", ct => {
                    ct.SlidingExpiration = _options.CacheTimeout;
                    return _unprotect();
                }));
            }

            return(_unprotect());
        }
Esempio n. 2
0
        public static byte[] DecryptData(byte[] protectedData, string strMachineKeysFilePath, string strValidationAlgorithm, string strDecryptionAlgorithm)
        {
            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      = 0;
                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];

                        AspNetCryptoServiceProvider obj = new AspNetCryptoServiceProvider(strValidationKey, strValidationAlgorithm, strDecryptionKey, strDecryptionAlgorithm);
                        ICryptoService cryptoService    = obj.GetCryptoService(DefinePurpose.GetPurpose(), 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);
        }
        public FormsAuthenticationCookie Unprotect(string protectedText)
        {
            if (protectedText == null)
            {
                throw new ArgumentNullException(nameof(protectedText));
            }

            var bBlob = CryptoUtil.HexToBinary(protectedText);

            var cryptoProvider = AspNetCryptoServiceProvider.GetCryptoServiceProvider(_options);
            var cryptoService  = cryptoProvider.GetCryptoService();

            byte[] unprotectedData = cryptoService.Unprotect(bBlob);

            return(ConvertToAuthenticationTicket(unprotectedData));
        }
        public string Protect(FormsAuthenticationCookie cookie)
        {
            if (cookie == null)
            {
                throw new ArgumentNullException(nameof(cookie));
            }

            var unprotectedData = ConvertToBytes(cookie);

            var cryptoProvider = AspNetCryptoServiceProvider.GetCryptoServiceProvider(_options);
            var cryptoService  = cryptoProvider.GetCryptoService();

            byte[] protectedData = cryptoService.Protect(unprotectedData);

            return(CryptoUtil.BinaryToHex(protectedData));
        }
Esempio n. 5
0
        public static string EncryptData(string strDecryptDataFilePath)
        {
            ReadObject objData = new ReadObject(strDecryptDataFilePath);
            AspNetCryptoServiceProvider obj = new AspNetCryptoServiceProvider(
                objData.ValidationKey,
                objData.ValidationAlgo, objData.DecryptionKey, objData.DecryptionAlgo);

            obj.SetEncryptionIV(objData.EncryptionIV);
            DefinePurpose.SetPurposeString(objData.Purpose);

            Purpose objPurpose = null;

            byte[] byteClearData = null;
            DefinePurpose.GetPurposeAndClearData(objData, out objPurpose, out byteClearData);
            ICryptoService cryptoService = obj.GetCryptoService(objPurpose);

            return(PrintData(cryptoService.Protect(byteClearData)));
        }
Esempio n. 6
0
        public static byte[] DecryptData(byte[] protectedData, string strMachineKeysFilePath, 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);
                bool     bFound      = false;
                foreach (string strDecryptionAlgorithm in ContantValue.arrayDecryptionAlgo)
                {
                    foreach (string strValidationAlgorithm in ContantValue.arrayValidationAlgo)
                    {
                        int nIndex = 1;
                        foreach (string strLine in machineKeys)
                        {
                            try
                            {
                                Console.ForegroundColor = ConsoleColor.Blue;
                                Console.Write("\rPocessing machinekeys {0},{1}: {2}/{3}..............", strDecryptionAlgorithm, strValidationAlgorithm, 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();
                                    bFound = true;
                                    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)
                            {
                            }
                        }
                        if (bFound)
                        {
                            break;
                        }
                    }
                    if (bFound)
                    {
                        break;
                    }
                }
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("\n\nKey path file {0} not found!!\n\n", strMachineKeysFilePath);
                Console.ResetColor();
            }
            return(clearData);
        }