Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir

here i have mentioned my code please review it and below i have mentioned my text which i am passing..


C#
protected static string DecryptData(string data2Decrypt)
   {
       //AssignParameter();
       const int PROVIDER_RSA_FULL = 1;
       const string CONTAINER_NAME = "MyContainer";
       CspParameters cspParams;
       cspParams = new CspParameters(PROVIDER_RSA_FULL);
       cspParams.KeyContainerName = CONTAINER_NAME;
       cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
       cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
       byte[] getpassword = Convert.FromBase64String(data2Decrypt);
       using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048, cspParams))
       {
           byte[] decryptedData = RSA.Decrypt(getpassword, false);
           return Encoding.Unicode.GetString(decryptedData);
       }


   }

i am passing this to data2Decrypt = dXyWe/9Shg3ANf5QSIvP+U/gPfzZyaZ6viMomn6+DFRXJ/Aty2FU30T0e0Tkys31CxaFfKcN6q5ZfpTFh2lRz8/Ff8LS7HfWXJcEg//B9BvigjH8UxxFDqDbcm7zcAGMLBy0vcJQEWGKf7wyQmNF1LEo/w275HNy8RNtab4hDWQ=

its working fine on local but when i am using this online it gets error
System.Security.Cryptography.CryptographicException: Bad Data.
Posted
Comments
Maarten Kools 27-Aug-13 6:20am    
Well, I'm getting the same error. So the string you've specified here is obviously not correct.

I assume you have an Encrypt method as well? Have you tried DecryptData(EncryptData("datatodecrypt"))? If that works, then your methods work correctly and something else must be wrong (like you're not passing the correct data). If not, then either your decrypt or encrypt is incorrect (and in such case, post both methods and we'll see from there).
vishvadeepak 27-Aug-13 6:30am    
first i encrypted a string "admin" then encrypted data i passed here in local its working fine but online its showing error thats why i am confused that what to do
Maarten Kools 27-Aug-13 6:32am    
Have you debugged your online version? Do you see any change in the string compared to the local version in the string to decrypt?

1 solution

cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
Well, you installed your keys on your local machine, hence it works locally. But you haven't yet installed your keys on the server.
Furthermore, passwords are normally not "encrypted" in a way that the clear text can be recovered, normally a hash value of the salted password is stored (and the salt also), and when the user logs in, the hash value is calculated the same way and compared to the stored hash value. Of course, when you need the password to log in to a different service, decryption of stored values may be necessary.
 
Share this answer
 
Comments
vishvadeepak 28-Aug-13 5:30am    
so what should i do i mean how can i run that code on server.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900