using System.Security.Cryptography;
using AngouriMath.Extensions;
using AngouriMath.Functions;
int primosEncontrados = 0;
BigInteger privateKey = RanPrivateKey();
bool esPrimo = IsProbablyPrime(privateKey, 20);
Console.WriteLine($"Clave privada generada: {privateKey} {esPrimo} {esPrimo} {esPrimo} {esPrimo}");
Console.WriteLine($"Clave privada generada: {privateKey} {esPrimo}");
if (esPrimo) primosEncontrados++;
string hexadecimalValue = privateKey.ToString("X");
hexadecimalValue = TrimStringTo64Characters(hexadecimalValue);
string hexadecimal = hexadecimalValue.PadLeft(64, '0').ToLower();
Console.WriteLine($"hexadecimal: {hexadecimal}");
var y = MathS.Log(2, privateKey);
Console.WriteLine($"Rango: {y.EvalNumerical()}");
Console.WriteLine($"PrivateKeyDecimal: {x.EvalNumerical()}");
Console.WriteLine($"--------------------------------------------------------");
Console.WriteLine($"Primos encontrados: {primosEncontrados}");
static BigInteger GeneratePrivateKey()
byte[] privateKeyBytes = new byte[32];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
rng.GetBytes(privateKeyBytes);
BigInteger privateKey = new BigInteger(privateKeyBytes.Concat(new byte[] { 0 }).ToArray());
static bool IsProbablyPrime(BigInteger n, int k)
if (n <= 1) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0) return false;
Random rng = new Random();
for (int i = 0; i < k; i++)
BigInteger a = RandomBigInteger(2, n - 2, rng);
BigInteger x = BigInteger.ModPow(a, d, n);
if (x == 1 || x == n - 1)
bool continueOuterLoop = false;
for (int r = 1; r < s; r++)
x = BigInteger.ModPow(x, 2, n);
continueOuterLoop = true;
static BigInteger RandomBigInteger(BigInteger min, BigInteger max, Random rng)
byte[] bytes = max.ToByteArray();
bytes[bytes.Length - 1] &= 0x7F;
result = new BigInteger(bytes);
} while (result < min || result > max);
static string TrimStringTo64Characters(string input)
return input.Substring(input.Length - 64);
static BigInteger RanPrivateKey()
byte[] randomNumber = new byte[32];
RandomNumberGenerator.Fill(randomNumber);
BigInteger privateKey = new BigInteger(randomNumber.Concat(new byte[] { 0 }).ToArray());