Example #1
0
        public static string EncryptPassword(MembershipSettings membershipSettings, string password, out int crypto, out string key, out string iv)
        {
            switch (membershipSettings.PasswordFormat)
            {
            // Clear
            case 0:
                crypto = 0;
                key    = null;
                iv     = null;
                return(password);

            // Hashed
            case 1:
                string salt = Convert.ToBase64String(new RandomNumGenerator().Generate());
                crypto = membershipSettings.PasswordHash;
                key    = null;
                iv     = salt;
                Hasher hasher = CreateHasher(crypto);
                return(hasher.Hash(password, salt));

            // Encrypted
            case 2:
                crypto = membershipSettings.PasswordCrypto;
                SymmetricCryptor cryptor = CreateSymmetricCryptor(crypto);
                key = Convert.ToBase64String(cryptor.Key);
                iv  = Convert.ToBase64String(cryptor.IV);
                return(cryptor.Encrypt(password));

            default:
                throw new NotSupportedException(membershipSettings.PasswordFormat.ToString());     // never
            }
        }
Example #2
0
        private static SymmetricCryptor CreateSymmetricCryptor(int crypto)
        {
            SymmetricCryptor cryptor = null;

            switch (crypto)
            {
            case (int)SymmetricCryptoName.Aes:
                cryptor = new AesCryptor();
                break;

            case (int)SymmetricCryptoName.DES:
                cryptor = new DESCryptor();
                break;

            case (int)SymmetricCryptoName.RC2:
                cryptor = new RC2Cryptor();
                break;

            case (int)SymmetricCryptoName.Rijndael:
                cryptor = new RijndaelCryptor();
                break;

            case (int)SymmetricCryptoName.TripleDES:
                cryptor = new TripleDESCryptor();
                break;
            }
            return(cryptor);
        }
Example #3
0
 public static bool ComparePassword(string encryptedPassword, int crypto, string key, string iv, string password)
 {
     if (string.IsNullOrWhiteSpace(key))
     {
         if (string.IsNullOrWhiteSpace(iv))
         {
             // Clear
             return(password == encryptedPassword);
         }
         else  // Hash
         {
             Hasher hasher = CreateHasher(crypto);
             string salt   = iv;
             return(hasher.Hash(password, salt) == encryptedPassword);
         }
     }
     else // Crypto
     {
         SymmetricCryptor cryptor = CreateSymmetricCryptor(crypto, key, iv);
         return(cryptor.Encrypt(password) == encryptedPassword);
     }
 }
Example #4
0
        private string GetDBSQLConnectionString(bool isDMConnection)
        {
            string strDBInfo           = string.Empty;
            string decryptedDBInfo     = string.Empty;
            string currentDirPath      = GetCurrentDirPath();
            string connectionParameter = string.Empty;

            using (StreamReader textReader = new StreamReader(currentDirPath))
            {
                strDBInfo = textReader.ReadToEnd();
            }

            SymmetricCryptor symmetricCryptor = new SymmetricCryptor(EncryptionAlgorithm.Rijndael);

            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(symmetricCryptor.DecryptFromString(strDBInfo));

            if (isDMConnection)
            {
                connectionParameter = GetDMSQLConnectionParameter();
            }
            else
            {
                connectionParameter = GetICSQLConnectionParameter();
            }

            XmlNode rootNode = xmlDocument.DocumentElement;
            XmlNode xmlNode  = rootNode.SelectSingleNode(connectionParameter);

            string dbPwd = xmlNode.InnerText;

            DBSQLConnectionString = "Data Source = " + dbServerName + ";Database = " + dbName + ";UID = " + dbName + ";Password = "******";";

            return(DBSQLConnectionString);
        }