Convert this Java Snippet to PHP

robdog

Well-known member
Java:
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

/**
 * Method to decrypt encrypted String text using secret password
 * @param password, secret key to decrypt the payload string
 * @param payloadJsonString, Student marking error/success details json in String format
 * @return decrypted payload json in String format
 * @throws Exception
 */
public static String decryptText(String password, String  payloadjsonstring JsonString) throws Exception {
    //Extract base64 ivParams
    String ivParamsAsBase64 = payloadJsonString.substring(0, 24);
    //Extract base64 salt
    String saltAsBase64 = payloadJsonString.substring(24, 52);
    //Extract base64 text (student marking success/error details)
    String cipherTextAsBase64 = payloadJsonString.substring(52);

    //convert salt from base64 format to byte array
    byte[] salt = Base64.decodeBase64(saltAsBase64);
    //convert password key from string to byte array
    byte[] pass = password.getBytes();

    //construct secret key for text decryption
    byte[] key = new byte[salt.length + pass.length];
    System.arraycopy(pass, 0, key, 0, pass.length);
    System.arraycopy(salt, 0, key, pass.length, salt.length);
    byte[] secretKey = Arrays.copyOf(MessageDigest.getInstance("SHA-256").digest(key), 16);
    SecretKeySpec skeySpec = new SecretKeySpec(secretKey, "AES");
        
        
    //convert ivParams from base64 format to byte array
    byte[] ivParams = Base64.decodeBase64(ivParamsAsBase64);
    //create IvParameterSpec object using ivParams byte array
    IvParameterSpec ivParameterSpec = new IvParameterSpec(ivParams);
    
    //create and initialise Cipher object
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
    
    //convert cipher text from base64 format to byte array
    byte[] cipherText = Base64.decodeBase64(cipherTextAsBase64);
    
    //decrypt text and return output string
    byte[] dycryptedTextByte = cipher.doFinal(cipherText);
    final String dycryptedTextString = new String(dycryptedTextByte);

    return dycryptedTextString;
}

Let me know your price to get this converted. Thank you.
 
There is no such software for transforming from Java to PHP yet. I suppose you need to find someone who will spent some time to convert your code piece by piece in PHP.
Goog luck.
 
There is no such software for transforming from Java to PHP yet. I suppose you need to find someone who will spent some time to convert your code piece by piece in PHP.
Goog luck.

Right, that is the ask. Hopefully someone can see what this is doing in Java, and recreate in PHP.
 
  • Like
Reactions: eL_
Did you find someone for that ?

Do you have test vectors to check the ported code works ? Basically a set of password/payloadjsonstring.
 
Can you please give examples of parameters to test the function?



Although I don't know Java, it turned out that for the code to at least compile, it should look like this:
Java:
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import java.util.Arrays;

public static String decryptText(String password, String  payloadJsonString) throws Exception {
    //Extract base64 ivParams
    String ivParamsAsBase64 = payloadJsonString.substring(0, 24);
    //Extract base64 salt
    String saltAsBase64 = payloadJsonString.substring(24, 52);
    //Extract base64 text (student marking success/error details)
    String cipherTextAsBase64 = payloadJsonString.substring(52);

    //convert salt from base64 format to byte array
    byte[] salt = Base64.decodeBase64(saltAsBase64);
    //convert password key from string to byte array
    byte[] pass = password.getBytes();

    //construct secret key for text decryption
    byte[] key = new byte[salt.length + pass.length];
    System.arraycopy(pass, 0, key, 0, pass.length);
    System.arraycopy(salt, 0, key, pass.length, salt.length);
    byte[] secretKey = Arrays.copyOf(MessageDigest.getInstance("SHA-256").digest(key), 16);
    SecretKeySpec skeySpec = new SecretKeySpec(secretKey, "AES");


    //convert ivParams from base64 format to byte array
    byte[] ivParams = Base64.decodeBase64(ivParamsAsBase64);
    //create IvParameterSpec object using ivParams byte array
    IvParameterSpec ivParameterSpec = new IvParameterSpec(ivParams);

    //create and initialise Cipher object
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);

    //convert cipher text from base64 format to byte array
    byte[] cipherText = Base64.decodeBase64(cipherTextAsBase64);

    //decrypt text and return output string
    byte[] dycryptedTextByte = cipher.doFinal(cipherText);
    final String dycryptedTextString = new String(dycryptedTextByte);

    return dycryptedTextString;
}
 
Last edited:
Top Bottom