예제 #1
0
 public void Dispose()
 {
     DhPrime.Dispose();
     DhGenerator.Dispose();
     DhPublic.Dispose();
     DhPrivate.Dispose();
 }
예제 #2
0
        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);
            }
        }
예제 #3
0
        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());
        }
예제 #4
0
 protected virtual void Dispose(bool disposing)
 {
     if (!IsDisposed)
     {
         if (disposing)
         {
             Rsa?.Dispose();
             DhPrime?.Dispose();
             DhGenerator?.Dispose();
             DhPublic?.Dispose();
             DhPrivate?.Dispose();
         }
         IsDisposed = true;
     }
 }