Exemplo n.º 1
0
        public void RSAEncryption()
        {
            BigInteger p = view.PrimeP, q = view.PrimeQ, e = view.E, d = 0;

            view.WarningMessage = string.Empty;

            if (p.ToString().Length > 7 || q.ToString().Length > 7 || e.ToString().Length > 7)
            {
                view.WarningMessage += "primenumbers with more than 7 Digits wont be checked,\nso your Encryption might be wrong";
                try
                {
                    d = MyMath.ModInverse(e, MyMath.PhiFuncPrime(p, q));
                }
                catch (DivideByZeroException exception)
                {
                    view.WarningMessage += "\ntry different input values";
                    return;
                }
            }
            else
            {
                //Checking if p and q are prime numbers
                if (!MyMath.IsPrime(p))
                {
                    UpdateWarningMessage("p is not prime");
                    return;
                }
                if (!MyMath.IsPrime(q))
                {
                    UpdateWarningMessage("q is not prime");
                    return;
                }
                //checkign if e is relative prime to p * q
                if (!((1 < e && e < MyMath.PhiFuncPrime(p, q)) && MyMath.GCD(e, MyMath.PhiFuncPrime(p, q)) == 1))
                {
                    UpdateWarningMessage("e must be 1 < e < phi(p * q) and relative prime to p * q");
                    return;
                }
                //
                if (p * q < 128)
                {
                    UpdateWarningMessage("p * q must be bigger than 127");
                    return;
                }

                d = MyMath.ModInverse(e, MyMath.PhiFuncPrime(p, q));
            }


            RSAEncryption encryption = new RSAEncryption();

            encryption.P = p;
            encryption.Q = q;
            encryption.E = e;
            encryption.D = d;


            view.PublicKey  = $"({encryption.E}|{encryption.N})";
            view.PrivateKey = $"({encryption.D}|{encryption.N})";

            view.EncryptedMessage = encryption.Encryption(ASCIIEncoding.ASCII.GetBytes(view.Message));
            view.DecryptedMessage = encryption.Decryption();
        }