发布网站后的RSA加密

时间:2018-04-13 10:00:48

标签: asp.net web cryptography rsa publishing

我有一个网站使用RSA加密一些数据。当我在本地主机上运行它时,RSA功能正常工作。但在发布我的网站后,该功能给了我一个错误。 函数调用是

Cryptography encryptor = new Cryptography();
string pkpath = Server.MapPath("publickey.xml");
byte[] RSAcipher = encryptor.RSAEncryptData(RSAplain, pkpath);

和RSAEncryptData()函数代码是

 public  void AssignParameter()
    {
        const int PROVIDER_RSA_FULL = 1;
        const string CONTAINER_NAME = "SpiderContainer";
        CspParameters cspParams;
        cspParams = new CspParameters(PROVIDER_RSA_FULL);
        cspParams.KeyContainerName = CONTAINER_NAME;
        cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
        cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
        rsa = new RSACryptoServiceProvider(cspParams);
    }

    public  byte[] RSAEncryptData(byte[] data2Encrypt,string pkpath)
    {
        AssignParameter();
        StreamReader reader = new StreamReader(@pkpath);
        string publicOnlyKeyXML = reader.ReadToEnd();
        rsa.FromXmlString(publicOnlyKeyXML);
        reader.Close();

        //read plaintext, encrypt it to ciphertext

        //byte[] plainbytes = System.Text.Encoding.UTF8.GetBytes(data2Encrypt);
        byte[] cipherbytes = rsa.Encrypt(data2Encrypt, false);

        //return Convert.ToBase64String(cipherbytes);
        return cipherbytes;
    }

  public  byte[] RSADecryptData(byte[] data2Decrypt,string prpath)
    {
        AssignParameter();

        //byte[] getpassword = Convert.FromBase64String(data2Decrypt);

        StreamReader reader = new StreamReader(@prpath);
        string publicPrivateKeyXML = reader.ReadToEnd();
        rsa.FromXmlString(publicPrivateKeyXML);
        reader.Close();

        //read ciphertext, decrypt it to plaintext
        byte[] plain = rsa.Decrypt(data2Decrypt, false);
        //return System.Text.Encoding.UTF8.GetString(plain);
        return plain;

    }

错误的堆栈跟踪是 Error stack trace

0 个答案:

没有答案