Esempio n. 1
0
        /// <summary>
        /// Public key operation. Encrypts biInput with the keydata
        /// in the given public key packet.
        /// </summary>
        /// <param name="biInput">The plaintext that is about to
        /// be encrypted</param>
        /// <param name="pkpKey">The public key packet with the key
        /// material for the encryption</param>
        /// <returns>The encrypted ciphertext.</returns>
        /// <remarks>No remarks.</remarks>
        public override BigInteger[] Encrypt(BigInteger biInput, PublicKeyPacket pkpKey)
        {
            EG_Public_Key epkKey = new EG_Public_Key();

            epkKey.p = pkpKey.KeyMaterial[0];
            epkKey.g = pkpKey.KeyMaterial[1];
            epkKey.y = pkpKey.KeyMaterial[2];

            BigInteger k = new BigInteger();

            //Random number needed for encryption
            k = BigInteger.genRandom(epkKey.p.bitCount() - 1);

            while (k > (epkKey.p - 1))
            {
                k = new BigInteger();
                k = BigInteger.genRandom(epkKey.p.bitCount() - 1);
            }

            BigInteger B = epkKey.g.modPow(k, epkKey.p);
            BigInteger c = epkKey.y.modPow(k, epkKey.p);

            c = (biInput * c) % epkKey.p;
            //BigInteger c = (biInput * epkKey.y.modPow(k, epkKey.p)) % epkKey.p;

            BigInteger[] biOutput = new BigInteger[2];

            biOutput[0] = B;
            biOutput[1] = c;

            return(biOutput);
        }
Esempio n. 2
0
        /// <summary>
        /// Public key operation. Encrypts biInput with the keydata
        /// in the given public key packet.
        /// </summary>
        /// <param name="biInput">The plaintext that is about to
        /// be encrypted</param>
        /// <param name="pkpKey">The public key packet with the key
        /// material for the encryption</param>
        /// <returns>The encrypted ciphertext.</returns>
        /// <remarks>No remarks.</remarks>
        public override BigInteger[] Encrypt(BigInteger biInput, PublicKeyPacket pkpKey)
        {
            EG_Public_Key epkKey = new EG_Public_Key();
            epkKey.p = pkpKey.KeyMaterial[0];
            epkKey.g = pkpKey.KeyMaterial[1];
            epkKey.y = pkpKey.KeyMaterial[2];

            BigInteger k = new BigInteger();

            //Random number needed for encryption
            k = BigInteger.genRandom(epkKey.p.bitCount()-1);

            while (k > (epkKey.p-1)) {
                k = new BigInteger();
                k = BigInteger.genRandom(epkKey.p.bitCount()-1);
            }

            BigInteger B = epkKey.g.modPow(k, epkKey.p);
            BigInteger c = epkKey.y.modPow(k, epkKey.p);
            c = (biInput * c) % epkKey.p;
            //BigInteger c = (biInput * epkKey.y.modPow(k, epkKey.p)) % epkKey.p;

            BigInteger[] biOutput = new BigInteger[2];

            biOutput[0] = B;
            biOutput[1] = c;

            return biOutput;
        }