sign the large message and verify with recover mes

2019-06-09 22:45发布

问题:

I write the following code for signing the message and then verify it, in java by Bouncy Castle.

signing work properly but verifying don't work completely. Verifying with short input message work properly but with large input message (larger than 106) don't work properly.

In other words when the length of input text for sign becomes large, the verify can not do complete. the result of code print:

signature tampered

and return partial of the primary message.

public static String sigVer(PublicKey pu, PrivateKey pr, String original) throws Exception{
    //sign
    BigInteger big = ((RSAKey) pu).getModulus();
    byte[] text = original.getBytes();
    RSAKeyParameters rsaPriv = new RSAKeyParameters(true, big,((RSAPrivateKey) pr).getPrivateExponent());
    RSAKeyParameters rsaPublic = new RSAKeyParameters(false, big,((RSAPublicKey) pu).getPublicExponent());
    RSAEngine rsa = new RSAEngine();
    byte[] data;
    Digest dig = new SHA1Digest();
    ISO9796d2Signer eng = new ISO9796d2Signer(rsa, dig, true);
    eng.init(true, rsaPriv);
    eng.update(text[0]);
    eng.update(text, 1, text.length - 1);
    data = eng.generateSignature();

    //verify
    eng = new ISO9796d2Signer(rsa, dig, true);
    eng.init(false, rsaPublic);
    if (!eng.verifySignature(data)) {
        System.out.println("signature tampered");
    }
    try{
        if (eng.hasFullMessage()) {
            eng.updateWithRecoveredMessage(data);
            System.out.println("full");
        }
            eng.updateWithRecoveredMessage(data);
            byte[] message = eng.getRecoveredMessage();
            String ss = new String(message);
            return ss;
    }
    catch (Exception e) {
        System.out.println("can not recover");
        return null;
    }
}

why eng.hasFullMessage() function return false and How should this problem be solved? or can any one suggest another solution to take large input without problem?

thanks all.