DataHandler.DataEncryptions
Provides methods to serialize, encrypt, decrypt, and deserialize objects using JSON + AES.
Shared Settings
- _jsonOpts (JsonSerializerOptions):
- IncludeFields = true
Enables JSON serialization of public fields as well as properties.
- IncludeFields = true
Methods
PackData(object data, SecureData Key)
Serializes an object of type T to JSON, wraps it with its assembly-qualified type name, Base64-encodes, then encrypts via AES-GCM.
-
Parameters:
- data: The object to serialize (must be castable to T).
- Key: SecureData used to derive the AES encryption key.
-
Process:
- Convert data to a byte array via BinaryConverter.NCObjectToByteArrayAsync.
- Create a tuple (assemblyQualifiedTypeName, payloadBytes).
- Serialize the tuple to UTF-8 JSON bytes (JsonSerializer.SerializeToUtf8Bytes).
- Base64-encode the JSON bytes.
- Encrypt the Base64 string using SimpleAESEncryption.Encrypt.
- Return the encrypted payload as AESEncryptedText.ToString().
-
Returns: Task — The encrypted Base64 JSON wrapper as a single string.
UnpackData(string data, SecureData Key)
Decrypts and deserializes a string previously produced by PackData back into the original object.
-
Parameters:
- data: The encrypted payload string (IV|EncryptedText format).
- Key: SecureData used to derive the AES decryption key.
-
Process:
- Parse data into SimpleAESEncryption.AESEncryptedText.
- Decrypt to recover the Base64-encoded JSON wrapper.
- Base64-decode to JSON bytes.
- Deserialize JSON into (string assemblyQualifiedTypeName, byte[] payloadBytes).
- Resolve the Type from assemblyQualifiedTypeName.
- Use reflection to call BinaryConverter.NCByteArrayToObjectAsync on payloadBytes.
- Await and return the .Result of that task (the original object).
-
Returns: Task — The deserialized object instance.
-
Exceptions:
- Throws if decryption fails, JSON is invalid, the type cannot be resolved, or deserialization errors occur.