1 Reply Latest reply on Jun 28, 2012 10:54 AM by "Andrei Costache, Oracle-Oracle"

    BTREE - C# Api, Can't encrypt database

      Hi. I've a problem using berkeley api for C#. I'm trying to create an encrypted db.
      That's an example of code I'm trying using:

      BTreeDatabase btreeDB;
      DatabaseEntry key ;
      DatabaseEntry data;

      public void dummy()
      dbo.dbs = dbs;
      String dbFileName = "Messaggi.db";
      BTreeDatabaseConfig cfg = new BTreeDatabaseConfig();
      DatabaseEnvironmentConfig dbCfg = new DatabaseEnvironmentConfig();
      if (File.Exists(dbFileName))
      MessageBox.Show("Elimino il database");


      cfg.Creation = CreatePolicy.ALWAYS;
      cfg.Encrypted = true;//?? it's needed?

      cfg.SetEncryption("123456789", EncryptionAlgorithm.DEFAULT);

      btreeDB = BTreeDatabase.Open(dbFileName, cfg);

      catch (BerkeleyDB.DatabaseException dbex)


      but it throw that's exception:
      Eccezione first-chance di tipo 'BerkeleyDB.DatabaseException' in libdb_dotnet53.dll
      +'ExportMessages.vshost.exe' (Gestito (v4.0.30319)): caricato 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_it_b77a5c561934e089\mscorlib.resources.dll'+
      in BerkeleyDB.DatabaseException.ThrowException(Int32 err) in D:\work\releases\db-5.3.21\lang\csharp\src\DatabaseException.cs:riga 72
      in BerkeleyDB.Internal.DB.open(DB_TXN txn, String file, String database, DBTYPE type, UInt32 flags, Int32 mode) in D:\work\releases\db-5.3.21\lang\csharp\src\Internal\DB.cs:riga 261
      in BerkeleyDB.BTreeDatabase.Open(String Filename, String DatabaseName, BTreeDatabaseConfig cfg, Transaction txn) in D:\work\releases\db-5.3.21\lang\csharp\src\BTreeDatabase.cs:riga 230
      in BerkeleyDB.BTreeDatabase.Open(String Filename, BTreeDatabaseConfig cfg) in D:\work\releases\db-5.3.21\lang\csharp\src\BTreeDatabase.cs:riga 97
      +in [...]+

      It's my mistake?
      Thank you.
        • 1. Re: BTREE - C# Api, Can't encrypt database
          "Andrei Costache, Oracle-Oracle"
          Hi Massimiliano,

          We had a bug related to encrypting database when using the C# API, bug #18891, but that was fixed in 5.2 (see the entry in the 5.2 change log)
          +Fixed a bug where encryption could not be enabled for individual databases in an encrypted environment. [#18891]+
          , hence the fix is part of Berkeley DB 5.3.21.

          You haven't posted a complete stack trace or a small stand-alone testcase, so I cannot know exactly what is the problem.
          But, here are the rules around using encryption:

          A. When the database is part of an environment
          - you will need to specify the encryption password and encryption algorithm using the DatabaseEnvironmentConfig.SetEncryption() method
          - if you want a database in the environment to be encrypted (using the password and algorithm specified for the environment), you will need to set the DatabaseConfig.Encrypted property to true
          - note that it is an error to try to set an encryption password/algorithm using the DatabaseConfig.SetEncryption() method for a database that is part of an environment, because the database will use the encryption password/algorithm specified for the environment
          - here is a snippet of code showing how to correctly encrypt a database that is part of an environment:
               DatabaseEnvironmentConfig envCfg;
               envCfg = new DatabaseEnvironmentConfig();
               envCfg.Create = true;
               envCfg.UseMPool = true;
               envCfg.SetEncryption("123456789", EncryptionAlgorithm.DEFAULT);
               env = DatabaseEnvironment.Open(envHome, envCfg);
               BTreeDatabaseConfig btreeCfg = new BTreeDatabaseConfig();
               btreeCfg.Creation = CreatePolicy.IF_NEEDED;
               btreeCfg.Env = env;
               btreeCfg.Encrypted = true;
               db = BTreeDatabase.Open(dbName, btreeCfg);
          B. When the database is NOT part of an environment
               BTreeDatabaseConfig btreeCfg2 = new BTreeDatabaseConfig();
               btreeCfg2.Creation = CreatePolicy.IF_NEEDED;
               btreeCfg2.Encrypted = true;
               btreeCfg2.SetEncryption("123456789", EncryptionAlgorithm.DEFAULT);
               db2 = BTreeDatabase.Open(db2Name, btreeCfg2);
          This should clarify things. If not or you have additional questions let me know.