CryptographicEngine Decrypt throw值不在Decrypt mathod的预期范围异常范围内

时间:2015-04-29 11:30:50

标签: c# encryption windows-runtime cryptography windows-phone-8.1

我尝试使用该代码但是当Decrypt行发生时,我得到的值不在Decrypt mathod的预期范围异常范围内

这是单身类,所以他创建了一次密钥

{
    public class CryptographyProvider
    {
        private CryptographicKey key;
        public CryptographyProvider()
        {
            String AsymmetricAlgName = Windows.Security.Cryptography.Core.AsymmetricAlgorithmNames.RsaPkcs1;
            AsymmetricKeyAlgorithmProvider asym = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgName);
             key = asym.CreateKeyPair(512);

        }
        public string Encrypt(string source)
        {
            IBuffer buf = CryptographicBuffer.ConvertStringToBinary(source, BinaryStringEncoding.Utf16BE);
            IBuffer enc = CryptographicEngine.Encrypt(key, buf, null);
            byte[] encryptedbyteArr;
            CryptographicBuffer.CopyToByteArray(enc, out encryptedbyteArr);
            var ret =  Convert.ToBase64String(encryptedbyteArr);

            return ret;
        }

        public string Decrypt(string source)
        {
            IBuffer buf = CryptographicBuffer.ConvertStringToBinary(source, BinaryStringEncoding.Utf16BE);
            IBuffer enc = CryptographicEngine.Decrypt(key, buf, null);
            byte[] encryptedbyteArr;
            CryptographicBuffer.CopyToByteArray(enc, out encryptedbyteArr);
            var ret = Convert.ToBase64String(encryptedbyteArr);
            return ret;
        }
    }
}

我的代码有什么问题?

0 个答案:

没有答案