-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting BadCiphertextException: Invalid version #2042
Comments
I have removed several AWS related names and arns here for confidentiality. |
Hey @Smaz2024,
It looks like you are Base64 encoding the ciphertext after you successfully encrypting it but not decoding it back to bytes before you decrypt it. If you base64 decode it before you pass it to the decryptData method the Please let us know if you run into more issues. Thanks! |
Hi @josecorella : Thanks a lot for your reply and helped. The solution worked for me . However I was experimenting more on the same codebase.
Code
Error :Unable to decrypt data key: No Encrypted Data Keys found to match.
Code
Error:Unable to decrypt data key: No Encrypted Data Keys found to match. The reason I am trying to do so because the encrypting and decrypting parties can be in different cloud/on-prem environment or different account in AWS. So separate key rings can be used. Please help |
I am sorry Crypto Tools did not respond to your latest post sooner. When a Keyring wraps a data key, Because you are using different Keyrings at encrypt and decrypt, Your options:
But they all boil down to a consistent description of what encrypted the data key. Think of a regular physical key chain. The same principle applies here. |
I am creating a POC where I have created a public and private key pair. The private key is stored in KMS. The public key is stored in Secrets Manager. I am using the following code for envelope encryption.
Java Code
package com.poc.envelope.encryption;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Collections;
import java.util.Map;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemWriter;
import com.amazonaws.encryptionsdk.AwsCrypto;
import com.amazonaws.encryptionsdk.CommitmentPolicy;
import com.amazonaws.encryptionsdk.CryptoAlgorithm;
import com.amazonaws.encryptionsdk.CryptoResult;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.kms.KmsClient;
import software.amazon.awssdk.services.kms.model.EncryptionAlgorithmSpec;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse;
import software.amazon.cryptography.materialproviders.IKeyring;
import software.amazon.cryptography.materialproviders.MaterialProviders;
import software.amazon.cryptography.materialproviders.model.CreateAwsKmsRsaKeyringInput;
import software.amazon.cryptography.materialproviders.model.MaterialProvidersConfig;
//https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/java-example-code.html
public class HandleRequestForEnvelopeEncryption implements RequestHandler<Map<String, String>, String> {
LambdaLogger logger;
}
pom.xml
4.0.0
aws.envelope.encryption
aws.envelope.encryption
0.0.1-SNAPSHOT
=================
Error I am getting
com.amazonaws.encryptionsdk.exception.BadCiphertextException: Invalid version
at com.amazonaws.encryptionsdk.model.CiphertextHeaders.deserialize(CiphertextHeaders.java:588)
at com.amazonaws.encryptionsdk.ParsedCiphertext.(ParsedCiphertext.java:42)
at com.amazonaws.encryptionsdk.AwsCrypto.decryptData(AwsCrypto.java:752)
I have tried several options but it did not help. PLease share inputs
The text was updated successfully, but these errors were encountered: