GenerateKeyPair() 공개 메소드

public GenerateKeyPair ( ) : AsymmetricCipherKeyPair
리턴 AsymmetricCipherKeyPair
예제 #1
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");
			}
		}
예제 #2
0
		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);
		}
예제 #3
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");
			}
		}