Example #1
0
        public void DecryptV3_WithCorrectKey_DecryptsMessage()
        {
            //arrange
            var generator = new ECTokenGenerator();
            var key       = "thisisakey";
            var encrypted = "24JJvrIX--LTuT_BN4WTB_n_uNtl91zrGmMzcSiKODAh0FukUT-O0WaudZ4mVc4yJmYp8bGQzIqE6toLqn40GaK98xfDe0xmrgfB46OEQiNGCAErwzed3XAy5a45Z-RVduxvppoUvga17mG8W5mEafPsfU9RgVyH6eajPosJSssIeywFFybVDf4kRZod";
            //act
            var token = generator.DecryptV3(key, encrypted, false);

            //assert
            Assert.NotNull(token);
            Assert.Equal("ec_expire=1522944645&ec_clientip=0.0.0.0&ec_country_allow=US&ec_country_deny=NA&ec_ref_allow=1234&ec_ref_deny=456", token);
        }
Example #2
0
        public void EncryptV3_WithDateTimeOnly_ReturnsEncryptedTokenWithOnlyDate()
        {
            //arrange
            var    generator  = new ECTokenGenerator();
            var    expireTime = DateTime.Now.AddMilliseconds(300);
            string key        = Faker.Name.FullName();

            //act
            var token = generator.EncryptV3(key, expireTime);

            //assert
            Assert.NotNull(token);
            var decryptdToken = generator.DecryptV3(key, token, false);

            string expected = $"ec_expire={expireTime.FromEpoch()}";

            Assert.Equal(expected, decryptdToken);
        }
Example #3
0
        public IActionResult SetToken()
        {
            var generator = new ECTokenGenerator();
            var key       = "{Your Key}";

            var    expireTime = DateTime.UtcNow.AddMinutes(5);
            string clientIp   = HttpContext.Connection.RemoteIpAddress.ToString();
            var    token      = generator.EncryptV3(key, expireTime, clientIp);

            Response.SetTokenCookies(token);

            var decryptdToken          = generator.DecryptV3(key, token, false);
            NameValueCollection qscoll = HttpUtility.ParseQueryString(decryptdToken);

            TempData["token"] = qscoll;

            TempData["Message"] = "Set Token OK!";
            return(View());
        }
Example #4
0
        public void EncryptV3_WithDateTimeAndClientIP_ReturnsEcnryptedTokenWithBoth()
        {
            //arrange
            var    generator  = new ECTokenGenerator();
            var    expireTime = DateTime.Now.AddMilliseconds(300);
            string clientIp   = Faker.Internet.IPv4Address();
            string key        = Faker.Name.FullName();

            //act
            var token = generator.EncryptV3(key, expireTime, clientIp);

            //assert

            Assert.NotNull(token);
            var decryptdToken = generator.DecryptV3(key, token, false);

            string expected = $"ec_expire={expireTime.FromEpoch()}&ec_clientip={clientIp}";

            Assert.Equal(expected, decryptdToken);
        }
        static void Main(string[] args)
        {
            try
            {
                ECTokenGenerator tokenGenerator = new ECTokenGenerator();

                if (args.Length == 1)
                {
                    System.Console.WriteLine("EC Token encryption and decryption utility.  Version: 3.0.0");
                    System.Console.WriteLine(".NET Core Version supported by ecencryptstdlib (.NET Standard 1.4) ");
                    Environment.Exit(0);
                }

                if (args.Length < 2)
                {
                    // display some examples of how to use this application
                    System.Console.WriteLine("----------------------------------------------------------------");
                    System.Console.WriteLine("| Usage / Help:");
                    System.Console.WriteLine("|       ec_encryptcore.exe <key> <text>             | create v3 encrypt token using <text> and <key>");
                    System.Console.WriteLine("|       ec_encryptcore.exe <key> <text> -v          | create v3 encrypt token using <text> and <key> in verbose mode");
                    System.Console.WriteLine("|       ec_encryptcore.exe decrypt <key> <text>     | decrypt token");
                    System.Console.WriteLine("|       ec_encryptcore.exe decrypt <key> <text> -v  | decrypt token in verbose mode");
                    System.Console.WriteLine("---------------------------------------------------------------");
                    Environment.Exit(1);
                }

                // variables to store the key and token
                string strKey    = "";
                string strToken  = "";
                string strResult = "";
                bool   isEncrypt = true;

                // we can turn on verbose output to help debug problems
                bool blnVerbose = false;

                if (blnVerbose)
                {
                    System.Console.WriteLine("----------------------------------------------------------------\n");
                }

                if (args.Length > 2)
                {
                    if (args[2] == VERBOSE_COMMAND)
                    {
                        blnVerbose = true;
                    }
                }
                if (args.Length > 3)
                {
                    if (args[3] == VERBOSE_COMMAND)
                    {
                        blnVerbose = true;
                    }
                }

                if (args[0] == DECRYPT_COMMAND)
                {
                    isEncrypt = false;
                }

                // if this is a decrypt function, then take an encrypted token and decrypt it
                if (isEncrypt)
                {
                    strKey   = args[0];
                    strToken = args[1];

                    try
                    {
                        strResult = tokenGenerator.EncryptV3(strKey, strToken, blnVerbose);

                        if (string.IsNullOrEmpty(strResult))
                        {
                            System.Console.WriteLine("Failed to encrypt token");
                        }
                    }
                    catch (System.Exception ex)
                    {
                        if (blnVerbose)
                        {
                            System.Console.WriteLine("Exception occured while encrypting token" + ex.Message);
                            Environment.Exit(1);
                        }
                    }
                }
                else
                {
                    strKey   = args[1];
                    strToken = args[2];

                    try
                    {
                        strResult = tokenGenerator.DecryptV3(strKey, strToken, blnVerbose);
                        if (string.IsNullOrEmpty(strResult))
                        {
                            System.Console.WriteLine("Failed to decrypt token.");
                        }
                    }
                    catch (System.Exception ex)
                    {
                        if (blnVerbose)
                        {
                            System.Console.WriteLine("Exception occured while encrypting token" + ex.Message);
                        }
                    }
                }

                if (blnVerbose)
                {
                    System.Console.WriteLine("----------------------------------------------------------------");
                }

                if (!string.IsNullOrEmpty(strResult))
                {
                    System.Console.WriteLine(strResult);
                }
                else
                {
                    System.Console.WriteLine("Failed to encrypt/decrypt token");
                    Environment.Exit(1);
                }
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine("Exception occured while encrypting/decrypting token" + ex.Message);
            }
        }