dsafew1231 2017-11-06 05:34
浏览 377

使用相同密钥的AES加密在Java和PHP中生成不同的值

In PHP, I am unable to decrypt a string encrypted in java using AES CBC 128 bit. I have tried bott phpseclib and openssl on the PHP side. Though my preference is for phpseclib.

Java Side

  1. I generated an AES 128 bit key using:

    keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(128);
    secretKey = keyGen.generateKey();
    
  2. Generated an initialization vector by getting bytes from a string:

    byte[] iv = "myuniqueivparam".getBytes("UTF-8");
    
  3. Encrypted a string as follows:

    Cipher enCipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    

    // the key and iv are passed on to php as strings

PHP side

  1. Set the algorithm:

    $aes = new Crypt_AES(CRYPT_AES_MODE_CBC);
    
  2. The key and iv I got from java are passed as strings:

    $aes->setKey($userKey); 
    $aes->setKeyLength(128);
    // when I echo this I get myuniqueivparam
    $ivdecode=base64_decode($iv); 
    $aes->setIV($ivdecode);
    
  3. Set the same input and add padding

    $input="winning";
    $blocksize = 16;
    $pad1=$blocksize -(strlen($input) %$blocksize);
    $input1=$input . str_repeat(chr($pad1), $pad1);
    
  4. Encrypted the string using phpseclib:

    $aes = new Crypt_AES(CRYPT_AES_MODE_CBC);
    $dummy = base64_encode($aes->encrypt($input1));
    echo $dummy;
    
  5. $decryptedstring=$aes->decrypt($encryptedStringInJava);

Issues

  1. When I print $dummy, in step 4, the encrypted string that I get does not match the encrypted string from java.

  2. Decrypting $encryptedStringInJava is not working in PHP (step 5)

I have tried encryption/decryption in java using both phpseclib and openssl.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料