Answered by:
ASP.NET C#: Encrypt a single integer value for storing in a hidden field

Question
-
User-596225964 posted
Hello
I have a single integer value (intOrderID) that I would like to easily encrypt so I can store it in a hidden field, and decrypt when needed by my methods.
Originally I stored this integer value in a session variable, but this causes problems when the session expires.
Can someone please point me in the right direction as I know next to nothing about encrypting data in C#? I've been Googling this for hours, without success.
Thanks
Jubbs
Friday, May 15, 2020 7:44 AM
Answers
-
User-596225964 posted
Thanks Patrice
That AES method looks promising. I've found another AES one here that works really well and seems a bit simpler to implement/use:
https://www.c-sharpcorner.com/article/encryption-and-decryption-using-a-symmetric-key-in-c-sharp/
mgebhard, ordinarily I would post my code, but posting code would have been redundant in the circumstances as I had explained my simple requirements for a stand-alone class for encrypting/decrypting data.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 16, 2020 2:25 AM
All replies
-
User753101303 posted
Hi,
See for example https://www.codeproject.com/Articles/1967/Encryption-Decryption-with-NET and the doc at https://docs.microsoft.com/en-us/dotnet/standard/security/cryptographic-services#secret-key-encryption
If you need to encrypt this value it likely means also that on the server side you are not checking if the current user is allowed to access this particular order id. It could be best to start with that before adding encryption.
Friday, May 15, 2020 10:38 AM -
User-596225964 posted
Thanks for the response.
The user only has access to their own orders, but I don't want them to see the OrderID number, that is why I want the encryption. I read both of those articles before, I couldn't get Fang's method to work at all.
All I want is methods that accept an integer argument and return an encrypted string, and conversely accepts the encrypted string and returns the original integer value.
Friday, May 15, 2020 12:33 PM -
User475983607 posted
It is easier to provide assistance if you share your code rather than the community guessing how your code works.
Friday, May 15, 2020 1:35 PM -
User753101303 posted
Great to see it is done already. I tried https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=netcore-3.1#examples which works.
Friday, May 15, 2020 3:23 PM -
User-596225964 posted
Thanks Patrice
That AES method looks promising. I've found another AES one here that works really well and seems a bit simpler to implement/use:
https://www.c-sharpcorner.com/article/encryption-and-decryption-using-a-symmetric-key-in-c-sharp/
mgebhard, ordinarily I would post my code, but posting code would have been redundant in the circumstances as I had explained my simple requirements for a stand-alone class for encrypting/decrypting data.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 16, 2020 2:25 AM