IllegalArgumentException: IV buffer too short for given offset/length combination

大城市里の小女人 提交于 2020-04-17 12:36:35

问题


I have one application which is in PHP encrypting text using openssl_encrypt with following method. (Using same value for salt and iv as '239422ae7940144f')

function encrypt_password($password) {
    define('AES_256_CBC', 'aes-256-cbc');
    $sessionId = $password;
    //random number for encrtyption(salt)
    $salt = '239422ae7940144f';
    $iv = $salt; //cipher length
    $encryptedSession = openssl_encrypt($sessionId, AES_256_CBC, $salt, 0, $iv);
    return array('encryptedPassword' => $encryptedSession, 'salt' => $salt);
}

function decrypt_password($result) {
    define('AES_256_CBC', 'aes-256-cbc');
    $vPassword = 'xUP9PwhcXm5xbKIfiSxMCA==';
    //random number for descrypt(salt)
    $salt = '239422ae7940144f';
    $iv = $salt; //cipher length.
    $decrypted = openssl_decrypt($vPassword, AES_256_CBC, $salt, 0, $iv);
    return $decrypted;
}

Encrypt of password 'abc123' provides 'xUP9PwhcXm5xbKIfiSxMCA==' and decrypting it gives back 'abc123'.

How to find equivalent java program which would do the same. I tried using the example on Using Java to decrypt openssl aes-256-cbc using provided key and iv, but it fails with

java.lang.IllegalArgumentException: IV buffer too short for given offset/length combination.

Following are the secretKey and initVector lines in java program I am using.

    final byte[] secretKey = javax.xml.bind.DatatypeConverter.parseHexBinary("239422ae7940144f");
    final byte[] initVector = javax.xml.bind.DatatypeConverter.parseHexBinary("239422ae7940144f");

来源:https://stackoverflow.com/questions/38603153/illegalargumentexception-iv-buffer-too-short-for-given-offset-length-combinatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!