locked
Cryptography for Windows 8?

    Question

  • Hi all, I am trying to encrypt & decrypt  using AesManaged. I am able to do in Windows Phone with following code:

    Windows Phone 8 Code:

    public static string EncryptString(string Data, string Key)
            {
                string str = "";
                try
                {
                    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);
                                    encrypt.FlushFinalBlock();
                                }
                                string tobase641 = Convert.ToBase64String(encryptionStream.ToArray());
                                str = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(tobase641));
                            }
                        }
                    }
    
                }
                catch
                {
                    throw new Exception("Key Expired");
                }
                return str;
            }

    When I tried same code in windows 8. The namespace not existed. So I have searched and find other encrypting namespace. but this encrypting method giving different result.

    Windows 8 Encryption:

    public static string Encrypt(string toEncrypt, string key=AppConstants.EncryptionKey)
            {
                try
                {
                    // 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;
                    // 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 "";
                }
            }
    AppConstants.EncryptionKey="An3$0$13"

    Please tell me Where I am doing wrong. Two namespaces are giving two different values. I need AesManaged out put for server.

    Thanks in advance.


    Pallam Madhukar Windows Phone Developer

    Wednesday, August 27, 2014 10:59 AM

Answers