dongyong1400 2015-07-29 17:33
浏览 324

解密(使用PHP)Java加密(PBEWithMD5AndDES)

I need your help. I was asked to decrypt strings via PHP, starting by this java class:

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import com.sun.mail.util.BASE64DecoderStream;

public class Decryptor {

    private Cipher dcipher;

    private static final int iterationCount = 10;

    private static byte[] salt = {
        (byte) 0xB2, (byte) 0x12, (byte) 0xD5, (byte) 0xB2,
        (byte) 0x44, (byte) 0x21, (byte) 0xC3, (byte) 0xC3 };

    Decryptor(String passPhrase) {
        try {
            KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
            SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
            dcipher = Cipher.getInstance(key.getAlgorithm());
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
    }

    public String decrypt(String str) {
        try {
            byte[] dec = BASE64DecoderStream.decode(str.getBytes());
            byte[] utf8 = dcipher.doFinal(dec);
            return new String(utf8, "UTF8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

}

I don't know the Java language so I need some help to understand this encryption.

I tried to use this code: https://github.com/KevinBusse/PBEWithMD5AndDES

But I can't "translate" the salt to PHP string and it doesn't seem to work.

Thank you!

  • 写回答

1条回答 默认 最新

  • douwen9345 2017-08-01 18:30
    关注

    Try something like this, It worked for me.

    Take your salt:

    $bytes = array ( 0xB9,  0x2B, 0xC8,  0x32, 0x56,  0x35 ,  0xE3, 0x03 );
    

    Convert it to String in php :

    $salt = call_user_func_array(
        'sprintf',
        array_merge((array) str_repeat('%c', count($bytes)), $bytes)
    );
    

    Goto this repo: https://github.com/KevinBusse/PBEWithMD5AndDES

    Goto class PkcsKeyGenerator and comment this code $salt = pack('H*', $salt); like this.

    function __construct($keystring, $salt, $iterationsMd5, $segments)
        {
           // $salt = pack('H*', $salt);
            $keyMaterial = '';
            $data = $keystring . $salt;
            $result = '';
            for ($j = 0; $j < $segments; $j++)
            {
                if ($j == 0)
                {
                    $result = $data;
                }
                else
                {
                    $result .= $data;
                }
                for ($i = 0; $i < $iterationsMd5; $i++)
                {
                    $result = md5($result, true);
                }
                $keyMaterial .= $result;
            }
            $this->_key = substr($keyMaterial, 0, 8);
            $this->_iv = substr($keyMaterial, 8, 8);
        }
    

    Now try.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