title: Azure SDK for Java - sample program throwing InvalidKeyException
body: <p>Using Azure Storage SDK for Java, I am trying to perform basic create, read, update, delete operations on Azure Table Storage as given in the link below:
  <a href="https://azure.microsoft.com/en-us/documentation/articles/storage-java-how-to-use-table-storage/" rel="nofollow">https://azure.microsoft.com/en-us/documentation/articles/storage-java-how-to-use-table-storage/</a></p>
  
  <p>Sample program for creating a table:</p>
  
  <pre><code>package com.azure.test;
  import java.io.UnsupportedEncodingException;
  import com.microsoft.azure.storage.*;
  import com.microsoft.azure.storage.table.CloudTable;
  import com.microsoft.azure.storage.table.CloudTableClient;
  import com.microsoft.windowsazure.core.utils.Base64;
  
  public class App 
  {
  
      public static void main( String[] args ) throws StorageException,    UnsupportedEncodingException
  {
  
      String storageConnectionString =
              "DefaultEndpointsProtocol=http;" +
              "AccountName=accountname;" + 
              "AccountKey=storagekey;"+
             "EndpointSuffix=table.core.windows.net"; 
  
      try
      {
          // Retrieve storage account from connection-string.
          CloudStorageAccount storageAccount =
             CloudStorageAccount.parse(storageConnectionString);
  
          CloudTableClient tableClient =               storageAccount.createCloudTableClient();
  
        //Create the table if it doesn't exist.
         String tableName = "MyTable";
         CloudTable cloudTable = tableClient.getTableReference(tableName);
         cloudTable.createIfNotExists();               
  
      }
      catch (Exception e)
      {
          // Output the stack trace.
          e.printStackTrace();
          System.out.println(e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>The code seems to be fairly simple to understand. It would connect to the Azure table storage and if a table with a given name does not exist it will create it. But I am getting a InvalidKeyException(full exception pasted below). </p>
  
  <blockquote>
    <p>java.security.InvalidKeyException: Storage Key is not a valid base64 encoded string.<br>
        at com.microsoft.azure.storage.StorageCredentials.tryParseCredentials(StorageCredentials.java:68)<br>
        at com.microsoft.azure.storage.CloudStorageAccount.tryConfigureServiceAccount(CloudStorageAccount.java:408)<br>
        at com.microsoft.azure.storage.CloudStorageAccount.parse(CloudStorageAccount.java:259)<br>
        at com.azure.test.App.main(App.java:71)</p>
  </blockquote>
  
  <p>I am surprised that not many people using Azure Storage are facing this issue. I tried to encode the storage key using and used the encoded key in the connection string but still no use.</p>
  
  <pre><code>String encodedKey=Base64.encode(storageKey.getBytes())
  
  String storageConnectionString =
              "DefaultEndpointsProtocol=http;" +
              "AccountName=accountname" + 
              "AccountKey="+encodedKey+
             "EndpointSuffix=table.core.windows.net;"; 
  </code></pre>
  
  <p>Can anyone please help me with this? I searched in google a lot and I am able to find one user raised a similar issue on discus but there is no answer provided for that or rather that answer was not helpful.</p>