title: Is there a workaround for: java.lang.RuntimeException: Could not generate DH keypair
body: <p>I am testing a Java application. I am trying to start an SSL handshake using DH ciphersuite. but I am getting the following error:</p>
  
  <pre><code>java.lang.RuntimeException: Could not generate DH keypair
  </code></pre>
  
  <p>Some people have suggested <code>BouncyCastle</code>, but many people have reported errors with it, so I am not encouraged to use it if there is another alternative.</p>
  
  <p>One have suggested downloading <code>Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files</code> from <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html" rel="noreferrer">http://www.oracle.com/technetwork/java/javase/downloads/index.html</a>. I did replaced the following two files <code>java.security</code> and <code>java.policy</code> in <code>C:\Program Files (x86)\Java\jre7\lib\security</code>. Note that I also noticed that I have <code>Java\jre7\security</code> installed in: <code>Program Files (x86)</code> and <code>Program Files</code> and I replaced both. But, I still see the same error. </p>
  
  <p>Is there any  workaround for this error ?</p>
  
  <p><strong>EDIT:</strong>
  The stack trace:</p>
  
  <pre><code>javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair
      at sun.security.ssl.Alerts.getSSLException(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.handleException(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
      at MyClass.MyClass.myFunction(MyProg.java:78)
      at MyClass.MyClass.main(MyClass.java:233)
  Caused by: java.lang.RuntimeException: Could not generate DH keypair
      at sun.security.ssl.DHCrypt.&lt;init&gt;(Unknown Source)
      at sun.security.ssl.ClientHandshaker.serverKeyExchange(Unknown Source)
      at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
      at sun.security.ssl.Handshaker.processLoop(Unknown Source)
      at sun.security.ssl.Handshaker.process_record(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
      ... 4 more
  Caused by: java.security.InvalidAlgorithmParameterException: Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive)
      at com.sun.crypto.provider.DHKeyPairGenerator.initialize(DHKeyPairGenerator.java:120)
      at java.security.KeyPairGenerator$Delegate.initialize(Unknown Source)
      ... 11 more
  </code></pre>
  
  <p><strong>EDIT2:</strong> 
  My code is acting as a client trying to initiate SSL handshake with a remote server (website). I set the client's cipher suite list to:</p>
  
  <pre><code>{
  "TLS_ECDHE_RSA_WITH_RC4_128_SHA",
  "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
  "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", 
  "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
  "TLS_ECDHE_RSA_WITH_NULL_SHA",
  "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",  
  "TLS_DHE_RSA_WITH_AES_128_CBC_SHA",
  "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA",
  "SSL_DHE_RSA_WITH_DES_CBC_SHA",
  "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA"
  };
  </code></pre>
  
  <p>All ciphersuites in the client's list are supported by Java. How can configure the Java client to support initiating an SSL handshake when the server offers long DH keys ? </p>