Skip to content

Instantly share code, notes, and snippets.

@thoemmi
Last active June 12, 2023 07:10
Show Gist options
  • Select an option

  • Save thoemmi/e118c15e7588750b1cc18dab00be31fd to your computer and use it in GitHub Desktop.

Select an option

Save thoemmi/e118c15e7588750b1cc18dab00be31fd to your computer and use it in GitHub Desktop.

Revisions

  1. thoemmi revised this gist Feb 11, 2017. No changes.
  2. thoemmi revised this gist Feb 11, 2017. 1 changed file with 67 additions and 66 deletions.
    133 changes: 67 additions & 66 deletions EncryptingJsonConverter.cs
    Original file line number Diff line number Diff line change
    @@ -1,80 +1,81 @@
    public class EncryptingJsonConverter : JsonConverter {
    private readonly byte[] _encryptionKeyBytes;
    private readonly byte[] _encryptionKeyBytes;

    public EncryptingJsonConverter(string encryptionKey) {
    if (encryptionKey == null) {
    throw new ArgumentNullException(nameof(encryptionKey));
    }
    public EncryptingJsonConverter(string encryptionKey) {
    if (encryptionKey == null) {
    throw new ArgumentNullException(nameof(encryptionKey));
    }

    // Hash the key to ensure it is exactly 256 bits long, as required by AES-256
    using (var sha = new SHA256Managed()) {
    _encryptionKeyBytes =
    sha.ComputeHash(Encoding.UTF8.GetBytes(encryptionKey));
    }
    }
    // Hash the key to ensure it is exactly 256 bits long, as required by AES-256
    using (var sha = new SHA256Managed()) {
    _encryptionKeyBytes =
    sha.ComputeHash(Encoding.UTF8.GetBytes(encryptionKey));
    }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
    var stringValue = (string)value;
    if (string.IsNullOrEmpty(stringValue)) {
    writer.WriteNull();
    return;
    }
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
    var stringValue = (string)value;
    if (string.IsNullOrEmpty(stringValue)) {
    writer.WriteNull();
    return;
    }

    var buffer = Encoding.UTF8.GetBytes(stringValue);
    var buffer = Encoding.UTF8.GetBytes(stringValue);

    using (var inputStream = new MemoryStream(buffer, false))
    using (var outputStream = new MemoryStream())
    using (var aes = new AesManaged {
    Key = _encryptionKeyBytes
    }) {
    var iv = aes.IV; // first access generates a new IV
    outputStream.Write(iv, 0, iv.Length);
    outputStream.Flush();
    using (var inputStream = new MemoryStream(buffer, false))
    using (var outputStream = new MemoryStream())
    using (var aes = new AesManaged {
    Key = _encryptionKeyBytes
    }) {
    var iv = aes.IV; // first access generates a new IV
    outputStream.Write(iv, 0, iv.Length);
    outputStream.Flush();

    var encryptor = aes.CreateEncryptor(_encryptionKeyBytes, iv);
    using (var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write)) {
    inputStream.CopyTo(cryptoStream);
    }
    var encryptor = aes.CreateEncryptor(_encryptionKeyBytes, iv);
    using (var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write)) {
    inputStream.CopyTo(cryptoStream);
    }

    writer.WriteValue(Convert.ToBase64String(outputStream.ToArray()));
    }
    }
    writer.WriteValue(Convert.ToBase64String(outputStream.ToArray()));
    }
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
    var value = reader.Value as string;
    if (string.IsNullOrEmpty(value)) {
    return reader.Value;
    }
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
    var value = reader.Value as string;
    if (string.IsNullOrEmpty(value)) {
    return reader.Value;
    }

    try {
    var buffer = Convert.FromBase64String(value);
    try {
    var buffer = Convert.FromBase64String(value);

    using (var inputStream = new MemoryStream(buffer, false))
    using (var outputStream = new MemoryStream())
    using (var aes = new AesManaged {
    Key = _encryptionKeyBytes
    }) {
    var iv = new byte[16];
    var bytesRead = inputStream.Read(iv, 0, 16);
    if (bytesRead < 16) {
    throw new CryptographicException("IV is missing or invalid.");
    }
    using (var inputStream = new MemoryStream(buffer, false))
    using (var outputStream = new MemoryStream())
    using (var aes = new AesManaged {
    Key = _encryptionKeyBytes
    }) {
    var iv = new byte[16];
    var bytesRead = inputStream.Read(iv, 0, 16);
    if (bytesRead < 16) {
    throw new CryptographicException("IV is missing or invalid.");
    }

    var decryptor = aes.CreateDecryptor(_encryptionKeyBytes, iv);
    using (var cryptoStream = new CryptoStream(inputStream, decryptor, CryptoStreamMode.Read)) {
    cryptoStream.CopyTo(outputStream);
    }
    var decryptor = aes.CreateDecryptor(_encryptionKeyBytes, iv);
    using (var cryptoStream = new CryptoStream(inputStream, decryptor, CryptoStreamMode.Read)) {
    cryptoStream.CopyTo(outputStream);
    }

    var decryptedValue = Encoding.UTF8.GetString(outputStream.ToArray());
    return decryptedValue;
    }
    }
    catch {
    return string.Empty;
    }
    }
    var decryptedValue = Encoding.UTF8.GetString(outputStream.ToArray());
    return decryptedValue;
    }
    }
    catch {
    return string.Empty;
    }
    }

    public override bool CanConvert(Type objectType) {
    return objectType == typeof(string);
    }
    }
    /// <inheritdoc />
    public override bool CanConvert(Type objectType) {
    return objectType == typeof(string);
    }
    }
  3. thoemmi revised this gist Feb 11, 2017. No changes.
  4. thoemmi revised this gist Feb 11, 2017. No changes.
  5. thoemmi revised this gist Feb 11, 2017. No changes.
  6. thoemmi created this gist Feb 11, 2017.
    80 changes: 80 additions & 0 deletions EncryptingJsonConverter.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    public class EncryptingJsonConverter : JsonConverter {
    private readonly byte[] _encryptionKeyBytes;

    public EncryptingJsonConverter(string encryptionKey) {
    if (encryptionKey == null) {
    throw new ArgumentNullException(nameof(encryptionKey));
    }

    // Hash the key to ensure it is exactly 256 bits long, as required by AES-256
    using (var sha = new SHA256Managed()) {
    _encryptionKeyBytes =
    sha.ComputeHash(Encoding.UTF8.GetBytes(encryptionKey));
    }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
    var stringValue = (string)value;
    if (string.IsNullOrEmpty(stringValue)) {
    writer.WriteNull();
    return;
    }

    var buffer = Encoding.UTF8.GetBytes(stringValue);

    using (var inputStream = new MemoryStream(buffer, false))
    using (var outputStream = new MemoryStream())
    using (var aes = new AesManaged {
    Key = _encryptionKeyBytes
    }) {
    var iv = aes.IV; // first access generates a new IV
    outputStream.Write(iv, 0, iv.Length);
    outputStream.Flush();

    var encryptor = aes.CreateEncryptor(_encryptionKeyBytes, iv);
    using (var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write)) {
    inputStream.CopyTo(cryptoStream);
    }

    writer.WriteValue(Convert.ToBase64String(outputStream.ToArray()));
    }
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
    var value = reader.Value as string;
    if (string.IsNullOrEmpty(value)) {
    return reader.Value;
    }

    try {
    var buffer = Convert.FromBase64String(value);

    using (var inputStream = new MemoryStream(buffer, false))
    using (var outputStream = new MemoryStream())
    using (var aes = new AesManaged {
    Key = _encryptionKeyBytes
    }) {
    var iv = new byte[16];
    var bytesRead = inputStream.Read(iv, 0, 16);
    if (bytesRead < 16) {
    throw new CryptographicException("IV is missing or invalid.");
    }

    var decryptor = aes.CreateDecryptor(_encryptionKeyBytes, iv);
    using (var cryptoStream = new CryptoStream(inputStream, decryptor, CryptoStreamMode.Read)) {
    cryptoStream.CopyTo(outputStream);
    }

    var decryptedValue = Encoding.UTF8.GetString(outputStream.ToArray());
    return decryptedValue;
    }
    }
    catch {
    return string.Empty;
    }
    }

    public override bool CanConvert(Type objectType) {
    return objectType == typeof(string);
    }
    }
    4 changes: 4 additions & 0 deletions Usage.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    public class Settings {
    [JsonConverter(typeof(EncryptingJsonConverter), "#my*S3cr3t")]
    public string Password { get; set; }
    }