Windows RT项目中的解密

时间:2014-11-25 14:43:23

标签: c# windows-rt

此代码在Windows Phone Silverlight项目中运行良好。 但这不适用于Windows RT项目。 它的syay加密和Aes和AesManaged类丢失等。 请帮我谢谢。 我真的不需要密码和盐。它只是简单的取字符串并解密它。

public class DecryptionHelper
{
    public static string Decrypt(string base64StringToDecrypt)
    {
        if (string.IsNullOrEmpty(base64StringToDecrypt))
            return string.Empty;
        //Set up the encryption objects
        using (Aes acsp = GetProvider(Encoding.UTF8.GetBytes
                                                    (Constants.EncryptionKey)))
        {
            byte[] RawBytes = Convert.FromBase64String(base64StringToDecrypt);
            ICryptoTransform ictD = acsp.CreateDecryptor();
            //RawBytes now contains original byte array, still in Encrypted state
            //Decrypt into stream
            MemoryStream msD = new MemoryStream(RawBytes, 0, RawBytes.Length);
            CryptoStream csD = new CryptoStream(msD, ictD, CryptoStreamMode.Read);
            //csD now contains original byte array, fully decrypted
            //return the content of msD as a regular string
            return (new StreamReader(csD)).ReadToEnd();
        }
    }

    private static Aes GetProvider(byte[] key)
    {
        Aes result = new AesManaged();
        result.GenerateIV();
        result.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        byte[] RealKey = GetKey(key, result);
        result.Key = RealKey;
        return result;
    }

    private static byte[] GetKey(byte[] suggestedKey, SymmetricAlgorithm p)
    {
        byte[] kRaw = suggestedKey;
        List<byte> kList = new List<byte>();
        for (int i = 0; i < p.LegalKeySizes[0].MinSize; i += 8)
        {
            kList.Add(kRaw[(i / 8) % kRaw.Length]);
        }
        byte[] k = kList.ToArray();
        return k;
    }
}

0 个答案:

没有答案