title: How to fix " The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption" error
body: <p>I am trying to connect MS SQL server 2016 through java. I am using JDK1.8 and mssql-jdbc-7.2.1.jre8.jar file.</p>
  
  <pre><code>public class ConnectToMSSQL {
      public static void main(String[] args) {
          String connectionURL = "jdbc:sqlserver://localhost:1433;databaseName=TestSQL;user=sa;password=password";
          try {
              Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
              Connection connection = DriverManager.getConnection(connectionURL);
              if (connection != null) {
                  System.out.println("Connected");
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>While running the above code, got the below error, how to fix this?</p>
  
  <pre><code>com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "RSA premaster secret error". ClientConnectionId:8438be77-02cd-4d82-b84d-16c0045ac415
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2887)
      at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1881)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2452)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2103)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1950)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1162)
      at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:735)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at database.ConnectToMSSQL.main(ConnectToMSSQL.java:12)
  Caused by: javax.net.ssl.SSLKeyException: RSA premaster secret error
      at sun.security.ssl.RSAClientKeyExchange.&lt;init&gt;(Unknown Source)
      at sun.security.ssl.ClientHandshaker.serverHelloDone(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)
      at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
      at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1799)
      ... 8 more
  Caused by: java.security.NoSuchAlgorithmException: SunTls12RsaPremasterSecret KeyGenerator not available
      at javax.crypto.KeyGenerator.&lt;init&gt;(KeyGenerator.java:169)
      at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223)
      at sun.security.ssl.JsseJce.getKeyGenerator(Unknown Source)
      ... 18 more
  </code></pre>