sign the large message and verify with recover message

℡╲_俬逩灬. 提交于 2019-12-23 05:36:05

问题


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.

来源:https://stackoverflow.com/questions/11482155/sign-the-large-message-and-verify-with-recover-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!