Obaid Ul Haque
Obaid Ul Haque

Reputation: 29

Not enough storage is available to process this command In RSA Encryption

In RSA Encryption With RSACryptoServiceProvider USING ASP.NET C# , I Got This Error Why? What is Wrong in My Code?

 [WebMethod]
    public string newDcrypt(string text)
    {
        UnicodeEncoding encoder = new UnicodeEncoding();
        string publickey = HttpContext.Current.Server.MapPath("Keys/publickey.pem");
        string privatekey = HttpContext.Current.Server.MapPath("Keys/privatekey.pem");
        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        privatekey = RSA.ToXmlString(true);
        byte[] datatodecrypt = Convert.FromBase64String(text);
        RSA.FromXmlString(privatekey);
        byte[] decryptdata = RSA.Decrypt(datatodecrypt, false);
        return Encoding.UTF8.GetString(decryptdata); 
    }

Error On This Line Of Code....

var decryptdata = RSA.Decrypt(datatodecrypt, false)

Encryption Is Fine Here Is Encryption Code....

  [WebMethod]
    public string NewEncrypt(string text)
    {
        UnicodeEncoding encoder = new UnicodeEncoding();
        string publickey = HttpContext.Current.Server.MapPath("Keys/publickey.pem");
        string privatekey = HttpContext.Current.Server.MapPath("Keys/privatekey.pem");

        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        publickey = RSA.ToXmlString(true);
        var datatoencrypt = encoder.GetBytes(text);
        var encryptedbytearray = RSA.Encrypt(datatoencrypt, false);
        RSA.FromXmlString(publickey);
        return Convert.ToBase64String(encryptedbytearray);
    }

Error:

System.Security.Cryptography.CryptographicException: Not enough storage is available to process this command. at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) at System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey) at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP) at DataEncryptDecrypt.Encryption.newDcrypt(String text) in e:\VS Projects\DataEncryptDecrypt\DataEncryptDecrypt\Encryption.asmx.cs:line 180

Upvotes: 0

Views: 1165

Answers (1)

freewill
freewill

Reputation: 1171

I had the same problem and resolved it as follows:

Either you are trying wrong key; or the key does not exist in the csp keystore.

Upvotes: 1

Related Questions