Skip to content

How should Java verify your generated signature #308

Open
@goodtan

Description

@goodtan

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();
    }
} 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions