Code Sample

Create a data vault for encrypted storage of application data.

 public void DataVaultSampleCode()
   {
      try
      {
         DataVault oDataVault = null;

         // If this dataVault already exists, then get it by calling getVault()
         // Else create this new dataVault by calling createVault()
         if ( DataVault.VaultExists( "DataVaultExample" ) )
            oDataVault = DataVault.GetVault( "DataVaultExample" );
         else
            oDataVault = DataVault.CreateVault( "DataVaultExample", "password!1A", "saltD#ddg#k05%gnd[!1A" );

         // Call setLockTimeout(). This allows you to set the timeout of the vault in seconds
         oDataVault.SetLockTimeout( 1500 );
         int iTimeout = oDataVault.GetLockTimeout();

         // Call setRetryLimit(). This allows you to set the number of retries before the vault is destroyed
         oDataVault.SetRetryLimit( 10 );
         int iRetryLimit = oDataVault.GetRetryLimit();

         // Call setPasswordPolicy(). The passwordPolicy also includes the retryLimit and LockTimeout that we set above.
         DataVault.PasswordPolicy oPasswordPolicy = new DataVault.PasswordPolicy();
         oPasswordPolicy.defaultPasswordAllowed = true;
         oPasswordPolicy.minimumLength = 4;
         oPasswordPolicy.hasDigits = true;
         oPasswordPolicy.hasUpper = true;
         oPasswordPolicy.hasLower = true;
         oPasswordPolicy.hasSpecial = true;
         oPasswordPolicy.expirationDays = 20;
         oPasswordPolicy.minUniqueChars = 3;
         oPasswordPolicy.lockTimeout = 1600;
         oPasswordPolicy.retryLimit = 20;

         // SetPasswordPolicy() will always lock the vault to ensure the old password
         // conforms to the new password policy settings.
         oDataVault.SetPasswordPolicy( oPasswordPolicy );

         // We are now locked and need to unlock before we can access the vault.
         oDataVault.Unlock( "password!1A", "saltD#ddg#k05%gnd[!1A" );

         // Call getPasswordPolicy() to return the current password policy settings.
         DataVault.PasswordPolicy oCurrentPolicy = oDataVault.GetPasswordPolicy();

         // Call setString() by giving it a name:value pair to encrypt and persist 
         // a string data type within your dataVault.
         oDataVault.SetString( "stringName", "stringValue" );

         // Call getString to retrieve the string we just stored in our data vault!
         String storedStringValue = oDataVault.GetString( "stringName" );

         // Call setValue() by giving it a name:value pair to encrypt and persist 
         // a binary data type within your dataVault.
         byte[] binaryValue = { 1, 2, 3, 4, 5, 6, 7 };
         oDataVault.SetValue( "binaryName", binaryValue );

         // Call getValue to retrieve the binary we just stored in our data vault!
         byte[] storedBinaryValue = oDataVault.GetValue( "binaryName" );

         // Call getDataNames to retrieve all stored element names from our data vault.
         DataVault.DVDataName[] dataNameArray = oDataVault.GetDataNames();
         for ( int i = 0; i < dataNameArray.GetLength( 0 ); i++ )
         {
            if ( dataNameArray[ i ].type == DataVault.DVDataType.String )
            {
               String thisStringValue = oDataVault.GetString( dataNameArray[ i ].name );
            }
            else if ( dataNameArray[ i ].type == DataVault.DVDataType.Binary )
            {
               byte[] thisBinaryValue = oDataVault.GetValue( dataNameArray[ i ].name );
            }
         }

         // Call changePassword with 2 parameters. Vault must be unlocked.
         // If you pass null parameters as your new password or your new salt,
         // it will generate a default password or default salt, respectively.
         oDataVault.ChangePassword( null, null );

         // Call isDefaultPasswordused() to see if we are using an automatically
         // generated password (which we are).
         bool isDefaultPasswordUsed = oDataVault.IsDefaultPasswordUsed();

         // Lock the vault.
         oDataVault.Lock();

         // Call changePassword with 4 parameters even if the vault is locked.
         // Here, we pass null for oldSalt and oldPassword because defaults were used.
         oDataVault.ChangePassword( null, null, "password!1A", "saltD#ddg#k05%gnd[!1A" );

         // Call isDefaultPasswordused() and we will see that the default password is NOT used anymore.
         isDefaultPasswordUsed = oDataVault.IsDefaultPasswordUsed();
      }
      catch ( DataVaultException dvex )
      {
         // Handle datavault exception.
      }
      catch ( Exception ex )
      {
         // Handle exception.
      }
      finally
      {
         // Because this is a test example, we will delete our vault at the end.
         // This means we will forever lose all data we persisted in our data vault.
         if ( DataVault.VaultExists( "DataVaultExample" ) )
            DataVault.DeleteVault( "DataVaultExample" );
      }
   }