Inheritance: KeyGenerationParameters
		public static int Main(
            string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("DsaElGamalKeyRingGenerator [-a] identity passPhrase");
                return 0;
            }

			IAsymmetricCipherKeyPairGenerator dsaKpg = GeneratorUtilities.GetKeyPairGenerator("DSA");
            DsaParametersGenerator pGen = new DsaParametersGenerator();
            pGen.Init(1024, 80, new SecureRandom());
            DsaParameters dsaParams = pGen.GenerateParameters();
            DsaKeyGenerationParameters kgp = new DsaKeyGenerationParameters(new SecureRandom(), dsaParams);
            dsaKpg.Init(kgp);


			//
            // this takes a while as the key generator has to Generate some DSA parameters
            // before it Generates the key.
            //
            AsymmetricCipherKeyPair dsaKp = dsaKpg.GenerateKeyPair();


			IAsymmetricCipherKeyPairGenerator elgKpg = GeneratorUtilities.GetKeyPairGenerator("ELGAMAL");

			BigInteger g = new BigInteger("153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16);
            BigInteger p = new BigInteger("9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16);

			ElGamalParameters elParams = new ElGamalParameters(p, g);
            ElGamalKeyGenerationParameters elKgp = new ElGamalKeyGenerationParameters(new SecureRandom(), elParams);
            elgKpg.Init(elKgp);

			//
            // this is quicker because we are using preGenerated parameters.
            //
            AsymmetricCipherKeyPair elgKp = elgKpg.GenerateKeyPair();

			Stream out1, out2;
			if (args[0].Equals("-a"))
            {
                if (args.Length < 3)
                {
                    Console.WriteLine("DSAElGamalKeyRingGenerator [-a] identity passPhrase");
                    return 0;
                }

				out1 = File.Create("secret.asc");
                out2 = File.Create("pub.asc");

				ExportKeyPair(out1, out2, dsaKp, elgKp, args[1], args[2].ToCharArray(), true);
            }
            else
            {
                out1 = File.Create("secret.bpg");
                out2 = File.Create("pub.bpg");

				ExportKeyPair(out1, out2, dsaKp, elgKp, args[0], args[1].ToCharArray(), false);
            }
			out1.Close();
			out2.Close();
			return 0;
        }
        public void GenerateTest()
        {
            char[] passPhrase = "hello".ToCharArray();
            DsaParametersGenerator pGen = new DsaParametersGenerator();
            pGen.Init(512, 80, new SecureRandom());
            DsaParameters dsaParams = pGen.GenerateParameters();
            DsaKeyGenerationParameters dsaKgp = new DsaKeyGenerationParameters(new SecureRandom(), dsaParams);
            IAsymmetricCipherKeyPairGenerator dsaKpg = GeneratorUtilities.GetKeyPairGenerator("DSA");
            dsaKpg.Init(dsaKgp);

            //
            // this takes a while as the key generator has to Generate some DSA parameters
            // before it Generates the key.
            //
            AsymmetricCipherKeyPair dsaKp = dsaKpg.GenerateKeyPair();
            IAsymmetricCipherKeyPairGenerator elgKpg = GeneratorUtilities.GetKeyPairGenerator("ELGAMAL");

            BigInteger g = new BigInteger("153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16);
            BigInteger p = new BigInteger("9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16);

            ElGamalParameters elParams = new ElGamalParameters(p, g);
            ElGamalKeyGenerationParameters elKgp = new ElGamalKeyGenerationParameters(new SecureRandom(), elParams);
            elgKpg.Init(elKgp);

            //
            // this is quicker because we are using preGenerated parameters.
            //
            AsymmetricCipherKeyPair elgKp = elgKpg.GenerateKeyPair();
            PgpKeyPair dsaKeyPair = new PgpKeyPair(PublicKeyAlgorithmTag.Dsa, dsaKp, DateTime.UtcNow);
            PgpKeyPair elgKeyPair = new PgpKeyPair(PublicKeyAlgorithmTag.ElGamalEncrypt, elgKp, DateTime.UtcNow);

            PgpKeyRingGenerator keyRingGen = new PgpKeyRingGenerator(PgpSignature.PositiveCertification, dsaKeyPair,
                "test", SymmetricKeyAlgorithmTag.Aes256, passPhrase, null, null, new SecureRandom());

            keyRingGen.AddSubKey(elgKeyPair);

            PgpSecretKeyRing keyRing = keyRingGen.GenerateSecretKeyRing();

            keyRing.GetSecretKey().ExtractPrivateKey(passPhrase);

            PgpPublicKeyRing pubRing = keyRingGen.GeneratePublicKeyRing();

            PgpPublicKey vKey = null;
            PgpPublicKey sKey = null;

            foreach (PgpPublicKey pk in pubRing.GetPublicKeys())
            {
                if (pk.IsMasterKey)
                {
                    vKey = pk;
                }
                else
                {
                    sKey = pk;
                }
            }

            foreach (PgpSignature sig in sKey.GetSignatures())
            {
                if (sig.KeyId == vKey.KeyId
                    && sig.SignatureType == PgpSignature.SubkeyBinding)
                {
                    sig.InitVerify(vKey);

                    if (!sig.VerifyCertification(vKey, sKey))
                    {
                        Fail("failed to verify sub-key signature.");
                    }
                }
            }
        }
        public void Init(
			KeyGenerationParameters parameters)
        {
            this.param = (ElGamalKeyGenerationParameters) parameters;
        }
		public void TestElGamal()
		{
			BigInteger g512 = new BigInteger("153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16);
			BigInteger p512 = new BigInteger("9494fec095f3b85ee286542b3836fc81a5dd0a0349b4c239dd38744d488cf8e31db8bcb7d33b41abb9e5a33cca9144b1cef332c94bf0573bf047a3aca98cdf3b", 16);

			ElGamalParameters dhParams = new ElGamalParameters(p512, g512);
			ElGamalKeyGenerationParameters parameters = new ElGamalKeyGenerationParameters(new SecureRandom(), dhParams);         ElGamalKeyPairGenerator kpGen = new ElGamalKeyPairGenerator();

			kpGen.Init(parameters);

			AsymmetricCipherKeyPair pair = kpGen.GenerateKeyPair();
			ElGamalPublicKeyParameters pu1 = (ElGamalPublicKeyParameters)pair.Public;
			ElGamalPrivateKeyParameters pv1 = (ElGamalPrivateKeyParameters)pair.Private;

			ElGamalPublicKeyParameters pu2 = new ElGamalPublicKeyParameters(pu1.Y, pu1.Parameters);
			ElGamalPrivateKeyParameters pv2 = new ElGamalPrivateKeyParameters(pv1.X, pv1.Parameters);
			ElGamalPublicKeyParameters pu3 = new ElGamalPublicKeyParameters(pv1.X, pu1.Parameters);
			ElGamalPrivateKeyParameters pv3 = new ElGamalPrivateKeyParameters(pu1.Y, pu1.Parameters);

			doTest(pu1, pu2, pu3);
			doTest(pv1, pv2, pv3);

			ElGamalParameters pr1 = pu1.Parameters;
			ElGamalParameters pr2 = new ElGamalParameters(pr1.P, pr1.G);
			ElGamalParameters pr3 = new ElGamalParameters(pr1.G, pr1.P);

			doTest(pr1, pr2, pr3);

			pu2 = new ElGamalPublicKeyParameters(pu1.Y, pr2);
			pv2 = new ElGamalPrivateKeyParameters(pv1.X, pr2);

			doTest(pu1, pu2, pu3);
			doTest(pv1, pv2, pv3);

			ElGamalTestKeyParameters k1 = new ElGamalTestKeyParameters(false, null);
			ElGamalTestKeyParameters k2 = new ElGamalTestKeyParameters(false, null);
			ElGamalTestKeyParameters k3 = new ElGamalTestKeyParameters(false, pu1.Parameters);

			doTest(k1, k2, k3);
		}
Exemple #5
0
		private void doTestEnc(
			int         size,
			int         privateValueSize,
			BigInteger  g,
			BigInteger  p)
		{
			ElGamalParameters dhParams = new ElGamalParameters(p, g, privateValueSize);
			ElGamalKeyGenerationParameters ekgParams = new ElGamalKeyGenerationParameters(new SecureRandom(), dhParams);
			ElGamalKeyPairGenerator kpGen = new ElGamalKeyPairGenerator();

			kpGen.Init(ekgParams);

			//
			// generate pair
			//
			AsymmetricCipherKeyPair pair = kpGen.GenerateKeyPair();

			ElGamalPublicKeyParameters pu = (ElGamalPublicKeyParameters) pair.Public;
			ElGamalPrivateKeyParameters pv = (ElGamalPrivateKeyParameters) pair.Private;

			checkKeySize(privateValueSize, pv);

			ElGamalEngine e = new ElGamalEngine();

			e.Init(true, pu);

			if (e.GetOutputBlockSize() != size / 4)
			{
				Fail(size + " GetOutputBlockSize() on encryption failed.");
			}

			byte[] message = Hex.Decode("5468697320697320612074657374");

			byte[] pText = message;
			byte[] cText = e.ProcessBlock(pText, 0, pText.Length);

			e.Init(false, pv);

			if (e.GetOutputBlockSize() != (size / 8) - 1)
			{
				Fail(size + " GetOutputBlockSize() on decryption failed.");
			}

			pText = e.ProcessBlock(cText, 0, cText.Length);

			if (!Arrays.AreEqual(message, pText))
			{
				Fail(size + " bit test failed");
			}



			e.Init(true, pu);
			byte[] bytes = new byte[e.GetInputBlockSize() + 2];

			try
			{
				e.ProcessBlock(bytes, 0, bytes.Length);

				Fail("out of range block not detected");
			}
			catch (DataLengthException)
			{
				// expected
			}

			try
			{
				bytes[0] = (byte)0xff;

				e.ProcessBlock(bytes, 0, bytes.Length - 1);

				Fail("out of range block not detected");
			}
			catch (DataLengthException)
			{
				// expected
			}

			try
			{
				bytes[0] = (byte)0x7f;

				e.ProcessBlock(bytes, 0, bytes.Length - 1);
			}
			catch (DataLengthException)
			{
				Fail("in range block failed");
			}
		}
Exemple #6
0
		/**
		 * this test is can take quiet a while
		 *
		 * @param size size of key in bits.
		 */
		private void doTestGeneration(
			int size)
		{
			ElGamalParametersGenerator pGen = new ElGamalParametersGenerator();

			pGen.Init(size, 10, new SecureRandom());

			ElGamalParameters elParams = pGen.GenerateParameters();

			if (elParams.L != 0)
			{
				Fail("ElGamalParametersGenerator failed to set L to 0 in generated ElGamalParameters");
			}

			ElGamalKeyGenerationParameters ekgParams = new ElGamalKeyGenerationParameters(new SecureRandom(), elParams);

			ElGamalKeyPairGenerator kpGen = new ElGamalKeyPairGenerator();

			kpGen.Init(ekgParams);

			//
			// generate first pair
			//
			AsymmetricCipherKeyPair pair = kpGen.GenerateKeyPair();

			ElGamalPublicKeyParameters pu = (ElGamalPublicKeyParameters)pair.Public;
			ElGamalPrivateKeyParameters pv = (ElGamalPrivateKeyParameters)pair.Private;

			ElGamalEngine e = new ElGamalEngine();

			e.Init(true, new ParametersWithRandom(pu, new SecureRandom()));

			byte[] message = Hex.Decode("5468697320697320612074657374");

			byte[] pText = message;
			byte[] cText = e.ProcessBlock(pText, 0, pText.Length);

			e.Init(false, pv);

			pText = e.ProcessBlock(cText, 0, cText.Length);

			if (!Arrays.AreEqual(message, pText))
			{
				Fail("generation test failed");
			}
		}
		private void doTestGP(
			int			size,
			int         privateValueSize,
			BigInteger	g,
			BigInteger	p)
		{
			IAsymmetricCipherKeyPairGenerator keyGen = GeneratorUtilities.GetKeyPairGenerator("ElGamal");

//			DHParameterSpec elParams = new DHParameterSpec(p, g);
//			keyGen.initialize(elParams);
			ElGamalParameters elParams = new ElGamalParameters(p, g, privateValueSize);
			ElGamalKeyGenerationParameters elKgp = new ElGamalKeyGenerationParameters(
				new SecureRandom(), elParams);
			keyGen.Init(elKgp);

			AsymmetricCipherKeyPair keyPair = keyGen.GenerateKeyPair();
			SecureRandom rand = new SecureRandom();

			checkKeySize(privateValueSize, keyPair);

			IBufferedCipher cipher = CipherUtilities.GetCipher("ElGamal");

			cipher.Init(true, new ParametersWithRandom(keyPair.Public, rand));

			byte[] inBytes = Encoding.ASCII.GetBytes("This is a test");

			if (cipher.GetOutputSize(inBytes.Length) != (size / 8) * 2)
			{
				Fail("getOutputSize wrong on encryption");
			}

			byte[] outBytes = cipher.DoFinal(inBytes);

			cipher.Init(false, keyPair.Private);

			if (cipher.GetOutputSize(outBytes.Length) != (size / 8) - 1)
			{
				Fail("GetOutputSize wrong on decryption");
			}


			//
			// No Padding - maximum length
			//
			byte[] modBytes = ((ElGamalPublicKeyParameters)keyPair.Public).Parameters.P.ToByteArray();
			byte[] maxInput = new byte[modBytes.Length - 1];

			maxInput[0] |= 0x7f;

			cipher.Init(true, new ParametersWithRandom(keyPair.Public, rand));

			outBytes = cipher.DoFinal(maxInput);

			cipher.Init(false, keyPair.Private);

			outBytes = cipher.DoFinal(outBytes);

			if (!AreEqual(outBytes, maxInput))
			{
				Fail("NoPadding test failed on decrypt expected "
					+ Hex.ToHexString(maxInput) + " got "
					+ Hex.ToHexString(outBytes));
			}


			//
			// encrypt/decrypt
			//
			IBufferedCipher c1 = CipherUtilities.GetCipher("ElGamal");
			IBufferedCipher c2 = CipherUtilities.GetCipher("ElGamal");

			c1.Init(true, new ParametersWithRandom(keyPair.Public, rand));

			byte[] out1 = c1.DoFinal(inBytes);

			c2.Init(false, keyPair.Private);

			byte[] out2 = c2.DoFinal(out1);

			if (!AreEqual(inBytes, out2))
			{
				Fail(size + " encrypt test failed");
			}


			//
			// encrypt/decrypt with update
			//
			int outLen = c1.ProcessBytes(inBytes, 0, 2, out1, 0);

			outLen += c1.DoFinal(inBytes, 2, inBytes.Length - 2, out1, outLen);

			outLen = c2.ProcessBytes(out1, 0, 2, out2, 0);

			outLen += c2.DoFinal(out1, 2, out1.Length - 2, out2, outLen);

			if (!AreEqual(inBytes, out2))
			{
				Fail(size + " encrypt with update test failed");
			}



			//
			// public key encoding test
			//
//			byte[] pubEnc = keyPair.Public.GetEncoded();
			byte[] pubEnc = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public).GetDerEncoded();

//			KeyFactory keyFac = KeyFactory.GetInstance("ElGamal");
//			X509EncodedKeySpec pubX509 = new X509EncodedKeySpec(pubEnc);
//			DHPublicKeyParameters pubKey = (DHPublicKeyParameters)keyFac.generatePublic(pubX509);
			ElGamalPublicKeyParameters pubKey = (ElGamalPublicKeyParameters)
				PublicKeyFactory.CreateKey(pubEnc);
			ElGamalParameters spec = pubKey.Parameters;

			if (!spec.G.Equals(elParams.G) || !spec.P.Equals(elParams.P))
			{
				Fail(size + " bit public key encoding/decoding test failed on parameters");
			}

			if (!((ElGamalPublicKeyParameters)keyPair.Public).Y.Equals(pubKey.Y))
			{
				Fail(size + " bit public key encoding/decoding test failed on y value");
			}

/*
			//
			// public key serialisation test
			//
			// TODO Is there some standard this serialization must conform to?
			BinaryFormatter formatter = new BinaryFormatter();

			MemoryStream bOut = new MemoryStream();
//			ObjectOutputStream oOut = new ObjectOutputStream(bOut);
//			oOut.writeObject(keyPair.Public);
			formatter.Serialize(bOut, keyPair.Public);

			MemoryStream bIn = new MemoryStream(bOut.ToArray(), false);
//			ObjectInputStream oIn = new ObjectInputStream(bIn);
//			pubKey = (DHPublicKeyParameters)oIn.readObject();
			pubKey = (ElGamalPublicKeyParameters) formatter.Deserialize(bIn);
			spec = pubKey.Parameters;

			if (!spec.G.Equals(elParams.G) || !spec.P.Equals(elParams.P))
			{
				Fail(size + " bit public key serialisation test failed on parameters");
			}

			if (!((ElGamalPublicKeyParameters )keyPair.Public).Y.Equals(pubKey.Y))
			{
				Fail(size + " bit public key serialisation test failed on y value");
			}
*/

			//
			// private key encoding test
			//
			// TODO Keys don't support GetEncoded
//			byte[] privEnc = keyPair.Private.GetEncoded();
			byte[] privEnc = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair.Private).GetDerEncoded();

//			PKCS8EncodedKeySpec privPKCS8 = new PKCS8EncodedKeySpec(privEnc);
//			DHPrivateKeyParameters privKey = (DHPrivateKeyParameters)keyFac.generatePrivate(privPKCS8);
			ElGamalPrivateKeyParameters privKey = (ElGamalPrivateKeyParameters)
				PrivateKeyFactory.CreateKey(privEnc);

			spec = privKey.Parameters;

			if (!spec.G.Equals(elParams.G) || !spec.P.Equals(elParams.P))
			{
				Fail(size + " bit private key encoding/decoding test failed on parameters");
			}

			if (!((ElGamalPrivateKeyParameters)keyPair.Private).X.Equals(privKey.X))
			{
				Fail(size + " bit private key encoding/decoding test failed on y value");
			}

/*
			//
			// private key serialisation test
			//
			bOut = new MemoryStream();
//			oOut = new ObjectOutputStream(bOut);
//			oOut.writeObject(keyPair.Private);
			formatter.Serialize(bOut, keyPair.Private);

			bIn = new MemoryStream(bOut.ToArray(), false);
//			oIn = new ObjectInputStream(bIn);
//			privKey = (DHPrivateKeyParameters)oIn.readObject();
			privKey = (ElGamalPrivateKeyParameters) formatter.Deserialize(bIn);
			spec = privKey.Parameters;

			if (!spec.G.Equals(elParams.G) || !spec.P.Equals(elParams.P))
			{
				Fail(size + " bit private key serialisation test failed on parameters");
			}

			if (!((ElGamalPrivateKeyParameters) keyPair.Private).X.Equals(privKey.X))
			{
				Fail(size + " bit private key serialisation test failed on y value");
			}
*/
		}