开发者

RSA encryption and decryption in MVC3

开发者 https://www.devze.com 2023-04-13 03:02 出处:网络
Please help me in the following issue. RSAEncryptionAndDecryption rsaEncDec = newRSAEncryptionAndDecryption(document.PublicKey, document.PublicAndPrivateKey);

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:

 RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(bitStrength);

        rsaCryptoServiceProvider.FromXmlString(publicKey);

        int keySize = bitStrength / 8;
        byte[] dataToEnc = Encoding.UTF32.GetBytes(inputData);
        int maxLength = keySize - 42;
        int dataLengt开发者_如何学编程h = 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.

Encoded string is getting trimmed while storing in DB, this is happening only when i use MVC3, and not 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

Thanks in Advance!!!!


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.

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消