Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How should Java verify your generated signature #308

Open
goodtan opened this issue Mar 27, 2024 · 1 comment
Open

How should Java verify your generated signature #308

goodtan opened this issue Mar 27, 2024 · 1 comment

Comments

@goodtan
Copy link

goodtan commented Mar 27, 2024

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
Copy link
Author

goodtan commented Mar 27, 2024

help help help !!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant