MD5 with RSA in php

2019-03-04 19:34发布

I'm trying to implement digital signature in php as in java sample code below:

            Signature rsaSig = Signature.getInstance("MD5withRSA");
            RSAPrivateKey clientPrivateKey = readPrivateKeyFromFile(fileName);
            rsaSig.initSign(clientPrivateKey);
            String source = msg;
            byte temp[] = source.getBytes();
            rsaSig.update(temp);
            byte sig[] = rsaSig.sign();
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(sig);

My php code :

    $rsa = new Crypt_RSA();
    $rsa->loadKey('...'); // in xml format

    $plaintext = '...';

    $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); 
    $signature = $rsa->sign($plaintext);

But looks like some thing is missing. We should get same signature as java code returns.Can anybody guide me in this?

1条回答
霸刀☆藐视天下
2楼-- · 2019-03-04 20:22

By default phpseclib uses sha1 as the hash. You probably need to do $rsa->setHash('md5').

查看更多
登录 后发表回答