Exemple #1
0
        /// <summary>
        /// Creates a new ElGamal secret key and returns it as a
        /// 2 dimensional array of biginteger. return[0] holds
        /// the public values of the key and return[1] all the
        /// secret values.
        /// </summary>
        /// <remarks>
        /// Creates a new ElGamal secret key and returns it as a
        /// 2 dimensional array of biginteger. return[0] holds
        /// the public values of the key and return[1] all the
        /// secret values.<br></br>
        /// The order of the public components is p, g, y
        /// The order of the secret components is x.
        /// </remarks>
        /// <param name="nbits">The size of the key in bits.</param>
        /// <returns>A new ElGamal secret key and returns it as a
        /// 2 dimensional array of biginteger. return[0] holds
        /// the public values of the key and return[1] all the
        /// secret values.<br></br>
        /// The order of the public components is p, g, y
        /// The order of the secret components is x.
        /// </returns>
        public override BigInteger[][] Generate(int nBits)
        {
            EG_Secret_Key eskKey = GenerateKey(nBits);

            biGeneratedKey       = new BigInteger[2][];
            biGeneratedKey[0]    = new BigInteger[3];
            biGeneratedKey[0][0] = eskKey.p;
            biGeneratedKey[0][1] = eskKey.g;
            biGeneratedKey[0][2] = eskKey.y;

            biGeneratedKey[1]    = new BigInteger[1];
            biGeneratedKey[1][0] = eskKey.x;

            return(biGeneratedKey);
        }
Exemple #2
0
        private EG_Secret_Key GenerateKey(int nBits)
        {
            BigInteger    q = new BigInteger();
            BigInteger    p;
            BigInteger    g;
            BigInteger    gPowTwo;
            BigInteger    gPowQ;
            EG_Secret_Key eskKey = new EG_Secret_Key();

            /*
             * // construct a prime p = 2q + 1
             * do {
             *      q = BigInteger.genRandom(nBits - 1);
             *      System.Windows.Forms.Application.DoEvents();
             *      p = (2*q) + 1;
             * } while ((!p.isProbablePrime()) || (!q.isProbablePrime()));
             */

            q = BigInteger.genPseudoPrime(nBits - 1);
            p = BigInteger.genPseudoPrime(nBits);

            // find a generator
            do
            {
                g       = new BigInteger();
                g       = BigInteger.genRandom(nBits - 1);
                gPowTwo = g.modPow(new BigInteger(2), p);
                gPowQ   = g.modPow(q, p);
            } while ((gPowTwo == 1) || (gPowQ == 1));

            BigInteger x;

            do
            {
                x = new BigInteger();
                x = BigInteger.genRandom(nBits);
            } while (x >= p - 1);

            BigInteger y = g.modPow(x, p);

            eskKey.p = p;
            eskKey.g = g;
            eskKey.x = x;
            eskKey.y = y;

            return(eskKey);
        }
Exemple #3
0
        /// <summary>
        /// Secret key operation. Decrypts biCipher with the keydata
        /// in the given secret key packet.
        /// </summary>
        /// <param name="biInput">The ciphertext that is about to
        /// be decrypted</param>
        /// <param name="skpKey">The secret key packet with the key
        /// material for the decryption</param>
        /// <param name="strPassphrase">The passphrase for the 
        /// keymaterial</param>
        /// <returns>The decrypted ciphertext.</returns>
        /// <remarks>No remarks.</remarks>
        public override BigInteger Decrypt(BigInteger[] biInput, SecretKeyPacket skpKey, string strPassphrase)
        {
            BigInteger[] biKeyMaterial = skpKey.GetDecryptedKeyMaterial(strPassphrase);
            EG_Secret_Key eskKey = new EG_Secret_Key();
            eskKey.x = biKeyMaterial[0];
            eskKey.p = skpKey.PublicKey.KeyMaterial[0];
            eskKey.g = skpKey.PublicKey.KeyMaterial[1];
            eskKey.y = skpKey.PublicKey.KeyMaterial[2];

            if (biInput.Length != 2)
                throw new ArgumentException("biInput is not an ElGamal encrypted Packet");

            BigInteger B = biInput[0];
            BigInteger c = biInput[1];

            BigInteger z = B.modPow(eskKey.x, eskKey.p).modInverse(eskKey.p);

            BigInteger output = (z * c) % eskKey.p;

            return output;
        }
Exemple #4
0
        /// <summary>
        /// Secret key operation. Decrypts biCipher with the keydata
        /// in the given secret key packet.
        /// </summary>
        /// <param name="biInput">The ciphertext that is about to
        /// be decrypted</param>
        /// <param name="skpKey">The secret key packet with the key
        /// material for the decryption</param>
        /// <param name="strPassphrase">The passphrase for the
        /// keymaterial</param>
        /// <returns>The decrypted ciphertext.</returns>
        /// <remarks>No remarks.</remarks>
        public override BigInteger Decrypt(BigInteger[] biInput, SecretKeyPacket skpKey, string strPassphrase)
        {
            BigInteger[]  biKeyMaterial = skpKey.GetDecryptedKeyMaterial(strPassphrase);
            EG_Secret_Key eskKey        = new EG_Secret_Key();

            eskKey.x = biKeyMaterial[0];
            eskKey.p = skpKey.PublicKey.KeyMaterial[0];
            eskKey.g = skpKey.PublicKey.KeyMaterial[1];
            eskKey.y = skpKey.PublicKey.KeyMaterial[2];

            if (biInput.Length != 2)
            {
                throw new ArgumentException("biInput is not an ElGamal encrypted Packet");
            }

            BigInteger B = biInput[0];
            BigInteger c = biInput[1];

            BigInteger z = B.modPow(eskKey.x, eskKey.p).modInverse(eskKey.p);

            BigInteger output = (z * c) % eskKey.p;

            return(output);
        }
Exemple #5
0
        private EG_Secret_Key GenerateKey(int nBits)
        {
            BigInteger q = new BigInteger();
            BigInteger p;
            BigInteger g;
            BigInteger gPowTwo;
            BigInteger gPowQ;
            EG_Secret_Key eskKey = new EG_Secret_Key();

            /*
            // construct a prime p = 2q + 1
            do {
                q = BigInteger.genRandom(nBits - 1);
                System.Windows.Forms.Application.DoEvents();
                p = (2*q) + 1;
            } while ((!p.isProbablePrime()) || (!q.isProbablePrime()));
            */

            q = BigInteger.genPseudoPrime(nBits - 1);
            p = BigInteger.genPseudoPrime(nBits);

            // find a generator
            do {
                g = new BigInteger();
                g = BigInteger.genRandom(nBits - 1);
                gPowTwo = g.modPow(new BigInteger(2), p);
                gPowQ = g.modPow(q, p);
            } while ((gPowTwo == 1) || (gPowQ == 1));

            BigInteger x;

            do {
                x = new BigInteger();
                x = BigInteger.genRandom(nBits);
            } while (x >= p-1);

            BigInteger y = g.modPow(x, p);

            eskKey.p = p;
            eskKey.g = g;
            eskKey.x = x;
            eskKey.y = y;

            return eskKey;
        }