How to decrypt AES-256 CBC?

There is a given code ( Java) that encrypts a certain text:

    public String encrypt(String plainText, String password) throws Exception
{
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.update(password.getBytes("UTF-8"));
    byte[] keyBytes = new byte[32];
    System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);

    cipher = Cipher.getInstance("AES/CBC/ZeroBytePadding");
    key = new SecretKeySpec(keyBytes, "AES");
    spec = getIV();

    cipher.init(Cipher.ENCRYPT_MODE, key, spec);
    byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
    String encryptedText = new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");
    return encryptedText;
}
public AlgorithmParameterSpec getIV() //get iv
{
    byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
    IvParameterSpec ivParameterSpec;
    ivParameterSpec = new IvParameterSpec(iv);

    return ivParameterSpec;
}

How can I decrypt the received text on the server using php?

Author: nexus2014su, 2018-05-13

1 answers

To decrypt the text, you need to perform the reverse operations: decode from base64, get an array of bytes, decode the array using a password and an initialization vector (example of lib https://github.com/lt/PHP-AES), and convert the decoded array to a string.

 2
Author: Vadim Bondaruk, 2018-05-13 13:57:24