Exemplo n.º 1
0
        public int Encrypt(int p, int q, int M, int e)
        {
            // Get n
            long         n    = p * q;
            NumberTheory calc = new NumberTheory();
            // Get C = M^e mod n
            int C = (int)calc.FastPower(M, e, n);

            return(C);
        }
Exemplo n.º 2
0
        public List <int> GetKeys(int q, int alpha, int xa, int xb)
        {
            NumberTheory calc = new NumberTheory();
            // q: prime number
            // alpha: primitive root of q
            // UserA Key Generation
            long ya = calc.FastPower(alpha, xa, q);
            // UserB Key Generation
            long yb = calc.FastPower(alpha, xb, q);

            List <int> keys = new List <int>
            {
                // Secret Key by User A
                (int)(calc.FastPower(yb, xa, q)),
                // Secret Key by User B
                (int)(calc.FastPower(ya, xb, q))
            };

            return(keys);
        }
Exemplo n.º 3
0
        public int Decrypt(int p, int q, int C, int e)
        {
            long n = p * q;
            // Get phi(n)
            // Call Euclidean Extended to get d
            NumberTheory calc = new NumberTheory();
            long         d    = calc.GetMultiplicativeInverse(e, calc.Phi(p, q));
            // Get M = C^d mod d
            int M = (int)calc.FastPower(C, d, n);

            return(M);
        }