简体   繁体   中英

RSA Encryption causes errors

I'm hoping you can help me with a weird error I've been getting while trying to implement a QueryString encryption module using RSA for my encryption type. My question is 2 fold:

  1. Can you help me resolve the errors I'm getting?
  2. Do you recommend something other than RSA to encrypt a QueryString?

Background/Important info:
I created an object called QueryString, which i store in the session (and which uses the SessionID to generate keys/salt). I instantiate it on Session Start, which generates the keys, and it naturally dies on Session.Abandon... I retrieve it in my BasePage and use it in my pages afterword much like i would a normal querystring (QueryString[key] for gets and stuff)... I store my Public and Private keys in the object itself, as internal properties to the object.

Another important thing is that my web site has a lot of grids, whithin which a record rows that contain links, and so they all have to be encrypted before they are set (href=...)... so the QueryString object i created can get taxed quite a bit and quite quickly (while looping with OnRowCreated or something to encrypt the hrefs).

The error(s):
i am currently getting intermittent errors, that cannot be reproduced (they happen at random... trust me... very random), of the following types when i try to either Encrypt or Decrypt:
Error type 1: CreateProvHandle Error type 2: The specified file could not be found. Error type 3: Attempted to perform an unauthorized operation.

For errors 1 and 2, i managed to deal with it so far by simply recursively calling the method (encrypt or decrypt) that caused it and they usually only recurse once (max i've had is 3 using my metrics) and the error magically disappears... so i blamed it on too many calls too fast to the object itself or something... but if someone has any clue as to why this would happen or how to solve this, i would love to take the recursing out of my methods and truly throw when a major exception occurs instead. On top of that i told my RSA params not to persist anything in the CSP store and so i thought the file thing didn't matter but apparently not...

For error 3, i simply cannot get my head around it! My RSA parameters say not to persist anything in the CSP so i don't know how, when or why it would even try to access files (yes, i am repeating myself!), let alone files that are restricted or that the user wouldn't have access to? Please help me!!

Here's some code for my RSA params... maybe you'll find something there that doesn't jive with what i'm trying to do (generate the keys once on object instantiation, store the object in the session, and use that from that point on/disconnect from anything remote/calls to server processes that are not part of the site or .NET)?

public static void AssignParameter()
    {
        const int PROVIDER_RSA_FULL = 1;
        const string CONTAINER_NAME = "ICareContainer";
        CspParameters cspParams;
        cspParams = new CspParameters(PROVIDER_RSA_FULL);
        cspParams.KeyContainerName = CONTAINER_NAME;
        cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
        cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
        CryptoKeyAccessRule rule = new CryptoKeyAccessRule("everyone", CryptoKeyRights.FullControl, AccessControlType.Allow);
        cspParams.CryptoKeySecurity = new CryptoKeySecurity();
        cspParams.CryptoKeySecurity.SetAccessRule(rule);

        rsa = new RSACryptoServiceProvider(cspParams);
        rsa.PersistKeyInCsp = false;
        rsa.KeySize = 1024;
    }



public static string[] GetKeys()
    {
        AssignParameter();
        string[] keys =  new string[2];
        //privatekey
        keys[0] = rsa.ToXmlString(true);
        //publickey
        keys[1] = rsa.ToXmlString(false);

        return keys;
    }

public static string EncryptData(string data2Encrypt, string key)
    {
        AssignParameter();
        string publicOnlyKeyXML = key;
        rsa.FromXmlString(publicOnlyKeyXML);
        //read plaintext, encrypt it to ciphertext  
        byte[] plainbytes = System.Text.Encoding.Default.GetBytes(data2Encrypt);
        byte[] cipherbytes = rsa.Encrypt(plainbytes, false);
        return Convert.ToBase64String(cipherbytes);
    }


public static string DecryptData(string data2Decrypt, string key)
    {
        AssignParameter();
        byte[] getpassword = Convert.FromBase64String(data2Decrypt);
        string publicPrivateKeyXML = key;
        rsa.FromXmlString(publicPrivateKeyXML);
        //read ciphertext, decrypt it to plaintext  
        byte[] plain = rsa.Decrypt(getpassword, false);
        return System.Text.Encoding.Default.GetString(plain);
    }

Ah yes... stupid mistake (aren't they always?):

Normal Class
    Static Crypto Class
    End Static
End Normal

Can you find the problem and why i was getting collision errors? I've changed the Static to be Normal and all is well in my neck of the woods.

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM