Compatible Encryption/Decryption for Windows Store Apps and Full .NET Framework

I recently needed to encrypt some data in a Windows Store App and decrypt it on the server in a full .NET Framework application.  The Encryption APIs available in the two environments are different, so I came up with the following class that implements a compatible pair of encryption and decryption functions to enable encrypting in a Store App and Decrypting on the server or vice versa.

It's a single file, and has conditional compilation directives so it works on both kinds of projects.  It uses AES 256-bit encryption and you must share both a 256bit key and an and initialization vector between your two projects.  Typically the IV is shared in code, and the key is acquired at runtime.

The result is a single object called EncDec in the attached EncDec.cs file. that you new-up with the IV and the key, and then use .Encrypt() and .Decrypt(). All data is passed as strings, base64 endoded if needed.

Here’s how to call it

     string plainText = "Hello, World!";
    
    string TestKey = "gFlMfLZu4unBGfBh9weIuLwlcXHSa59vxyUQWM3yh1M=";
    string TestIV = "rRNgGypocle9VG9bth6kxg==";
    
    var ed = new EncDec(TestKey, TestIV);
    
    var cypherText = ed.Encrypt(plainText);
    var plainText2 = ed.Decrypt(cypherText);
    

David

EncDec.cs

Comments

  • Anonymous
    August 24, 2014
    looks useful.  Particularly base64 endoded data.