I have a sign in API in which provide a secret key in decrypt form like below to further used in different types of APIs for data fetching. I have to decrypt it for use case.
“secretAccessKey”: { “ciphertext”: { “words”: [ -11211163938, 15677252277, 14825332397, 8644425824, 5827551513, -14516662867, 10680784880, 9296565512, 435337374, 2609999864, -19100081560, 453333329 ], “sigBytes”: 48 }, “key”: { “words”: [ 15778663618, 14227885834, 18916051559, 14199998468, 16285585456, 16313393861, 20211151033, 20513332385, -16025004046, -1008020031, 8954886893, 5647776294 ], “sigBytes”: 32 }, “iv”: { “words”: [ -16025454446, -1008737731, 8954444593, 5644223294 ], “sigBytes”: 16 }, “salt”: { “words”: [ -7514324436, 9903098141 ], “sigBytes”: 8 }, “blockSize”: 4 },
I have tried below decryptor class available on internet but did not get correct result.
public class Decryptor {
public ResponseData parseResponseData(String jsonString) {
Gson gson = new Gson();
return gson.fromJson(jsonString, ResponseData.class);
}
public String decrypt(EncryptionKeys encryptionKeys) throws Exception {
byte[] ciphertextBytes = intArrayToByteArray(encryptionKeys.ciphertext.words);
byte[] keyBytes = intArrayToByteArray(encryptionKeys.key.words);
byte[] ivBytes = intArrayToByteArray(encryptionKeys.iv.words);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] decryptedBytes = cipher.doFinal(ciphertextBytes);
return new String(decryptedBytes);
}
private byte[] intArrayToByteArray(int[] intArray) {
byte[] byteArray = new byte[intArray.length * 4];
for (int i = 0; i < intArray.length; i++) {
byteArray[i * 4] = (byte) ((intArray[i] >> 24) & 0xFF);
byteArray[i * 4 + 1] = (byte) ((intArray[i] >> 16) & 0xFF);
byteArray[i * 4 + 2] = (byte) ((intArray[i] >> 8) & 0xFF);
byteArray[i * 4 + 3] = (byte) (intArray[i] & 0xFF);
}
return byteArray;
}
}