1

I am developing a code where I need to upload the file data into azure storage blob. My requirement is to upload this data in encrypted format,thus I am using azure key vault.

final String storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=abc;AccountKey=pqr+lov=="; 
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
com.microsoft.azure.storage.blob.CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference("plmcontainer2");
container.createIfNotExists();
String filePath = "C:\\STSWorkspace\\PLMSubscriberMS\\Payload.xml";
com.microsoft.azure.storage.blob.CloudBlockBlob blob = container.getBlockBlobReference("Payload4.xml");
java.io.File source = new java.io.File(filePath);
java.io.FileInputStream fileInputStream=new java.io.FileInputStream(source);
// blob.upload(fileInputStream, source.length());


//encryption code

// Create the IKey used for encryption.
RsaKey key = new RsaKey("private:key1" /* key identifier */);


// Create the encryption policy to be used for upload and download.
BlobEncryptionPolicy policy = new BlobEncryptionPolicy(key, null);

// Set the encryption policy on the request options.
BlobRequestOptions options = new BlobRequestOptions();

options.setEncryptionPolicy(policy);

AccessCondition accessCondition = null;
operationContext opContext = null;
// Upload the encrypted contents to the blob.
blob.upload(fileInputStream, source.length(), null, options, null); //here is exception

On the last line I am getting an exception, if I change it to blob.upload(fileInputStream, source.length());

then the data is uploaded into blog but in plain text. how do I use blob.upload(fileInputStream, source.length(), null, options, null); what should I place at the location of null.

Exception

Exception in thread "main" com.microsoft.azure.storage.StorageException: A Client side exception occurred, please check the inner exception for details
    at com.microsoft.azure.storage.StorageException.translateClientException(StorageException.java:42)
    at com.microsoft.azure.storage.blob.BlobEncryptionPolicy.createAndSetEncryptionContext(BlobEncryptionPolicy.java:305)
    at com.microsoft.azure.storage.blob.CloudBlockBlob.openOutputStream(CloudBlockBlob.java:575)
    at com.microsoft.azure.storage.blob.CloudBlockBlob.upload(CloudBlockBlob.java:715)
    at com.encrypt.blob.BlobEncryption.main(BlobEncryption.java:55)
Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1026)
    at javax.crypto.Cipher.implInit(Cipher.java:801)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
    at javax.crypto.Cipher.init(Cipher.java:1249)
    at javax.crypto.Cipher.init(Cipher.java:1186)
    at com.microsoft.azure.storage.blob.BlobEncryptionPolicy.createAndSetEncryptionContext(BlobEncryptionPolicy.java:288)
    ... 3 more
Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • did you check the inner exception on what exactly is the error ? the properties you are referring are https://azure.github.io/azure-sdk-for-java/com/microsoft/azure/storage/OperationContext.html and https://azure.github.io/azure-sdk-for-java/com/microsoft/azure/storage/AccessCondition.html they are optional and hence you can pass null and the defaults will be used – Aravind Aug 01 '16 at 08:50
  • 1
    @Aravind yes, I am new to azure. I was not sure about this problem as a reason I put it here. I used the code from [Here](https://azure.microsoft.com/en-in/documentation/articles/storage-client-side-encryption-java/) –  Aug 01 '16 at 08:54
  • You've copied the code verbatim from the source. You would need to provide a proper RsaKey here in your code: `RsaKey key = new RsaKey("private:key1" /* key identifier */);`. Please see this link for generating an RSA key using Java: http://www.javamex.com/tutorials/cryptography/rsa_encryption.shtml. – Gaurav Mantri Aug 01 '16 at 09:09
  • 1
    @GauravMantri Thanks for the feedback.The link you posted is not working –  Aug 01 '16 at 09:15
  • 1
    @GauravMantri can you please suggest me another link –  Aug 01 '16 at 09:16
  • @GauravMantri Do I need to pass both public and private key there –  Aug 01 '16 at 09:20
  • Sorry! Unfortunately I don't know much about this feature to help you more. – Gaurav Mantri Aug 01 '16 at 09:21
  • I tried but same error as I posted above –  Aug 01 '16 at 11:11

1 Answers1

0

@AnandDeshmukh, I reproduced the issue. It seems that the issue was caused by not installing Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files for your current JRE or JDK. There is an answered SO thread Java Security: Illegal key size or default parameters? which you can refer to.

My Java Environment is JDK8u101. When I downloaded the jce files for Java 8 instead of the original files, the exception disappeared.

Please download the related version of JCE files to solve the issue.

Community
  • 1
  • 1
Peter Pan
  • 23,476
  • 4
  • 25
  • 43