I have been given sample code in Java and I have to write a code in PHP. Output of PHP Code is not matching with Java Sample Output given. The sample Java code may not be complete (it is what is given in the API documentation given to us. But in essential it summarizes the steps that we need to complete)
Message to encrypt is 4NPMCXGERGXSBI7F9IPM5000004NPMCXGERGXSBI7F9IPM
JAVA Code
import javax.crypto.Cipher;</br>
import javax.crypto.spec.SecretKeySpec;</br>
import javax.crypto.spec.IvParameterSpec;</br>
import io.fabric.sdk.android.services.network.HttpRequest;</br>
class AAAA{ // class can have any name</br>
public static String getEncryptedTextFor(String text)</br>
throws Exception {</br>
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");</br>
byte[] keyBytes = new byte[16];</br>
String key = "YASHSHAH!@#987MOBAPP";</br>
byte[] b = key.getBytes("UTF-8");</br>
int len = b.length;</br>
if (len > keyBytes.length) len = keyBytes.length;</br>
System.arraycopy(b, 0, keyBytes, 0, len);</br>
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");</br>
IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);</br>
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);</br>
byte[] results = cipher.doFinal(text.getBytes("UTF-8"));</br>
return HttpRequest.Base64.encodeBytes(results);</br>
}</br>
}</br>
PHP Code that we tried
$keyBytes = [];
$reqArrLen = 16;
$secretKey = 'YASHSHAH!@#987MOBAPP';
$a = utf8_encode($secretKey);
$b = unpack('C*', $a);
$len = count($b);
if($len > $reqArrLen){
$len = $reqArrLen;
}
foreach ($b as $key => $value) {
if($key <= $len)
$keyBytes[]=$value;
}
echo '<pre>';
$strtobyt = implode(array_map("chr", $keyBytes));
$enLoan_Amount = AesCipher::encrypt($strtobyt, $eLoan_Amount);
Sample Output FORMAT in Java is : (This is not the actual output but format is shared) pnehvlPu2gKJXJabR+HbMERdHWnrdc713/SAsHOFWDQ9kv/eWNFI4IRmYWQ6QcoH
I'm not able to get this output format after executing PHP code.
Sample PHP Output :
2438bf468c0142c82127d1dd740f53c3c19125f724265d58811322b50027a876a05b35f61a767fced426597d09f592f3
PHP output length is 128 whereas Java is in 64.