Simple insecure two-way data “obfuscation”?

后端 未结 17 1420
别跟我提以往
别跟我提以往 2020-11-22 01:10

I\'m looking for very simple obfuscation (like encrypt and decrypt but not necessarily secure) functionality for some data. It\'s not mission critical. I need something to k

17条回答
  •  旧时难觅i
    2020-11-22 01:39

    I wanted to post my solution since none of the above the solutions are as simple as mine. Let me know what you think:

     // This will return an encrypted string based on the unencrypted parameter
     public static string Encrypt(this string DecryptedValue)
     {
          HttpServerUtility.UrlTokenEncode(MachineKey.Protect(Encoding.UTF8.GetBytes(DecryptedValue.Trim())));
     }
    
     // This will return an unencrypted string based on the parameter
     public static string Decrypt(this string EncryptedValue)
     {
          Encoding.UTF8.GetString(MachineKey.Unprotect(HttpServerUtility.UrlTokenDecode(EncryptedValue)));
     }
    

    Optional

    This assumes that the MachineKey of the server used to encrypt the value is the same as the one used to decrypt the value. If desired, you can specify a static MachineKey in the Web.config so that your application can decrypt/encrypt data regardless of where it is run (e.g. development vs. production server). You can generate a static machine key following these instructions.

提交回复
热议问题