AES cypher in GCM mode with null byte IV in java - java

I have to encrypt a json payload using AES cypher in GCM mode with null byte IV.
When i tried using the secret key and the json payload, i am getting a different result than expected. It only mismatches with AES GCM MAC part.
These are the parameters and my source code:
GCM_TAG_LENGTH= 16,
Symmetric key In Hex: 083080D3D0C521C02CD3AE2134363D09EA50DFF914677FAB9E22F18F9C28A3B9
jsonPayload:{"Parameter1":"Value1","Parameter2":"Value2","Parameter3":"Value3"}
OutPut (Expected): EF6BA2FFA05B6985FE129E3CB6845C4EA1E94AE98D31A538A4E24906FB720D764D640894CD9 DE7CEC00114396651A1CCAEDCF480C57A959E925C04492B9CF85FC711FAB3CBED10DC2BA99A2B B063CEFF8DE1
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec(secretkey, "AES");
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(Integer.valueOf(WalletStaticTypes.GCM_TAG_LENGTH.getType()) * 8,newbyte[12]);
cipher.init(Cipher.ENCRYPT_MODE, keySpec,gcmParameterSpec);
byte[] encryptedPayLoad = cipher.doFinal(jsonPayload.getBytes("UTF-8"));
Hex.encodeHexString(encryptedPayLoad,false);

I noticed that this is about the Apple Pay integration, I managed to achieve the same results from the test documentation using
GCMParameterSpec(128, ByteArray(12))
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, finalKey, GCMParameterSpec(128, ByteArray(12)))
// AES Key
083080D3D0C521C02CD3AE2134363D09EA50DFF914677FAB9E22F18F9C28A3B9
// JSON Payload (67 bytes), UTF-8 encoded: {"Parameter1":"Value1","Parameter2":"Value2","Parameter3":"Value3"}
Output:
E3EF6BA2FFA05B6985FE129E3CB6845C4EA1E94AE98D31A538A4E24906FB720D764D640894CD9DE7CEC00114396651A1CCAEDCF480C57A959E925C04492B9CF85FC711FAB3CBED10DC2BA99A2BB063CEFF8DE1

Related

How to decrypt an AES encrypted string while having only the secret key

I am consuming a SOAP API that sends a response that is encrypted with AES. I too have the secret key from the API provider. However I am a bit confused on how to decrypt the response.
All guides that describe how I can decrypt the message tell me I need SecretKeySpec when using javax.crypto.Cipher. However I have no idea what is actually expected there?
Here is an example what I am trying to do:
final String encryptedResponse = "F9nwhTquiEcRY3wfwCGVH1yvZ1fl28VnBXQ3vo6fyCzdV0MnOmeeHg8ea/7c/9ZT0AeEywnR06r5eUoeq4Swf/bFIixc9JJEYB7/fJ0h6I7blQbiOuks7QOUBoSMNaAum1NYTgTm0MHbM3GYLHDPlb8PkBFTL0XxZalKqcqRuhr3BQxPfITeSXjqSvPvy5Wt1Jq";
final String secretKey = "ijsdfgDJJff42h3412";
BASE64Decoder myDecoder = new BASE64Decoder();
byte[] crypted = myDecoder.decodeBuffer(secretKey);
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] cipherData = cipher.doFinal(crypted);
String decryptedResponse = new String(cipherData);
here I receive the following error
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
The key length should be 16,get the true key first;
mayby the key length should be 16,get the true key first;

What should be the output of the AES cipher?

