This quick-start provides just enough information to get you started using IQryptSiaqodb in C# within VisualStudio.

Downloading the C# libraries and add references

You will have to download .NET assemblies from here. IQryptSiaqodb dependent on following libraries:

  • net version is dependent on Newtonsoft.Json.dll, Siaqodb.dll and SiaqodbPortable.dll
  • universal version is dependent on Newtonsoft.Json.dll, Siaqodb.dll and SiaqodbPortable.dll
  • unity version is dependent on Siaqodb.dll

You can find those assemblies from Nuget or from Siaqodb website.

In VisualStudio add all assemblies as references (for required platform).

Add Required using Statements

As a minimum you will need the following using statements:

using IQryptSiaqodb;
using Sqo;
using Sqo.Documents;

Encryption settings

With IQryptSiaqodb you can encrypt Content and Tags of Siaqodb Documents with different encryption schemes: OPE, DET, RND.

  • OPE - order preserving encryption
  • DET - deterministic encryption
  • RND - randomized encryption

The document content can be encrypted only with RND scheme: Example:

 IQryptSiaqodbConfigurator.EncryptDocumentContent("yourBucketName");

The Tags of a Document can be encrypted with any encryption scheme RND, DET, OPE:

    IQryptSiaqodbConfigurator.EncryptTag( myInt, EncryptionType.OPE, typeof(int));
    IQryptSiaqodbConfigurator.EncryptTag( myfloat, EncryptionType.OPE, typeof(float));
    IQryptSiaqodbConfigurator.EncryptTag( myDateTime, EncryptionType.DET, typeof(DateTime));

After you set how tags should be encrypted, you will have to set the cipher and the encryption key:

 IQryptSiaqodbConfigurator.SetEncryptionChiper(Cipher.AES256, "my_super_secret");

*On production apps get the encryption key from a safe location- see Vault (do not embed it into the code).

That's it, now you can open Siaqodb database and store/retrieve documents.

Create and store an IQryptDocument

IQryptDocument is derived from Document class and transparently encrypt Content and Tags without any extra intervention. Here is how your would store a Document using Siaqodb:

IBucket bucket=siaqodb.Documents["invoices"];
Invoice inv = new Invoice { CustomerName = "My Company", InvoiceDate=DateTime.Now, Total = 2390 };

 Document document=new Document();
 document.Key="Invoice-324r";
 document.SetContent<Invoice>(inv);
 document.SetTag("customer_code", "CU234");
 bucket.Store(document);

And here is an example of creating and store an IQryptDocument using IQryptSiaqodb SDK

IQryptSiaqodbConfigurator.EncryptDocumentContent("invoices");
IQryptSiaqodbConfigurator.EncryptTag( "customer_code", EncryptionType.DET, typeof(string));

......

IBucket bucket=siaqodb.Documents["invoices"];
Invoice inv = new Invoice { CustomerName = "My Company", InvoiceDate=DateTime.Now, Total = 2390 };

 IQryptDocument document=new IQryptDocument ();
 document.Key="Invoice-324r";
 document.SetContent<Invoice>(inv);//content will be encrypted with RND
 document.SetTag("customer_code", "CU234"); //tag will be encrypted with DET 
 bucket.Store(document);

Find an Existing IQryptDocument

In this example we will read back an IQryptDocument assuming we know the Key value.

Here is an example how you would do with Siaqodb:

 Document document = bucket.Load("Invoice-324r");
 Invoice invoice = document.GetContent<Invoice>();

And here is an example by using IQryptSiaqodb SDK:

  IQryptDocument document = bucket.Load("Invoice-324r") as IQryptDocument;
 Invoice invoice = document.GetContent<Invoice>();//content is automatically decrypted

Update an Existing IQryptDocument

To update an existing Document using Siaqodb:

Document document = bucket.Load("Invoice-324r");

Invoice invoice = document.GetContent<Invoice>();
invoice.InvoiceDate=DateTime.Now.AddDays(-1);
document.SetContent<Invoice>(invoice);

bucket.Store(document);

And here using IQryptSiaqodb SDK:


IQryptDocument document = bucket.Load("Invoice-324r") as IQryptDocument;

Invoice invoice = document.GetContent<Invoice>();//automatically decrypted
invoice.InvoiceDate=DateTime.Now.AddDays(-1);
document.SetContent<Invoice>(invoice); //automatically encrypted

bucket.Store(document);

Delete an Existing IQryptDocument

To delete an existing IQryptDocument use same method as you would delete Siaqodb Document:

bucket.Delete("Invoice-324r");

Full Sample Program

using IQryptSiaqodb;
using Newtonsoft.Json;
using Sqo;
using Sqo.Documents;
using System;
using System.Text;


namespace ConsoleApplication1
{
    internal class MyJsonSerializer : IDocumentSerializer
    {

        public object Deserialize(Type type, byte[] objectBytes)
        {
            string jsonStr = Encoding.UTF8.GetString(objectBytes);
            return JsonConvert.DeserializeObject(jsonStr.TrimEnd('\0'), type);
        }
        public byte[] Serialize(object obj)
        {
            string jsonStr = JsonConvert.SerializeObject(obj, Formatting.Indented);
            return Encoding.UTF8.GetBytes(jsonStr);
        }

    }
    public class Invoice
    {
        public string CustomerName { get; set; }
        public int InvoiceNumber { get; set; }
        public decimal Total { get; set; }
        public DateTime InvoiceDate { get; set; }

    }

    static class Program2
    {
        public static void Main(string[] args)
        {

            SiaqodbConfigurator.SetDocumentSerializer(new MyJsonSerializer());

            //IQrypt settings
            IQryptSiaqodbConfigurator.EncryptDocumentContent("encrypted_invoices");
            IQryptSiaqodbConfigurator.EncryptTag("customer_code", IQrypt.EncryptionType.DET, typeof(string));
            IQryptSiaqodbConfigurator.SetEncryptionChiper(IQrypt.Cipher.AES256, "my_super_secret");

            using (Siaqodb siaqodb = new Siaqodb(@"c:\DATA\"))
            {
                IBucket bucket = siaqodb.Documents["encrypted_invoices"];

                Invoice inv = new Invoice { CustomerName = "My Company", InvoiceDate = DateTime.Now, Total = 2390 };

                IQryptDocument document = new IQryptDocument();
                document.Key = "Invoice-324r";
                document.SetContent<Invoice>(inv); //content will be encrypted with RND
                document.SetTag("customer_code", "CU234"); //tag will be encrypted with DET 

                bucket.Store(document);

                IQryptDocument documentLoaded = bucket.Load("Invoice-324r") as IQryptDocument;
                Invoice invoiceLoaded = documentLoaded.GetContent<Invoice>();//content is decrypted
                string customerCode = documentLoaded.GetTag<string>("customer_code");//tag is decrypted
                invoiceLoaded.InvoiceDate = DateTime.Now.AddDays(-1);
                documentLoaded.SetContent<Invoice>(invoiceLoaded);

                bucket.Store(documentLoaded);

                bucket.Delete(documentLoaded);
            }

        }
    }
}