using System.Security.Cryptography; namespace SPMS.Infrastructure.Security; public static class RsaEncryption { public static byte[] Decrypt(byte[] encryptedData, string privateKeyPem) { using var rsa = RSA.Create(); rsa.ImportFromPem(privateKeyPem); return rsa.Decrypt(encryptedData, RSAEncryptionPadding.OaepSHA256); } public static byte[] Encrypt(byte[] data, string publicKeyPem) { using var rsa = RSA.Create(); rsa.ImportFromPem(publicKeyPem); return rsa.Encrypt(data, RSAEncryptionPadding.OaepSHA256); } public static (string PublicKeyPem, string PrivateKeyPem) GenerateKeyPair() { using var rsa = RSA.Create(2048); var privateKey = rsa.ExportRSAPrivateKey(); var publicKey = rsa.ExportRSAPublicKey(); var privatePem = new string(PemEncoding.Write("RSA PRIVATE KEY", privateKey)); var publicPem = new string(PemEncoding.Write("RSA PUBLIC KEY", publicKey)); return (publicPem, privatePem); } }