TCP-based clients are designed to help understand the basics of network communication. These clients do not encrypt the data they send. I recommend not using them in commercial applications.
$ dotnet add package Mtf.CryptographyImplements the IAsymmetricCipher interface using RSA encryption with optional OAEP padding.
Provides both string and byte-level encryption/decryption, with support for importing keys from XML files.
Namespace: Mtf.Cryptography.AsymmetricCiphers
Implements: IAsymmetricCipher, IDisposable
RSAParameters containing the public keyRsaCipher(string keyFilePath, bool includePrivateParameters = false, bool useOaepPadding = true)Loads RSA parameters from an XML-formatted key file.
keyFilePath: Path to the XML fileincludePrivateParameters: true to include private key parametersuseOaepPadding: Use OAEP with SHA-256 padding (true) or PKCS#1 v1.5 (false)RsaCipher(RSAParameters parameters, bool useOaepPadding = true)Initializes from an RSAParameters struct.
parameters: Must include at least Modulus and ExponentuseOaepPadding: OAEP (SHA-256) or PKCS#1 paddingRSAParameters PublicKeyParameters { get; }Contains the public portion of the RSA key (Modulus, Exponent).
byte[] Encrypt(byte[] plainBytes)Encrypts a byte array using the configured RSA key and padding.
string Encrypt(string plainText)Encrypts a string using UTF-8 and returns a Base64 encoded result.
byte[] Decrypt(byte[] cipherBytes)Decrypts an RSA-encrypted byte array.
Requires that the private key is present, otherwise throws InvalidOperationException.
string Decrypt(string cipherText)Decrypts a Base64-encoded RSA ciphertext string.
Requires private key.
Dispose()Releases underlying RSA resources. Safe to call multiple times.
ArgumentNullException — Input is nullInvalidOperationException — Private key is missing for decryptionCryptographicException — Key import or crypto failureFormatException — Invalid Base64 input during decryptionvar rsa = new RsaCipher("key.xml", includePrivateParameters: true);
var encrypted = rsa.Encrypt("secret message");
var decrypted = rsa.Decrypt(encrypted);