4.1 KiB
4.1 KiB
source_files, generated_at, model, schema_version, sha256
| source_files | generated_at | model | schema_version | sha256 | |
|---|---|---|---|---|---|
|
2026-04-16T14:14:38.025149+00:00 | zai-org/GLM-5-FP8 | 1 | 2d41661347ec3dbc |
Documentation: DTS.Common.Security.Encryption
1. Purpose
This module provides symmetric encryption and decryption utilities for string data within the DTS.Common.Security namespace. It exists to securely transform plaintext strings into byte arrays and vice versa using the Advanced Encryption Standard (AES) algorithm. It serves as a stateless wrapper around AesCryptoServiceProvider to facilitate data confidentiality.
2. Public Interface
Class: Encryption
Type: static
Method: EncryptStringToBytes
Signature: public static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv)
Encrypts a given string into an array of bytes using AES.
- Parameters:
plainText(string): The data to encrypt.key(byte[]): The secret key for symmetric encryption.iv(byte[]): The initialization vector.
- Returns:
byte[]containing the encrypted data. - Behavior: Validates inputs, initializes an
AesCryptoServiceProvider, and writes the encrypted data to aMemoryStreamusing aCryptoStream.
Method: DecryptStringFromBytes
Signature: public static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv)
Decrypts a byte array back into a string using AES.
- Parameters:
cipherText(byte[]): The encrypted data.key(byte[]): The secret key used for encryption.iv(byte[]): The initialization vector used for encryption.
- Returns:
stringcontaining the decrypted plaintext. - Behavior: Validates inputs, initializes an
AesCryptoServiceProvider, and reads the decrypted data from aMemoryStreamusing aCryptoStream.
3. Invariants
- Null/Empty Checks: All parameters (
plainText,key,iv,cipherText) must be non-null and have a length greater than 0. If these conditions are not met, anArgumentNullExceptionis thrown. - Symmetric Keys: The
keyandivprovided toDecryptStringFromBytesmust be identical to those used inEncryptStringToBytesto produce the original plaintext. - Encoding: Text encoding relies on the default behavior of
StreamWriterandStreamReader(typically UTF-8) wrapping the crypto streams. - Algorithm: The encryption algorithm is implicitly set to the default mode and padding of
AesCryptoServiceProvider(typically CBC mode with PKCS7 padding), as these properties are not explicitly set in the source.
4. Dependencies
- Internal Dependencies: None.
- External Dependencies:
SystemSystem.IO(forMemoryStream,StreamReader,StreamWriter)System.Security.Cryptography(forAesCryptoServiceProvider,CryptoStream)System.Text(implicitly used by Stream readers/writers)
5. Gotchas
- Incorrect Exception Parameter Name: In both
EncryptStringToBytesandDecryptStringFromBytes, the validation logic for theivparameter throws an exception namingkeyinstead:This will mislead developers debugging null IV issues.if (iv == null || iv.Length <= 0) throw new ArgumentNullException(nameof(key)); - Comment/Code Mismatch: The XML comments and inline comments refer to
RijndaelManaged, but the actual code instantiatesAesCryptoServiceProvider. While AES is a subset of Rijndael, the types are different. The variable namerijAlgis also misleading given the type. - Implicit Crypto Settings: The code does not explicitly set the
Mode(e.g., CBC, ECB) orPaddingproperties of theAesCryptoServiceProvider. It relies entirely on the framework's default values. This can be fragile if code is ported to environments with different defaults, though .NET defaults are consistent (CBC/PKCS7). - Unused Imports: The file imports
System.Collections.Generic,System.Linq, andSystem.Threading.Tasks, none of which are used in the provided implementation.