How can i Decrypt Aes 128 in Universal windows app? (using System.Security.Cryptography not exist)

社会主义新天地 提交于 2019-12-11 19:45:59

问题


I want to decrypt AES 128 in Universal app (win 10), but we do not have AesManaged because this lib doesn't exist in System.Security.Cryptography. So, how can I decrypt my string in UWA c#?

I use this code in windows phone 8 :

public static string DecryptString(string cipherText)
    {
        const string password = "myPass";
        string plaintext = null;
        try
        {
            if (!string.IsNullOrEmpty(cipherText))
            {
                if (cipherText != "")
                {
                    var key = new byte[KeySize];
                    var passwordbytes = Encoding.UTF8.GetBytes(password);

                    for (var i = 0; i < KeySize; i++)
                    {
                        if (i >= passwordbytes.Length)
                        {
                            key[i] = 0;
                            break;
                        }
                        key[i] = passwordbytes[i];
                    }


                    var cipherTextBytes = Convert.FromBase64String(cipherText.Replace("-", "+").Replace("_", "/"));

                    // Declare the string used to hold
                    // the decrypted text.

                    BCEngine
                    // Create an AesCryptoServiceProvider object
                    // with the specified key and IV.
                    using (var aesAlg = new AesManaged())
                    {
                        //aesAlg.Mode = CipherMode.CBC;
                        aesAlg.KeySize = KeySize * 8;

                        // Create a decrytor to perform the stream transform.
                        var decryptor = aesAlg.CreateDecryptor(key, Iv);

                        // Create the streams used for decryption.
                        using (var msDecrypt = new MemoryStream(cipherTextBytes))
                        {
                            using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                            {
                                using (var srDecrypt = new StreamReader(csDecrypt))
                                {
                                    // Read the decrypted bytes from the decrypting stream
                                    // and place them in a string.
                                    plaintext = srDecrypt.ReadToEnd();
                                }
                            }
                        }

                    }
                }
            }
        }
        // ReSharper disable once EmptyGeneralCatchClause
        catch
        {
        }
        return plaintext;

    }

来源:https://stackoverflow.com/questions/33501203/how-can-i-decrypt-aes-128-in-universal-windows-app-using-system-security-crypt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!