travist / jsencrypt

A zero-dependency Javascript library to perform OpenSSL RSA Encryption, Decryption, and Key Generation.
http://www.travistidwell.com/jsencrypt
Other
6.69k stars 2.02k forks source link

How should Java verify your generated signature #308

Open goodtan opened 7 months ago

goodtan commented 7 months ago

I used your plug-in in my front-end development code to generate signatures and unsign them. However, in Java, the uncheck fails and the data is not correct when converting the byte array and the signature front-end code generated by Java can also be unsigned, so how should Java adjust。 const signWithPrivateKey = (privateKey, data) => { const encrypt = new JSEncrypt(); encrypt.setPrivateKey(privateKey) ; return encrypt.sign(data,CryptoJS.SHA1); };

// 使用公钥进行验证签名
const verifyWithPublicKey = (publicKey, data, signature) => {
  const encrypt = new JSEncrypt();
  encrypt.setPublicKey(publicKey);
  return encrypt.verify(data, signature, CryptoJS.SHA1);
};  Java code:@Override
protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
    if (publicKey == null) {
        throw new SignatureException("Missing public key");
    }
    try {
        if (sigBytes.length != RSACore.getByteLength(publicKey)) {
            throw new SignatureException("Signature length not correct: got " +
                sigBytes.length + " but was expecting " +
                RSACore.getByteLength(publicKey));
        }
        byte[] digest = getDigestValue();
        byte[] decrypted = RSACore.rsa(sigBytes, publicKey);
        byte[] unpadded = padding.unpad(decrypted);
        byte[] decodedDigest = decodeSignature(digestOID, unpadded);
        return MessageDigest.isEqual(digest, decodedDigest);
    } catch (javax.crypto.BadPaddingException e) {
        // occurs if the app has used the wrong RSA public key
        // or if sigBytes is invalid
        // return false rather than propagating the exception for
        // compatibility/ease of use
        return false;
    } catch (IOException e) {
        throw new SignatureException("Signature encoding error", e);
    } finally {
        resetDigest();
    }
} 
goodtan commented 7 months ago

help help help !!!!