Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RSACryptServiceProvider.EncryptValue返回NotSupportedException_C#_.net_Cryptography_Rsacryptoserviceprovider - Fatal编程技术网

C# RSACryptServiceProvider.EncryptValue返回NotSupportedException

C# RSACryptServiceProvider.EncryptValue返回NotSupportedException,c#,.net,cryptography,rsacryptoserviceprovider,C#,.net,Cryptography,Rsacryptoserviceprovider,我有一个.NET 4.5 MVC应用程序,我正在尝试使用用户RSACryptServiceProvider加密一些消息。但是,当我尝试使用EncryptValue()时,我得到一个NotSupportedException。这是我的代码: RSACryptoServiceProvider RSA = GetKeyFromContainer(_CONTAINER_NAME); byte[] byteEncrypted = RSA.EncryptValue(byteData);

我有一个.NET 4.5 MVC应用程序,我正在尝试使用用户
RSACryptServiceProvider
加密一些消息。但是,当我尝试使用
EncryptValue()
时,我得到一个NotSupportedException。这是我的代码:

     RSACryptoServiceProvider RSA = GetKeyFromContainer(_CONTAINER_NAME);

     byte[] byteEncrypted = RSA.EncryptValue(byteData); //Here is where I get the exception
以及GetKeyFromContainer方法(复制自):

//
///从容器中获取私钥并生成新的公钥
/// 
/// 
/// 
私有RSACryptServiceProvider GetKeyFromContainer(字符串ContainerName)
{
//创建CspParameters对象并设置密钥容器
//用于存储RSA密钥对的名称。
CspParameters cp=新的CspParameters();
cp.KeyContainerName=容器名称;
//创建可访问的RSACryptServiceProvider的新实例
//密钥容器
RSACryptServiceProvider rsa=新的RSACryptServiceProvider(cp);
返回rsa;
}

.NET 4.5不支持此方法吗?有其他方法吗?

有什么理由不直接使用
加密方法吗?实际上。。。不,没有。我现在觉得自己像个傻瓜。谢谢。我遇到了同样的问题,我需要使用
.EncryptValue
,因为我依赖于
RSA
的包装器,我将
rsacryptserviceprovider
传递给它。文档表明该方法不受支持。您知道在某些上下文中,这是不受支持的,还是根本不受支持的?
    /// <summary>
    /// Gets the private from the container and generates a new public key
    /// </summary>
    /// <param name="ContainerName"></param>
    /// <returns></returns>
    private RSACryptoServiceProvider GetKeyFromContainer(string ContainerName)
    {
        // Create the CspParameters object and set the key container 
        // name used to store the RSA key pair.
        CspParameters cp = new CspParameters();
        cp.KeyContainerName = ContainerName;

        // Create a new instance of RSACryptoServiceProvider that accesses
        // the key container
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);

        return rsa;
    }