解密查询字符串时出现错误的错误数据

时间:2014-05-08 20:09:10

标签: asp.net

请帮助这对我来说非常重要。 我想用数字签名加密和解密查询字符串,但是我收到了这个错误:

错误数据错误

请帮助这是我的加密代码:

public partial class HashQueryString : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        RSACryptoServiceProvider senderCipher = new RSACryptoServiceProvider();
        senderCipher.FromXmlString(SENDER_KEYS);


        RSACryptoServiceProvider receiverCipher = new RSACryptoServiceProvider();
        senderCipher.FromXmlString(RECEIVER_PUBLIC_KEY);


        string plainText = "milad";
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

        byte[] cipherTextBytes = receiverCipher.Encrypt(plainTextBytes, false);

        SHA1Managed hashalg = new SHA1Managed();
        byte[] hash = hashalg.ComputeHash(cipherTextBytes);
        RSAPKCS1SignatureFormatter sigFormatter = new RSAPKCS1SignatureFormatter(senderCipher);
        sigFormatter.SetHashAlgorithm("SHA1");
        byte[] signature = sigFormatter.CreateSignature(hash);

        string ciphertext = Convert.ToBase64String(cipherTextBytes);
        string signatureText = Convert.ToBase64String(signature);
        hlDecrypt.NavigateUrl = "/DeHashQueryString.aspx?secret=" + Server.UrlEncode(ciphertext) + "&signature=" + Server.UrlEncode(signatureText) + "";
    }
    catch (CryptographicException x)
    {
        MessageBox.Show(x.StackTrace+"");
    }
}
}

这是我的解密代码

public partial class DeHashQueryString : System.Web.UI.Page
{



protected void Page_Load(object sender, EventArgs e)
{
    string secret = Request.QueryString["secret"];
    if (secret == null)
    {
        throw new ApplicationException("Secret QueryString was not provided...");
    }
    string signature = Request.QueryString["signature"];
    if (signature == null)
    {
        throw new ApplicationException("Signature was not found...");
    }
    byte[] cipherText = Convert.FromBase64String(secret);
    byte[] signatureBytes = Convert.FromBase64String(signature);

    SHA1Managed hashalg = new SHA1Managed();
    byte[] computedhash = hashalg.ComputeHash(cipherText);

    RSACryptoServiceProvider senderCipher = new RSACryptoServiceProvider();
    senderCipher.FromXmlString(SENDER_PUBLIC_KEYS);
    RSAPKCS1SignatureDeformatter sigDeFormatter = new RSAPKCS1SignatureDeformatter(senderCipher);
    sigDeFormatter.SetHashAlgorithm("SHA1");

    if (!sigDeFormatter.VerifySignature(computedhash, signatureBytes))
    {
        throw new ApplicationException("Signature did not match from sender...");
    }
    decrypt(cipherText, RECEIVER_KEY);

}
private void decrypt(byte[] cipherText,string key)
{

    RSACryptoServiceProvider cipher =new RSACryptoServiceProvider();
    cipher.FromXmlString(key);
    string str = Convert.ToBase64String(cipherText);
    byte[]plainTextBytes=cipher.Decrypt(Convert.FromBase64String(str), false);
    Response.Write("My querystring was :" + Encoding.UTF8.GetString(plainTextBytes));
}
}

我完全写了公钥/私钥值

我删除公钥/私钥值

这是receiver_public_key: 私人常量字符串RECEIVER_PUBLIC_KEY =" zUjNd9zRuTluiiwiLu47SiIgvGl8 + YQPKuyklmwXzdH8WYP2VAWIR2rpRUIsNIv / 2LMLlKM69KXCIkn9SQZ0XsetAQqUK0VKO9DLUtgkJ4loSq + V6BGhpdNzjKYObdXz00g12HukV / Wq4kslB9 / ghgkCmQmC1mkxaCOPtQVcBfk = AQAB

6Q + Ro8TpTeT0eid9KImBsesRHGejTPxb6BCOhxMXd6RlD0BMrNk + wU62f09sVy3O55AML5fKX79d6kWDFIRDTQ ==

4X1Z64BdfMFI7ffnneCIquObQ9A / mf40omjsnKuk + SX2 + VGQFc7VNaA9 / hpeJ + cjAnUCM + LNDoNb8jAntq5fXQ == bcooyKZlZY164eSiYCtOzpq259OmqQik3xCVo + oJfxIRjXqz5CU / G99Rt22Z0wPAWXORWgY + jpZezpLKairYbQ == Wl15478VuCFdsMBgugx5uMJuPsb + NiCBx3R1HnRLMPB43YGqVJMHLu6A6Yx3n1Yp2Zo6v8BsnRxXhL93quZGoQ == kRyYgXh / aWJdk6w2AeootIwSuok6K2yfVR0kBXsEGMJzUafpZ9M6JFL + QpoeEbrxmkh3H4IhUcJiFF59Izxo7w == F + P0RdFnQwrZp / E41p4Kh / PdCyR4 / IXeYTKOkzwCVHKW0TnaM5xCu6OKcmB4Y8A4g0uAUnii2RmPaYdI6b7ArdHwtBFfJA6Xrn6NZkLa / SP1DGCLxZI + B4 + wY0oY1f6ZGwol4kPrvXysnclG9kUgb0YjVMZ9aqcwHZkkSuzTLhE =&#34 ;;

这是sender_key:

" zUjNd9zRuTluiiwiLu47SiIgvGl8 + YQPKuyklmwXzdH8WYP2VAWIR2rpRUIsNIv / 2LMLlKM69KXCIkn9SQZ0XsetAQqUK0VKO9DLUtgkJ4loSq + V6BGhpdNzjKYObdXz00g12HukV / Wq4kslB9 / ghgkCmQmC1mkxaCOPtQVcBfk = AQAB&#34 ;;

0 个答案:

没有答案