public static string Encrypt(this string clearText, ITraceLogger TL) { try { byte[] clearBytes = Encoding.UTF8.GetBytes(clearText); byte[] entropyBytes = Encoding.UTF8.GetBytes(GenerateEntropy()); byte[] encryptedBytes = ProtectedData.Protect(clearBytes, entropyBytes, dataProtectionScope); string encryptedText = Convert.ToBase64String(encryptedBytes); TL.LogMessage("Encrypt", encryptedText, false); return(encryptedText); } catch (Exception ex) { TL.LogMessageCrLf("Encrypt", ex.ToString()); return("Unable to encrypt this value"); } }
public static string Unencrypt(this string encryptedText, ITraceLogger TL) { try { if (encryptedText == null) { return(""); // Deal with initial case where text has not yet been encrypted } byte[] encryptedBytes = Convert.FromBase64String(encryptedText); byte[] entropyBytes = Encoding.UTF8.GetBytes(GenerateEntropy()); byte[] clearBytes = ProtectedData.Unprotect(encryptedBytes, entropyBytes, dataProtectionScope); string clearText = Encoding.UTF8.GetString(clearBytes); TL.LogMessage("Unencrypt", encryptedText, false); return(clearText); } catch (Exception ex) { TL.LogMessageCrLf("Unencrypt", ex.ToString()); return("Unable to decrypt this value"); } }