0

I am trying to do client side encryption for the data I am sending to S3. I want to take encryption keys as input from the user. In what format should I take the key from the user.

I tried to take input as the private key generated by ssh-keygen and tried reading it using the code mentioned at Get public key from private in Java. But I get the following error

Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)

I generated the key using ssh-keygen -t rsa

Community
  • 1
  • 1
Arpit Agarwal
  • 657
  • 2
  • 10
  • 15

1 Answers1

0
I want to take encryption keys as input from the user

I does not try your above need but I have generated 256bit Secret key to encrypt and decrypt my data in S3.

// Code To Generate Secret Key.
KeyGenerator symKeyGenerator = KeyGenerator.getInstance("AES");
symKeyGenerator.init(256);
SecretKey symKey = symKeyGenerator.generateKey();
System.out.println(new String(Base64.encodeBase64(symKey.getEncoded())));

And I used like this to Download and upload objects in S3

// Code To Make Objects Encrypt while uploading and Decrypt while Downloading.

public static void s3WithEncryption(AWSCredentials credentials) {
        String myKeyPair = "KEY_GENERATED_USING_ABOVE_CODE";
        SecretKey mySymmetricKey = new SecretKeySpec(Base64.decodeBase64(myKeyPair.getBytes()), "AES");
        EncryptionMaterials materials = new EncryptionMaterials(mySymmetricKey);
        AmazonS3Client encryptedS3 = new AmazonS3EncryptionClient(credentials, materials);
        try {
            File file = new File("D:/dummy.txt");
            SSECustomerKey sseKey = new SSECustomerKey(myKeyPair);
            PutObjectRequest objectRequest = new PutObjectRequest(bucketName, "withEncrypt/dummy.txt", file);
            encryptedS3.putObject(objectRequest.withSSECustomerKey(sseKey));
            System.out.println("s3WithEncryption: Object uploaded!!!");
            S3Object downloadedObject = encryptedS3.getObject(new GetObjectRequest(bucketName, "withEncrypt/" + file.getName()).withSSECustomerKey(sseKey));
            downloadFile("D:/withEncrption", downloadedObject.getObjectContent(), "Steps to configure unifiedUI.txt");
            System.out.println("s3WithEncryption: Object Downloaded!!!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            encryptedS3.shutdown();
        }

    }
ashokramcse
  • 2,841
  • 2
  • 19
  • 41