I am encrypting a string with AES in Java Netbeans using
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.err.println(new String(encrypted));
The result I am getting are special characters. If I encrypt the text "1111"
with a 128 bit key "Bar12345Bar12345" then I am getting the output as []3SU[][][]~a[][]`)
Is this valid?
If not what should be the output then and how can i get the correct output?

RSA AES decryption with junk characters

I am trying to decrypt a saml response using AES and RSA, and I could decrypt the saml assertion properly. But, the decrypted text is being embedded in to some junk characters, which is causing parsing exceptions.
Below is my code
InputStream privateKeyFileInputStream = Check.class.getClassLoader().getResourceAsStream("rsa_privatekey.key");
rsaPrivateKey = new byte[privateKeyFileInputStream.available()];
privateKeyFileInputStream.read(rsaPrivateKey);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
PrivateKey privKey = keyFactory.generatePrivate(privateKeySpec);
Cipher cipher1 = Cipher.getInstance("RSA/NONE/OAEPWithSHA1AndMGF1Padding", "BC");
cipher1.init(Cipher.DECRYPT_MODE, privKey);
byte[] encryptedMessage = Base64.decodeBase64(aesPrivateKeyEnc.getBytes());
aesPrivateKey = cipher1.doFinal(encryptedMessage);
IvParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
SecretKeySpec key = new SecretKeySpec(aesPrivateKey, "AES");
Cipher cipher2 = Cipher.getInstance("AES/CBC/NoPadding", "BC");
cipher2.init(Cipher.DECRYPT_MODE, key, ivSpec);
byte[] cipherTextBytes = Base64.decodeBase64(cipherText);
byte[] decryptedMessage = cipher2.doFinal(cipherTextBytes);
String message = new String(decryptedMessage, "UTF8");
Now, the message has
R����=2�W���?<saml:Assertion ...... </saml:Assertion>��fE]����
It seems that your IV value is prefixed to the ciphertext. Instead of a zero IV you should use the first 16 bytes of your ciphertext for cipher2. Don't forget to exclude them from encryption. This explains the garbage at the start.
It also seems that your cipher2 should be configured for padding. This is probably PKCS#7 padding. Please try "AES/CBC/PKCS5Padding" instead of "/NoPadding". If that doesn't work you'll need to update your question with the plaintext in hexadecimals so we can determine which padding is used. That should explain the garbage at the end.
Note that "PKCS5Padding" does perform PKCS#7 padding in Java.

SAML2.0 Assertion decrypt

I'm trying to integrate an Java Web App with an deploied IdP at my school. The IdP is already configured, I make the request and everything is fine, except one. I can't decrypt the IdP response.
Here you can find the response that I receive after a login
I have a public_key.crt and a private_key.pem.
I already have decrypted the symetric key inside the IdP response, that was encrypted with my public_key.crt. After that I get a 256 bytes array, wich I think is an AES-256 passphrase, but the IdP Response only talks about an AES-128-CBC. Nevertheless I try to create an Java AES object to decrypt the SAML Assertion, but I always get the error: Invalid AES key length: 256 bytes
Here is the code:
PrivateKey privKey = ...; // PrivateKey constructed with my private_key.pem file
System.out.println(privKey); // To show private_key.pem info
// you can find these two strings on IdP Response, from link above
String encAssertion = "5vdtxDD01BUFa438J5EUTebql8Fzf72e39OdBSYd3dcM8RIAdkzKftKhpk5sTU/Ui9swFMbTrL1ptpxUvPa2756yOfqA75eeRdUne939GqYFJUz5ppUY/oHToE06AcGCIxIjG6l8l1a31PwUhkF9LVp9ZK8URF2AGTRvq5R20fcgoIjQBd/tSA6BhQCGx5sa5OEOAGV0blRZlxy9yoPiWn+YdNKCQVtioRrXb5mHL/nFatKmwvoCIw5yVSxcc7BNjlSmUh2cQ86wdRY/ur84k/1D948X5hOJItXAkw70iIVNdE/mwbrjtqssEXHHP46i7YauY27jQ4QSSWXlF93z8mYPAGatUY6q42ZXzrft0tRKg0CjLDTMZfxyCJhZUpjvRx/2y4VxvjBKIOUSwGlhm/BPkt9yV9cge4I3u2DZuqXMP3Gh+cPF3InzA+nTh826owq/cKTNL79h1MSzKK0447nbaBubygcA9E7BOEmwxHvjdySXqGk2m4h1LyRyZjiNq15GphJM1EOjWV6EpC18e7d4FxJjRQ/B5HmXkI/tLJ5BdDxc1Qasqlu7ESIFxmkYKC8YMaBcTLisCe7CVJePg8zUIpPS/pMlm0e/18QW7CHXllGTy63WUZpQtXX2v/rQpz8TdpKkQQUNcAqCD+NDuq3hnWPUEnLVoKstykLkXDY3299k2kXRbsRgvLfCS3v5Gr1BdsbuL4AMfFaaCIktX2JECwzWs+zIWYFbCtZWXeY5iCxo0FF9XSPdXAfan/chGUG38PHkSFt7u+h4Dy3oxA5FsBZnzGSZZFxcPyhcN4s2jan9mP5HWCl70+q7VRaqvhUNIDp9RxmuPV7/gQW1P1KEB7d603pVKKcgg3CMOZ9iip3sBNE6R63pslNO6+PD2rnG6FzKLjRLxB4eWvMgGvDsFKBRjK7RBr2ayDAJkVMEEK4ILVKudNzN0F8TDFdxce5N092WZKxv+0AdVts5tiQ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E7ztevb8Pfc3FARRGIvOGw29ROiO1nW7t662wYQROyIXP2Dcfz0SFlRdxHsF5wzuUj9XNTUaIStbIrj0+1yWxixvk6mqkhthAnbWFIpqHyfNPxe+dC2ipVBsoYwibn44u9sH5TQ/HlwEy7C8=";
String encryptedKey = "qJNLI5UNAVSVwRCI8EHSa5GuhFAxPi9VZleCCrriLHSXWHf79RDG6ocU7slHZaf8zvIc2su41iJsZeb3xXOsg73xbJ2vLOWW5GQCRh97OxFmbd2M0UkYMM57g2YmDlPjTo7L75GrX2LmmL1ko47QmCb60b0um2m6RGNdj32QUIGArUM4TUdamSi0a8+bThe565OUkS25OfrT02z1er1m+VprUa5SPwqLXRqmg9dknCX8pIi5vrD02bRrKP5b+a6FTA77ocOFtYgzPQWPYfAU5jFledHRJA3phwsm7yOFvyjOeWtOVCAKmoYALJ+5rdxnkMGr/SGaI3jjNup7WQJxtw==";
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] decrypt = cipher.doFinal(Base64.decodeBase64(encryptedKey));
System.out.println("Decrypted Key length: " + decrypt.length); // Decrypted Key length: 256
System.out.println(new String(decrypt, "UTF-8")); // Some stupid chars
SecretKey skeySpec = new SecretKeySpec(decrypt, "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// cipher = Cipher.getInstance("AES/CBC/NoPadding");
// cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, (Key) skeySpec);
byte[] decryptedAssertion = cipher.doFinal(encAssertion.getBytes());
The error occurs on the last line.
Invalid AES key length: 256 bytes

AES -256 decryption

I have an encrypted email id for unsubscribing from abcde.test.com.
for ex : https://abcde.test.com/Forms/unSubscribe.jsp?n=2&rid=00028e7353d9c4eca480a579e10ef09b&eid=588876054d458e62779be9345f399252cac7346ad8c464b8ed0bdfbff3512dd96a5b4190c5d71c30c90c34ff39e544aa
This is encrypted in aes-256.where eid="encrypted message" and rid when combined with keysize,and keystr (like "6a6b663472346c38736873346569727538346234333534376635333962353666") forms the encoded key.
Now I want to decrypt this message.
can any one help me decrypting it?
Try the following using Java SE and Apache Commons. Please note that you haven't indicated the mode or padding for your cipher (just "AES"), so you might need to make some adjustments.
// decode the key string into bytes (using Apache Commons)
byte[] keyBytes = Hex.decodeHex(keystr.toCharArray());
// create a representation of the key
SecretKeySpec spec = new SecretKeySpec(keyBytes, "AES");
// turn the key spec into a usable key
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("AES");
SecretKey key = keyFactory.generateSecret(spec);
// use a cipher to decrypt the eid
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] plainText = cipher.doFinal(hex.decodeHex(eid.toCharArray())); // decode from Hex again
I don't know what type eid represents, so turning that into something concrete is up to you, but here's an example:
String eid = new String(plainText, "ASCII");

Categories