RSA加密和解密在MVC3

时间:2021-01-14 18:34:10

Please help me in the following issue.

请帮助我解决以下问题。

 RSAEncryptionAndDecryption rsaEncDec = new       RSAEncryptionAndDecryption(document.PublicKey, document.PublicAndPrivateKey);
 string reportContent = rsaEncDec.Decrypt(document.Report).OriginalData;

When i use the above code in MVC1, the decryption is working properly, but the same code is not able to decrypt the original string in MVC3.. Encryption code is:

当我在MVC1中使用上述代码时,解密工作正常,但是相同的代码不能解密MVC3中的原始字符串。加密代码是:

 RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(bitStrength);

        rsaCryptoServiceProvider.FromXmlString(publicKey);

        int keySize = bitStrength / 8;
        byte[] dataToEnc = Encoding.UTF32.GetBytes(inputData);
        int maxLength = keySize - 42;
        int dataLength = dataToEnc.Length;
        int iterations = dataLength / maxLength;
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i <= iterations; i++)
        {
            byte[] tempBytes = new byte[(dataLength - maxLength * i > maxLength) ? maxLength : dataLength - maxLength * i];
            Buffer.BlockCopy(dataToEnc, maxLength * i, tempBytes, 0, tempBytes.Length);
            byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt(tempBytes, true);

            Array.Reverse(encryptedBytes);

            stringBuilder.Append(Convert.ToBase64String(encryptedBytes));
        }

While storing the encoded string in the db., the encrypted string half part is getting stored, due to that it is getting the error.

在db中存储编码字符串时。,加密字符串的一半正在被存储,因为它正在获取错误。

Encoded string is getting trimmed while storing in DB, this is happening only when i use MVC3, and not MVC1.

编码字符串在DB中存储时被修剪,这只发生在使用MVC3时,而不是MVC1时。

I m encrypting the html contents., the encrypted contents are stored with the public and private keys, the same key is getting from the DB, when it is called, but the decryption is not working properly-- only half of the original string is getting generated and stopped with some special characters

我正在加密html内容。,加密的内容存储在公共和私有密匙中,相同的密匙在被调用时从DB中获取,但是解密不能正常工作——只有一半的原始字符串是用一些特殊的字符生成和停止的

Thanks in Advance!!!!

提前谢谢! ! ! !

1 个解决方案

#1


1  

I got to know that while passing the data from the MVC3 to the SQLServer, the string we are passing are not sent properly when we use the SaveOrUpdate() in Nhibernate.

我知道,当从MVC3传递数据到SQLServer时,当我们在Nhibernate中使用SaveOrUpdate()时,我们传递的字符串没有被正确地发送。

So what i have done is, I have retrieved the ID of the current inserted record and Updated the string using the Update Query..

因此,我所做的就是检索当前插入记录的ID并使用Update查询更新字符串。

:) :) Happy Coding.. !!!!!

:):)编码快乐. .! ! ! ! !

#1


1  

I got to know that while passing the data from the MVC3 to the SQLServer, the string we are passing are not sent properly when we use the SaveOrUpdate() in Nhibernate.

我知道,当从MVC3传递数据到SQLServer时,当我们在Nhibernate中使用SaveOrUpdate()时,我们传递的字符串没有被正确地发送。

So what i have done is, I have retrieved the ID of the current inserted record and Updated the string using the Update Query..

因此,我所做的就是检索当前插入记录的ID并使用Update查询更新字符串。

:) :) Happy Coding.. !!!!!

:):)编码快乐. .! ! ! ! !