Maxime FRAPPAT

Hum …no thanks ! – Lordinaire

Page 17 of 19

[WinRT] Encryption and decryption with AES algorithm

In many case it can be useful to encrypt sensitive data. For a game, this could be the list of unlocked achievement. Just to be clear, this is not an unbreakable solution ! Reflection and reverse engineering can be done on your code to get password and some other stuff in order to break your encryption but… it requires time and skills :)

I found this simple and elegant solution using the AesManaged class on this thread.

public static string Encrypt(string dataToEncrypt, string password, string salt)
{
    // Generate a key and IV from the password and salt
    IBuffer aesKeyMaterial;
    IBuffer iv;
    uint iterationCount = 10000;
    GenerateKeyMaterial(password, salt, iterationCount, out aesKeyMaterial, out iv);

    IBuffer plainText = CryptographicBuffer.ConvertStringToBinary(dataToEncrypt, BinaryStringEncoding.Utf8);

    // Setup an AES key, using AES in CBC mode and applying PKCS#7 padding on the input
    SymmetricKeyAlgorithmProvider aesProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
    CryptographicKey aesKey = aesProvider.CreateSymmetricKey(aesKeyMaterial);

    // Encrypt the data and convert it to a Base64 string
    IBuffer encrypted = CryptographicEngine.Encrypt(aesKey, plainText, iv);
    return CryptographicBuffer.EncodeToBase64String(encrypted);
}

public static string Decrypt(string dataToDecrypt, string password, string salt)
{
    // Generate a key and IV from the password and salt
    IBuffer aesKeyMaterial;
    IBuffer iv;
    uint iterationCount = 10000;
    GenerateKeyMaterial(password, salt, iterationCount, out aesKeyMaterial, out iv);

    // Setup an AES key, using AES in CBC mode and applying PKCS#7 padding on the input
    SymmetricKeyAlgorithmProvider aesProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
    CryptographicKey aesKey = aesProvider.CreateSymmetricKey(aesKeyMaterial);

    // Convert the base64 input to an IBuffer for decryption
    IBuffer ciphertext = CryptographicBuffer.DecodeFromBase64String(dataToDecrypt);

    // Decrypt the data and convert it back to a string
    IBuffer decrypted = CryptographicEngine.Decrypt(aesKey, ciphertext, iv);
    byte[] decryptedArray = decrypted.ToArray();
    return Encoding.UTF8.GetString(decryptedArray, 0, decryptedArray.Length);
}

private static void GenerateKeyMaterial(string password, string salt, uint iterationCount, out IBuffer keyMaterial, out IBuffer iv)
{
    // Setup KDF parameters for the desired salt and iteration count
    IBuffer saltBuffer = CryptographicBuffer.ConvertStringToBinary(salt, BinaryStringEncoding.Utf8);
    KeyDerivationParameters kdfParameters = KeyDerivationParameters.BuildForPbkdf2(saltBuffer, iterationCount);

    // Get a KDF provider for PBKDF2, and store the source password in a Cryptographic Key
    KeyDerivationAlgorithmProvider kdf = KeyDerivationAlgorithmProvider.OpenAlgorithm(KeyDerivationAlgorithmNames.Pbkdf2Sha256);
    IBuffer passwordBuffer = CryptographicBuffer.ConvertStringToBinary(password, BinaryStringEncoding.Utf8);
    CryptographicKey passwordSourceKey = kdf.CreateKey(passwordBuffer);

    // Generate key material from the source password, salt, and iteration count.  Only call DeriveKeyMaterial once,
    // since calling it twice will generate the same data for the key and IV.
    int keySize = 256 / 8;
    int ivSize = 128 / 8;
    uint totalDataNeeded = (uint)(keySize + ivSize);
    IBuffer keyAndIv = CryptographicEngine.DeriveKeyMaterial(passwordSourceKey, kdfParameters, totalDataNeeded);

    // Split the derived bytes into a seperate key and IV
    byte[] keyMaterialBytes = keyAndIv.ToArray();
    keyMaterial = WindowsRuntimeBuffer.Create(keyMaterialBytes, 0, keySize, keySize);
    iv = WindowsRuntimeBuffer.Create(keyMaterialBytes, keySize, ivSize, ivSize);
}

Really easy to use, you just need a couple password/salt :

string encryptString = Encrypt("my text", "mypassword", "mysalt");
string decryptString = Decrypt(encryptString, "mypassword", "mysalt");

Have fun !

[WinRT] Vectors with C#/XAML

Kloone ? :)

One of the most useful tools (for a designer ;)) in Blend 4 is the ability to import Photoshop (*.psd) or Illustrator (*.ai) files. This feature is not available (yet?) in Blend for Visual Studio 2012, so if you want to deal with it, you need to install both versions :( Fortunately, there is another way !

AI -> SVG -> XAML

So, your favorite designer has made a beautiful ghost with Illustrator so you need to must add it in your application. The first step is to save it in SVG format :

Select SVG type

Then, you need to use a SVG to XAML converter like this one : http://www.graphspe.com/svg-to-xaml-converter#/Converter

Use the Silverlight conversion

Now, you have XAML code :)


<Canvas xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Calque_1" Width="595.28" Height="841.89" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
 <Path Stroke="#FF000000" Data="M167.177,525C229.126,445.373 163.66,288.199 301.235,288 416.989,287.776 346.298,467.496 420.035,524 442.735,541.394 403.605,556.073 378.111,533.386 360.343,517.574 354.851,556.115 334.553,558 313.121,559.99 301.107,544.114 291.426,541 281.745,537.886 272.515,558.322 252,558 234.719,557.503 229.897,525.513 216.223,533 197.132,545.274 148.707,546.304 167.177,525z" />
 <Path Stroke="#FF000000" Data="M279.296,370.992C286.643,370.992 292.6,377.04 292.6,384.501 292.6,391.962 286.643,398.01 279.296,398.01 271.949,398.01 265.992,391.962 265.992,384.501 265.992,377.041 271.949,370.992 279.296,370.992z" />
 <Path Stroke="#FF000000" Data="M309.704,370.992C317.052,370.992 323.008,377.04 323.008,384.501 323.008,391.962 317.052,398.01 309.704,398.01 302.356,398.01 296.4,391.962 296.4,384.501 296.4,377.041 302.356,370.992 309.704,370.992z" />
 <Path Stroke="#FF000000" Data="M295,365.245C295,365.245 301.323,341.999 317,341 331.689,341 341.793,357.345 342,376 342.096,384.591 342.253,410.698 318.59,414 298.209,415.382 295,392 295,392" />
 <Path Stroke="#FF000000" Data="M295,365.245C295,365.245 288.676,341.999 273,341 258.31,341 248.208,357.345 248,376 247.904,384.591 247.747,410.698 271.41,414 291.791,415.382 295,392 295,392" />
</Canvas>

This has required more steps but the goal was reached.

Have fun !

Page 17 of 19

Powered by WordPress & Theme by Anders Norén

%d bloggers like this: