<PackageReference Include="BouncyCastle.Cryptography" Version="2.7.0-beta.98" />

RsaBlindingFactorGenerator

using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; using System; namespace Org.BouncyCastle.Crypto.Generators { public class RsaBlindingFactorGenerator { private RsaKeyParameters key; private SecureRandom random; public void Init(ICipherParameters param) { key = (RsaKeyParameters)ParameterUtilities.GetRandom(param, out SecureRandom secureRandom); random = CryptoServicesRegistrar.GetSecureRandom(secureRandom); if (key.IsPrivate) throw new ArgumentException("generator requires RSA public key"); } public BigInteger GenerateBlindingFactor() { if (key == null) throw new InvalidOperationException("generator not initialised"); BigInteger modulus = key.Modulus; int bitLength = modulus.BitLength - 1; BigInteger bigInteger; do { bigInteger = BigIntegers.CreateRandomBigInteger(bitLength, random); } while (bigInteger.CompareTo(BigInteger.Two) < 0 || !BigIntegers.ModOddIsCoprime(modulus, bigInteger)); return bigInteger; } } }