I have implemented the following AES Encryption in C# code. Which I can't change even if I want to.
public string Encrypt(string InputForEncryption, string Password, string IV)
{
IBuffer buffInputForEncryption = CryptographicBuffer.ConvertStringToBinary(InputForEncryption, BinaryStringEncoding.Utf8);
IBuffer buffPassword = CryptographicBuffer.ConvertStringToBinary(Password, BinaryStringEncoding.Utf8);
IBuffer buffIV = CryptographicBuffer.ConvertStringToBinary(IV, BinaryStringEncoding.Utf8);
KeyDerivationAlgorithmProvider DerAlgo = KeyDerivationAlgorithmProvider.OpenAlgorithm("PBKDF2_SHA1");
KeyDerivationParameters KeyPara = KeyDerivationParameters.BuildForPbkdf2(buffIV, 1024);
CryptographicKey KeyPassword = DerAlgo.CreateKey(buffPassword);
IBuffer buffMatPassword = CryptographicEngine.DeriveKeyMaterial(KeyPassword, KeyPara, 32);
CryptographicKey KeyIV = DerAlgo.CreateKey(buffPassword);
IBuffer buffMatIV = CryptographicEngine.DeriveKeyMaterial(KeyIV, KeyPara, 16);
SymmetricKeyAlgorithmProvider SymAlgo = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");
CryptographicKey KeySym = SymAlgo.CreateSymmetricKey(buffMatPassword);
IBuffer buffRESULT = CryptographicEngine.Encrypt(KeySym, buffInputForEncryption, buffMatIV);
string Result = CryptographicBuffer.EncodeToBase64String(buffRESULT);
return Result;
}
Following code is for Java in Android
private Cipher cipher;
private SecretKey secretKey;
private IvParameterSpec ivParameterSpec;
int iterationCount = 1024;
int keyStrength = 128;
private String sampleInputForPassSaltIV = "Abcd1234Abcd1234";
private String encryptInput = "helloAES";
private String encryptedOutput = "";
private String decryptedOutput = "";
public Boolean initializeEncryption() throws Exception {
String secretKeyAlgorithm = "PBKDF2WithHmacSHA1";
SecretKeyFactory secretKeyFactory;
KeySpec keySpec;
SecretKey secretKeyTemp;
String passPhrase = sampleInputForPassSaltIV;
String keySalt = sampleInputForPassSaltIV;
secretKeyFactory = SecretKeyFactory.getInstance(secretKeyAlgorithm);
keySpec = new PBEKeySpec(passPhrase.toCharArray(), keySalt.getBytes(), iterationCount, keyStrength);
secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES");
byte[] IV = sampleInputForPassSaltIV.getBytes();
ivParameterSpec = new IvParameterSpec(IV);
cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
return true;
}
private void encrypt(String dataToEncrypt) throws Exception {
if (dataToEncrypt.length() > 0) {
byte[] UTF8Data;
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
UTF8Data = cipher.doFinal(dataToEncrypt.getBytes());
encryptedOutput = Base64.encodeToString(UTF8Data, 0);
Toast toast = Toast.makeText(context, "Encrypted Text : " + encryptedOutput, Toast.LENGTH_LONG);
toast.show();
}
}
I have tried everything I could to get same output from both code but I couldn't find the solution. I have tried changing KeyLength, BlockSize, check for encoding and decoding of strings but NO... I can't change C# code since its already been used a lot so please somebody help me for this java code what should I do to get both outputs same.
I have already tried many solutions from threads on StackOverflow.
I think it must be padding issue or key size.
For testing only I am using same pass and same salt.
Please Help...