Example #1
0
        private static SecureSession CreateSession(string identifier, Instant instant = null)
        {
            if (instant == null)
            {
                instant = new Instant();
            }

            DateTime      now    = DateTime.UtcNow;
            SecureSession result = new SecureSession();

            result.Identifier   = identifier;
            result.CreationDate = now;
            result.LastActivity = now;
            result.TimeOffset   = instant.DiffInMilliseconds(now);
            result.IsActive     = true;

            AsymmetricCipherKeyPair keys = RsaKeyGen.GenerateKeyPair(DefaultKeySize);

            result.AsymmetricKey = keys.ToPem();

            AesKeyVectorPair kvp = new AesKeyVectorPair();

            result.SymmetricKey = kvp.Key.EncryptWithPublicKey(keys.Public);
            result.SymmetricIV  = kvp.IV.EncryptWithPublicKey(keys.Public);

            result.Save();
            return(result);
        }
Example #2
0
        public void EncryptTest()
        {
            string value = "This is a string: ".RandomLetters(16);
            AsymmetricCipherKeyPair keys = RsaKeyGen.GenerateKeyPair(RsaKeyLength._1024);
            string publicPem             = keys.PublicKeyToPem();
            string pemString             = keys.ToPem();

            string encrypted = value.EncryptWithPublicKey(keys);

            OutLine(encrypted, ConsoleColor.Cyan);
            string decrypted = encrypted.DecryptWithPrivateKey(pemString);

            OutLine(decrypted, ConsoleColor.Green);

            Expect.AreEqual(value, decrypted);
        }
        public void LookAtGeneratedRsaPemKeys()
        {
            AsymmetricCipherKeyPair keys = RsaKeyGen.GenerateKeyPair(RsaKeyLength._1024);

            OutLine(keys.Public.ToPem());
        }