How can I generate the exact same ciphertext in WinRT as this Windows Phone encryption code using AesManaged ? RRS feed

  • Question

  • Hi all,

    We are using Aes Encryption for Windows rt 8.1. We alradey tried same AES Encryption in windows phone 8.0 and we got encrypted result. 

    following link we followed to generate AES Encrypted result for windows RT,





    the code snippet we followed in windows phone 8.0 is,


    and we observed following result for windows phone application,

    public string EncryptString(string Data, string Key)
                string str = "";
                    if (Data != null)
                        using (Aes aes = new AesManaged())
                            aes.Key = Encoding.Unicode.GetBytes(Key);
                            aes.IV = aes.Key;
                            using (MemoryStream encryptionStream = new MemoryStream())
                                using (CryptoStream encrypt = new CryptoStream(encryptionStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
                                    byte[] utfD1 = UTF8Encoding.UTF8.GetBytes(Data);
                                    encrypt.Write(utfD1, 0, utfD1.Length);
                                string tobase641 = Convert.ToBase64String(encryptionStream.ToArray());
                                str = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(tobase641));

                    throw new Exception("Key Expired");
                return str;

    Key : An3$0$13

    Input : windowsphone

    Result : QytRSFFMY2NSdm03a0NaOEVpZlg5QT09

    But by using windows rt , code snippet

    public static string Encrypt(string toEncrypt, string key )
                    // Get the MD5 key hash (you can as well use the binary of the key string)
                    var keyHash = GetMD5Hash(key);

                    // Create a buffer that contains the encoded message to be encrypted.
                    var toDecryptBuffer = CryptographicBuffer.ConvertStringToBinary(toEncrypt, BinaryStringEncoding.Utf8);

                    //string algo = SymmetricAlgorithmNames.AesCbcPkcs7;
                    string algo = SymmetricAlgorithmNames.DesCbcPkcs7;
                    // Open a symmetric algorithm provider for the specified algorithm.
                    var aes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(algo);

                    // Create a symmetric key.
                    var symetricKey = aes.CreateSymmetricKey(keyHash);

                    // The input key must be securely shared between the sender of the cryptic message
                    // and the recipient. The initialization vector must also be shared but does not
                    // need to be shared in a secure manner. If the sender encodes a message string
                    // to a buffer, the binary encoding method must also be shared with the recipient.
                    var buffEncrypted = CryptographicEngine.Encrypt(symetricKey, toDecryptBuffer, null);

                    // Convert the encrypted buffer to a string (for display).
                    // We are using Base64 to convert bytes to string since you might get unmatched characters
                    // in the encrypted buffer that we cannot convert to string with UTF8.
                    var strEncrypted = CryptographicBuffer.EncodeToBase64String(buffEncrypted);

                    return strEncrypted;
                catch (Exception ex)
                    // MetroEventSource.Log.Error(ex.Message);
                    return "";

    private static IBuffer GetMD5Hash(string key)
                // Convert the message string to binary data.
                IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);

                // Create a HashAlgorithmProvider object.
                HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);

                // Hash the message.
                IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);

                // Verify that the hash length equals the length specified for the algorithm.
                if (buffHash.Length != objAlgProv.HashLength)
                    throw new Exception("There was an error creating the hash");

                return buffHash;

    for same input values, our result is differing,

    Result : Q7o76aoyqhwJOp9zAlxnVA==

    I followed these encryption methods for generating Aes Encrypted text. 

    we want same result for both windows phone and windows rt. 

    Please help us.

    Thanks and regards.

    Thursday, September 4, 2014 8:02 AM


  • You're using MD5 hash in one version and AES in the other... you need to use AES in both.

    (Neither of the blogs you referenced use MD5 so I'm not sure why you have that in your code.)

    Eric Fleck, Windows Store and Windows Phone Developer Support. If you would like to provide feedback or suggestions for future improvements to the Windows Phone SDK please go to http://wpdev.uservoice.com/ where you can post your suggestions and/or cast your votes for existing suggestions.

    Friday, September 5, 2014 8:15 PM