<PackageReference Include="SSH.NET" Version="2024.0.0" />

RsaKey

public class RsaKey : Key, IDisposable
Contains the RSA private and public key.
using Renci.SshNet.Common; using Renci.SshNet.Security.Cryptography; using System; namespace Renci.SshNet.Security { public class RsaKey : Key, IDisposable { private bool _isDisposed; private RsaDigitalSignature _digitalSignature; public BigInteger Modulus { get; } public BigInteger Exponent { get; } public BigInteger D { get; } public BigInteger P { get; } public BigInteger Q { get; } public BigInteger DP { get; } public BigInteger DQ { get; } public BigInteger InverseQ { get; } public override int KeyLength => Modulus.BitLength; protected internal override DigitalSignature DigitalSignature { get { if (_digitalSignature == null) _digitalSignature = new RsaDigitalSignature(this); return _digitalSignature; } } public override BigInteger[] Public => new BigInteger[2] { Exponent, Modulus }; public override string ToString() { return "ssh-rsa"; } public RsaKey(SshKeyData publicKeyData) { if (publicKeyData == null) throw new ArgumentNullException("publicKeyData"); if (publicKeyData.Name != "ssh-rsa" || publicKeyData.Keys.Length != 2) throw new ArgumentException($"""{publicKeyData.Name}""{publicKeyData.Keys.Length}""", "publicKeyData"); Exponent = publicKeyData.Keys[0]; Modulus = publicKeyData.Keys[1]; } public RsaKey(byte[] privateKeyData) { if (privateKeyData == null) throw new ArgumentNullException("privateKeyData"); DerData derData = new DerData(privateKeyData, false); derData.ReadBigInteger(); Modulus = derData.ReadBigInteger(); Exponent = derData.ReadBigInteger(); D = derData.ReadBigInteger(); P = derData.ReadBigInteger(); Q = derData.ReadBigInteger(); DP = derData.ReadBigInteger(); DQ = derData.ReadBigInteger(); InverseQ = derData.ReadBigInteger(); if (!derData.IsEndOfData) throw new InvalidOperationException("Invalid private key (expected EOF)."); } public RsaKey(BigInteger modulus, BigInteger exponent, BigInteger d, BigInteger p, BigInteger q, BigInteger inverseQ) { Modulus = modulus; Exponent = exponent; D = d; P = p; Q = q; DP = PrimeExponent(d, p); DQ = PrimeExponent(d, q); InverseQ = inverseQ; } private static BigInteger PrimeExponent(BigInteger privateExponent, BigInteger prime) { BigInteger divisor = prime - BigInteger.One; return privateExponent % divisor; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!_isDisposed && disposing) { RsaDigitalSignature digitalSignature = _digitalSignature; if (digitalSignature != null) { digitalSignature.Dispose(); _digitalSignature = null; } _isDisposed = true; } } ~RsaKey() { Dispose(false); } } }