I am looking for equivalent C code for below java code. i am trying to write two application one in java and other in C. Java application encrypt/decrypt "string" with below logic, and it is working when using below java method.
public class AEScrypt {
public static String encryptString(String strToEncrypt, String secret, String salt, byte[] iv) {
try {
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec keySpec = new PBEKeySpec(secret.toCharArray(), salt.getBytes(), 65536, 256);
SecretKey secretKeySpec = keyFactory.generateSecret(keySpec);
SecretKeySpec secretKey = new SecretKeySpec(secretKeySpec.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
int length = 0;
if (strToEncrypt.length() <= 16) {
length = 16;
} else if (strToEncrypt.length() > 16 && strToEncrypt.length() <= 32) {
length = 16;
}
strToEncrypt = fixedLengthString(strToEncrypt, length);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8)));
} catch (Exception exception) {
System.out.println("Error while encrypting value : "+exception.getMessage());
}
return null;
}
private static String fixedLengthString(String string, int length) {
return String.format("%" + length + "s", string);
}
public static String decryptString(String strToDecrypt, String secret, String salt, byte[] iv) {
try {
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec keySpec = new PBEKeySpec(secret.toCharArray(), salt.getBytes(), 65536, 256);
SecretKey secretKeySpec = keyFactory.generateSecret(keySpec);
SecretKeySpec secretKey = new SecretKeySpec(secretKeySpec.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))).trim();
} catch (Exception e) {
e.getMessage();
}
return null;
}
}
What i understand from above JAVA code are:
FOR ENCRYPTION:
- We are using HMAC-sha256 for generating "key", which takes "salt", "password".
- Padding input data.
- We are using AES-CBC-256 for Encrypting padded input data, using above generated "key" and "iv".
- We encoding with base64 the encryption data.
FOR DECRYPTION:
- We are using HMAC-sha256 for generating "key", which takes "salt", "password".
- We decoding with base64 and getting encrypted data.
- We are using AES-CBC-256 for Decrypting encrypted data, using above generated key and iv.
- Trim the decrypted data.
For replicating same in C, I used encryption/Decryption method from below link; EVP symmetric Encryption And Decryption
For generating the key i used "PKCS5_PBKDF2_HMAC" and EVP_MD as "EVP_sha256()".
int PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
const unsigned char *salt, int saltlen, int iter,
const EVP_MD *digest,
int keylen, unsigned char *out);
For base64 encoding/decoding: base64 Encoding/decoding
Also i have taken care of padding and trim logic. But I am getting different encrypted data from java and c code. Am i missing something here?
If you have any sample function in C that will be very helpful.