Example #1
0
        private void StoringPasswordsUsingSaltedHashes()
        {
            // using salted hashes

            Console.WriteLine("Encrypting Passwords started");

            Console.WriteLine(string.Empty);

            User U = new User();

            Console.WriteLine("please enter the user name");
            U.UserName = Console.ReadLine().Trim();

            //Console.WriteLine("please enter the password");
            //U.Password = Console.ReadLine().Trim();

            byte[] salt       = CryptographyExample.GenerateSalt();
            byte[] saltedHash = CryptographyExample.HashPasswordWithSalt(Encoding.UTF8.GetBytes(_tempPassword), salt);
            U.Password = Convert.ToBase64String(saltedHash);

            JSONDataBase JsonDB = new JSONDataBase();

            JsonDB.AddUser(U);


            Console.WriteLine("Encrypting Passwords ended");
            Console.WriteLine(string.Empty);
        }
        public void AssignNewRSAKey(bool storeKeysInDB = false)
        {
            // here we are saying we want to use a 2048 bit key
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048))
            {
                RSA.PersistKeyInCsp = false;
                _publicKey          = RSA.ExportParameters(false);
                _privateKey         = RSA.ExportParameters(true);

                if (storeKeysInDB)
                {
                    RSAKeys rsaKeys = new RSAKeys();
                    rsaKeys.RsaPrivateKey = RSA.ToXmlString(true);  // true gets the private key
                    rsaKeys.RsaPublicKey  = RSA.ToXmlString(false); // false gets the public key

                    _jsonDataBaseInstance = new JSONDataBase();
                    _jsonDataBaseInstance.AddRSAKey(rsaKeys);
                }
            }
        }