{"id":30987,"date":"2017-04-10T10:09:42","date_gmt":"2017-04-10T14:09:42","guid":{"rendered":"http:\/\/www.opensource.im\/?p=30987"},"modified":"2017-04-10T10:09:42","modified_gmt":"2017-04-10T14:09:42","slug":"c-encrypt-and-decrypt-a-string-stack-overflow","status":"publish","type":"post","link":"https:\/\/euvolution.com\/open-source-convergence\/encryption\/c-encrypt-and-decrypt-a-string-stack-overflow.php","title":{"rendered":"c# &#8211; Encrypt and decrypt a string &#8211; Stack Overflow"},"content":{"rendered":"<p><p>    Modern Examples of Symmetric Authenticated Encryption    of a string.  <\/p>\n<p>    The general best practice for symmetric encryption is to use    Authenticated Encryption with Associated Data (AEAD), however    this isn't a part of the standard .net crypto libraries. So the    first example uses AES256    and then HMAC256, a two step    Encrypt    then MAC, which requires more overhead and more keys.  <\/p>\n<p>    The second example uses the simpler practice of AES256-GCM    using the open source Bouncy Castle (via nuget).  <\/p>\n<p>    Both examples have a main function that takes secret message    string, key(s) and an optional non-secret payload and return    and authenticated encrypted string optionally prepended with    the non-secret data. Ideally you would use these with 256bit    key(s) randomly generated see NewKey().  <\/p>\n<p>    Both examples also have a helper methods that use a string    password to generate the keys. These helper methods are    provided as a convenience to match up with other examples,    however they are far less secure because the strength    of the password is going to be far weaker than a 256 bit    key.  <\/p>\n<p>    Update: Added byte[] overloads,    and only the Gist    has the full formatting with 4 spaces indent and api docs due    to StackOverflow answer limits.  <\/p>\n<p>    .NET Built-in Encrypt(AES)-Then-MAC(HMAC) [Gist]  <\/p>\n<p>    Bouncy Castle AES-GCM [Gist]  <\/p>\n<p><!-- Auto Generated --><\/p>\n<p>Read more:<br \/>\n<a target=\"_blank\" href=\"http:\/\/stackoverflow.com\/questions\/202011\/encrypt-and-decrypt-a-string\" title=\"c# - Encrypt and decrypt a string - Stack Overflow\">c# - Encrypt and decrypt a string - Stack Overflow<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p> Modern Examples of Symmetric Authenticated Encryption of a string. The general best practice for symmetric encryption is to use Authenticated Encryption with Associated Data (AEAD), however this isn't a part of the standard .net crypto libraries<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"class_list":["post-30987","post","type-post","status-publish","format-standard","hentry","category-encryption"],"_links":{"self":[{"href":"https:\/\/euvolution.com\/open-source-convergence\/wp-json\/wp\/v2\/posts\/30987"}],"collection":[{"href":"https:\/\/euvolution.com\/open-source-convergence\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/euvolution.com\/open-source-convergence\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/euvolution.com\/open-source-convergence\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/euvolution.com\/open-source-convergence\/wp-json\/wp\/v2\/comments?post=30987"}],"version-history":[{"count":0,"href":"https:\/\/euvolution.com\/open-source-convergence\/wp-json\/wp\/v2\/posts\/30987\/revisions"}],"wp:attachment":[{"href":"https:\/\/euvolution.com\/open-source-convergence\/wp-json\/wp\/v2\/media?parent=30987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/euvolution.com\/open-source-convergence\/wp-json\/wp\/v2\/categories?post=30987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/euvolution.com\/open-source-convergence\/wp-json\/wp\/v2\/tags?post=30987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}