Skip to content

Asymmetric & Signing ​

RSA helpers live under Asymmetric. Prefer AES-GCM for bulk data (Symmetric); use RSA to wrap small secrets or to sign/verify.

Encrypt / decrypt (XML keys) ​

csharp
using JASE.Crypto;
using System.Text;

if (!Asymmetric.GenerateRsaKeyPair(out string publicXml, out string privateXml, keySize: 2048))
{
    throw new InvalidOperationException("RSA key generation failed.");
}

byte[] plaintext = Encoding.UTF8.GetBytes("wrap-me");
byte[] cipher = Asymmetric.Encrypt(plaintext, publicXml, RsaPadding.OaepSHA256);
byte[] opened = Asymmetric.Decrypt(cipher, privateXml, RsaPadding.OaepSHA256);

Default padding is OaepSHA256. Keep privateXml secret. Do not RSA-encrypt large blobs — wrap a content key with RSA and encrypt the payload with AesGcm.

Sign / verify (raw RSA keys) ​

Sign / Verify byte-key overloads expect PKCS#1 key material (from GenerateRsaKeyPair(out byte[] …) or RSA.ExportRSAPrivateKey()), not UTF-8 XML:

csharp
if (!Asymmetric.GenerateRsaKeyPair(out byte[] publicKey, out byte[] privateKey, keySize: 2048))
{
    throw new InvalidOperationException("RSA key generation failed.");
}

byte[] message = Encoding.UTF8.GetBytes("audit-event");
byte[] signature = Asymmetric.Sign(message, privateKey, HashProvider.SHA384);
bool ok = Asymmetric.Verify(message, signature, publicKey, HashProvider.SHA384);

Sign / verify (certificate) ​

When keys live in an X509Certificate2 (must include the private key for signing):

csharp
byte[] signature = Asymmetric.Sign(message, signingCert, HashProvider.SHA256);
bool ok = Asymmetric.Verify(message, signature, signingCert, HashProvider.SHA256);

Guidance ​

  • Prefer hybrid designs (RSA for key wrap / signing, AES-GCM for bulk) over stuffing large payloads through RSA
  • Treat private key / PEM / XML material as secrets; never log them
  • Fail closed on verify/decrypt failures — do not “retry with weaker padding” in production
  • Pick one key representation (XML, PKCS#1 bytes, or cert) and stay consistent — do not UTF-8-encode XML into the byte-key overloads