/// <summary> /// Generate key pair. /// </summary> /// <returns></returns> public override AsymmetricCipherKeyPair GenerateKeyPair() { ElGamalParametersGenerator generator2 = new ElGamalParametersGenerator(); generator2.Init(_keySize, _certainty, Common.ThreadSecureRandom.Value); ElGamalParameters parameters2 = generator2.GenerateParameters(); KeyGenerationParameters parameters = new ElGamalKeyGenerationParameters(Common.ThreadSecureRandom.Value, parameters2); IAsymmetricCipherKeyPairGenerator generator = new ElGamalKeyPairGenerator(); generator.Init(parameters); return(generator.GenerateKeyPair()); }
/// <summary> /// Generates an El Gamal Key pair given its bit size. /// </summary> /// <param name="keySize">Bit size for keys of 1024, 2048, 3072 or 4096.</param> /// <returns></returns> public AsymmetricCipherKeyPair ElGamalkeyGen(int keySize) { var secRand = new SecureRandom(); var probPrime = GetMODRFC3526(keySize); var elGenertor = GeneratorUtilities.GetKeyPairGenerator("ELGAMAL"); var elBaseGenerator = new BigInteger("2", 16); var elGamalParameterSet = new ElGamalParameters(probPrime, elBaseGenerator); var elParams = new ElGamalKeyGenerationParameters(secRand, elGamalParameterSet); elGenertor.Init(elParams); var keypair = elGenertor.GenerateKeyPair(); return(keypair); }
/** * 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 GenerateElGamal(Stream outSecret, Stream outPublic) { // Prepare a strong Secure Random with seed SecureRandom secureRandom = PgpEncryptionUtil.GetSecureRandom(); IAsymmetricCipherKeyPairGenerator dsaKpg = GeneratorUtilities.GetKeyPairGenerator("DSA"); DsaParametersGenerator pGen = new DsaParametersGenerator(); pGen.Init((int)PublicKeyLength.BITS_1024, 80, new SecureRandom()); // DSA is 1024 even for long 2048+ ElGamal keys DsaParameters dsaParams = pGen.GenerateParameters(); DsaKeyGenerationParameters kgp = new DsaKeyGenerationParameters(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"); Group elgamalGroup = Precomputed.GetElGamalGroup((int)this.publicKeyLength); if (elgamalGroup == null) { throw new ArgumentException("ElGamal Group not found for key length: " + this.publicKeyLength); } Org.BouncyCastle.Math.BigInteger p = elgamalGroup.GetP(); Org.BouncyCastle.Math.BigInteger g = elgamalGroup.GetG(); secureRandom = PgpEncryptionUtil.GetSecureRandom(); ElGamalParameters elParams = new ElGamalParameters(p, g); ElGamalKeyGenerationParameters elKgp = new ElGamalKeyGenerationParameters(secureRandom, elParams); elgKpg.Init(elKgp); // // this is quicker because we are using preGenerated parameters. // AsymmetricCipherKeyPair elgKp = elgKpg.GenerateKeyPair(); DsaElGamalKeyGeneratorUtil.ExportKeyPair(outSecret, outPublic, dsaKp, elgKp, identity, passphrase, true); }
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); }
public AsymmetricCipherKeyPair GenerateElGamalKeyPair(int keySize, BigInteger prime = null, BigInteger generator = null) { ElGamalParameters elGamalParameters; if (prime != null && generator != null) { elGamalParameters = new ElGamalParameters(prime, generator); } else { var elGamalParameterGenerator = new ElGamalParametersGenerator(); elGamalParameterGenerator.Init(keySize, 64, secureRandom.Generator); elGamalParameters = elGamalParameterGenerator.GenerateParameters(); } var keyGenerationParameters = new ElGamalKeyGenerationParameters(secureRandom.Generator, elGamalParameters); var keyPairGenerator = new ElGamalKeyPairGenerator(); keyPairGenerator.Init(keyGenerationParameters); return(keyPairGenerator.GenerateKeyPair()); }
public void Init( KeyGenerationParameters parameters) { this.param = (ElGamalKeyGenerationParameters)parameters; }
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"); * } */ }
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. // IAsymmetricCipherKeyPair dsaKp = dsaKpg.GenerateKeyPair(); IAsymmetricCipherKeyPairGenerator elgKpg = GeneratorUtilities.GetKeyPairGenerator("ELGAMAL"); IBigInteger g = new BigInteger("153d5d6172adb43045b68ae8e1de1070b6137005686d29d3d73a7749199681ee5b212c9b96bfdcfa5b20cd5e3fd2044895d609cf9b410b7a0f12ca1cb9a428cc", 16); IBigInteger 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. // IAsymmetricCipherKeyPair 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); }
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"); } }