Exemplo n.º 1
0
        private string GetValueConfigDecript(string key1 = null, string key2 = null, string key3 = null)
        {
            var value = GetValueConfig <string>(key1, key2, key3);

            if (!string.IsNullOrWhiteSpace(value))
            {
                return(Base64Encryptor.Decrypt(value));
            }
            return(string.Empty);
        }
Exemplo n.º 2
0
        public void TestEncryption()
        {
            using var rmCrypto = new RijndaelManaged();

            byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
            byte[] iv  = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };

            using var encryptTransform = rmCrypto.CreateEncryptor(key, iv);

            var encryptor = new Base64Encryptor(encryptTransform);

            encryptor.Write(DateTime.MaxValue);
            encryptor.Write(DateTime.MaxValue, 1000);
            encryptor.Write(Guid.Empty);
            encryptor.Write(byte.MaxValue);
            encryptor.Write(new byte[] { 2, 3 });
            encryptor.Write(int.MaxValue);
            encryptor.Write(long.MaxValue);
            encryptor.Write(short.MaxValue);
            encryptor.Write(int.MinValue);
            encryptor.Write(long.MinValue);
            encryptor.Write(short.MinValue);
            var id = Base64.NewId();

            encryptor.WriteBase64(id);
            encryptor.WriteVar("OK");
            encryptor.WriteVar(new byte[] { 7, 8 });

            var encrypted = encryptor.ToString();
            var again     = encryptor.ToString();

            Assert.AreEqual(encrypted, again);

            using var decryptTransform = rmCrypto.CreateDecryptor(key, iv);

            var decryptor = new Base64Decryptor(encrypted, decryptTransform);

            Assert.AreEqual(DateTime.MaxValue, decryptor.ReadDateTime());
            Assert.IsTrue(DateTime.MaxValue.Ticks - decryptor.ReadDateTime(1000).Ticks <= 1000);
            Assert.AreEqual(Guid.Empty, decryptor.ReadGuid());
            Assert.AreEqual(byte.MaxValue, decryptor.ReadByte());
            Assert.IsTrue(new byte[] { 2, 3 }.SequenceEqual(decryptor.ReadBytes(2)));
            Assert.AreEqual(int.MaxValue, decryptor.ReadInt32());
            Assert.AreEqual(long.MaxValue, decryptor.ReadInt64());
            Assert.AreEqual(short.MaxValue, decryptor.ReadInt16());
            Assert.AreEqual(int.MinValue, decryptor.ReadInt32());
            Assert.AreEqual(long.MinValue, decryptor.ReadInt64());
            Assert.AreEqual(short.MinValue, decryptor.ReadInt16());
            Assert.AreEqual(id, decryptor.ReadBase64(16));
            Assert.AreEqual("OK", decryptor.ReadVarString());
            Assert.IsTrue(new byte[] { 7, 8 }.SequenceEqual(decryptor.ReadVarBytes()));
        }
        public string PackTokens()
        {
            var tokenList = pageTokens
                            .Where(t => !t.IsEmpty())
                            .ToDictionary(t => t.StreamPlatformName, t => t.Token);

            if (!tokenList.Any())
            {
                return(string.Empty);
            }

            var jsonTokens = JsonConvert.SerializeObject(tokenList);

            var base64Encryptor = new Base64Encryptor(new ToBase64Transform());

            base64Encryptor.WriteVar(jsonTokens);

            return(base64Encryptor.ToString());
        }
Exemplo n.º 4
0
        public void Encrypt_Decrypt_Test()
        {
            var source1         = "helloworld";
            var base64Encryptor = new Base64Encryptor();
            var encrypted1      = base64Encryptor.Base64Encrypt(source1, "utf-8");
            var decrypted1      = base64Encryptor.Base64Decrypt(encrypted1, "utf-8");

            Assert.Equal(source1, decrypted1);


            var source2    = "abc1234";
            var encrypted2 = "YWJjMTIzNA==";

            Assert.Equal(encrypted2, base64Encryptor.Base64Encrypt(source2));

            var encrypted3 = "YnJvdGhlcg==";
            var source3    = "brother";

            Assert.Equal(source3, base64Encryptor.Base64Decrypt(encrypted3));
        }
Exemplo n.º 5
0
        //public static DatabaseInfo DatabaseInfo { get; set; }
        //public static FirebaseAuthInfo FirebaseAuth { get; set; }

        public static void RunKyzinConfigure(this IServiceCollection services, IConfiguration configuration)
        {
            ApplicationName = configuration.GetSection(Constants.ApplicationName).Value;
            DefaultSlogan   = configuration.GetSection(Constants.DefaultSlogan).Value;
            //Encryption = configuration.GetSection(nameof(Encryption)).GetChildren().GetObjectFromSession<EncryptionInfo>();
            //FirebaseAuth = configuration.GetSection(nameof(FirebaseAuthInfo)).GetChildren().GetObjectFromSession<FirebaseAuthInfo>();
            AppSettings = configuration.GetSection(nameof(AppSettings)).GetChildren().GetObjectFromSession <AppSettings>();
            //DatabaseInfo = configuration.GetSection(nameof(DatabaseInfo)).GetChildren().GetObjectFromSession<DatabaseInfo>();
            BaseUrl           = configuration.GetSection(Constants.BaseUrl).Value;
            DateTimeFormat    = configuration.GetSection(Constants.DateTimeFormat).Value;
            TokenValidatePost = configuration.GetSection(Constants.TokenValidatePost).Value;
            PageSize          = int.Parse(configuration.GetSection(Constants.PageSize).Value);
            RelatedPostSize   = int.Parse(configuration.GetSection(Constants.RelatedPostSize).Value);

            var connectionEncrypt = configuration.GetConnectionString("DatabaseLocalConfig");

            if (!string.IsNullOrWhiteSpace(connectionEncrypt))
            {
                ConnectionString = Base64Encryptor.Decrypt(connectionEncrypt);
            }
        }