Skip to content

Instantly share code, notes, and snippets.

@mkropat
Last active October 17, 2017 18:02
Show Gist options
  • Save mkropat/49d8be90eb2eec96975b6201038e7b9a to your computer and use it in GitHub Desktop.
Save mkropat/49d8be90eb2eec96975b6201038e7b9a to your computer and use it in GitHub Desktop.
<Query Kind="Statements">
<Reference>&lt;RuntimeDirectory&gt;\System.Security.dll</Reference>
<Namespace>System.Security.Cryptography</Namespace>
</Query>
var scope = DataProtectionScope.CurrentUser;
var data = Encoding.UTF8.GetBytes("hello, world (privately)");
var encrypted = ProtectedData.Protect(data, null, scope);
BitConverter.ToString(encrypted).Dump("encrypted");
var decrypted = ProtectedData.Unprotect(encrypted, null, scope);
Encoding.UTF8.GetString(decrypted).Dump("decrypted");
var tampered = encrypted.ToArray();
tampered[encrypted.Length - 1] = 0xff;
ProtectedData.Unprotect(tampered, null, scope); // throws CryptographicException
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment