/// <summary> /// Adds Aes Key to KeyManager if you pass null as second parameter the Key will be generated automatically. /// </summary> /// <param name="alias">The name of a Key.</param> /// <param name="key">The binary content of Key.</param> /// <param name="password">The password that will be used to encrypt an Key value.</param> public void AddAesKey(string alias, byte[] key = null, string password = null) { if (key == null) { if (!this.Exists(alias, password)) { KeyManager.CreateAesKey(256, alias, new Policy(password, true)); // Creates new Aes key if it`s not already inside KeyManager. } } else { Key k = new Key(key, KeyType.Aes, password); // Creates Key (generic class for holding Keys e.g. Aes,Rsa. try { if (!this.Exists(alias, password)) { KeyManager.Save(alias, k, new Policy(password, true)); // Saves Created key to KeyManager. } } catch (Exception ex) { Log.Error("SecureRepository_KEYS", ex.Message); } } }
/// <summary> /// Creates Aes Key if one does not already exists inside KeyManager under _alias. /// </summary> private void CreateAesKey() { bool hasKey = true; try { Key k; k = KeyManager.Get(this.alias, this.password); // Checks if Aes Key exists inside KeyManager. // Checks if Key is proper Aes Key, if not we will have to delete it and generate proper one. if (k.Type != KeyType.Aes) { hasKey = false; this.keys.Remove(this.alias); } } catch { // e.g. Key is not in KeyManager. hasKey = false; } // Generates new Aes key. if (!hasKey) { try { KeyManager.CreateAesKey(256, this.alias, new Policy(this.password, true)); } catch (Exception ex) { Log.Error("SecureRepository_CRYPTOGRAPHY", ex.Message); } } }