public void Dispose() { DhPrime.Dispose(); DhGenerator.Dispose(); DhPublic.Dispose(); DhPrivate.Dispose(); }
public HKeyExchange(int e, string n, string d, int bitSize = 16) { _bitSize = bitSize; IsInitiator = !string.IsNullOrWhiteSpace(d); Rsa = IsInitiator ? RsaKey.ParsePrivateKey(e, n, d) : RsaKey.ParsePublicKey(e, n); if (IsInitiator) { do { DhPrime = BigInteger.GenPseudoPrime(212, 6, _byteGen); }while (!DhPrime.IsProbablePrime()); do { DhGenerator = BigInteger.GenPseudoPrime(212, 6, _byteGen); }while (DhGenerator >= DhPrime && !DhPrime.IsProbablePrime()); if (DhGenerator > DhPrime) { BigInteger dhGenShell = DhGenerator; DhGenerator = DhPrime; DhPrime = dhGenShell; } DhPrivate = new BigInteger(RandomHex(30), bitSize); DhPublic = DhGenerator.ModPow(DhPrivate, DhPrime); } }
public string GetSignedPrime() { if (!IsInitiator || !string.IsNullOrEmpty(_signedPrime)) { return(_signedPrime); } byte[] primeAsBytes = Encoding.Default.GetBytes(DhPrime.ToString(10)); Rsa.Sign(ref primeAsBytes); return(_signedPrime = BytesToHex(primeAsBytes).ToLower()); }
protected virtual void Dispose(bool disposing) { if (!IsDisposed) { if (disposing) { Rsa?.Dispose(); DhPrime?.Dispose(); DhGenerator?.Dispose(); DhPublic?.Dispose(); DhPrivate?.Dispose(); } IsDisposed = true; } }