Verifying a Digital Signature using message digest

Hi, i am new to java.
I have a Digitally signed document, i wanna verify this signed document against the original one.
i got the idea from this link:
http://help.sap.com/saphelp_45b/helpdata/en/8d/517619da7d11d1a5ab0000e835363f/content.htm
i signed a pdf doc with my SmartCard. the third party signing tool passed me the PKCS7 digital signature and i stored it in database. the problem arose when i retrieved this digital signature from DB and verified against the original doc using the message digest method. the base64 result strings are always not equal.
I am sure about this:
-the retrieved digital signature was GOOD.
-the original doc was GOOD.
but why i can't get the same 2 message digests? can somebody please help?
below is part of my code:
while (rsetDs.next())
     InputStream DSName2 = rsetDs.getBinaryStream(1);
     ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
     byte[] myByte = Base64.decode(byteStream.toString());
     ByteArrayInputStream newStream = new ByteArrayInputStream(myByte);
     CertificateFactory cf = CertificateFactory.getInstance("X.509");
     Collection c = cf.generateCertificates(newStream2);
     Iterator i = c.iterator();
     while (i.hasNext())
          Certificate cert = (Certificate)i.next();
          X509Certificate cert1 = (X509Certificate)cert;
          try
               java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
               /*=============DB MD (BEGIN)==================*/
               byte [] pubkeyByte = cert1.getPublicKey().getEncoded();
               md.update(myByte);
               md.update(pubkeyByte);
               byte[] raw = md.digest();
               String db_md = Base64.encode(raw);
               /*============DB MD (end)============*/
               /*=============PDF MD (BEGIN)==================*/
               DataInputStream m_disFile = new DataInputStream(new FileInputStream("C:\\" + "original_doc.pdf"));
               int m_iNum = m_disFile.available();
               byte[] msgBytes = new byte[m_iNum];
               m_iNum = m_disFile.read(msgBytes, 0, m_iNum);
               md.update(msgBytes);
               byte[] digestMd = md.digest();
               md.reset();
               String pdf_md = Base64.encode(digestMd);
               /*=============PDF MD (END)==================*/
..thanks in advance.

PKCS#7 SignedData objects are far more complex then it looks like you are taking them. First the PKCS#7 SignedData object will contain the OID for the message digest algorithm used and for the encryption algorithm used. From the looks of your code you are simply assuming MD5.
It also contains all of the data that was signed which is typically much more than just the document. It also of course contains the public keys and signatures which singed the document. In your case it will probably only have one public certificate and one signature.
Also note that a signature is an encrypted hash. Looking at your code I do not see you use encryption at all or rather for verification decryption.
Here is the basic process a signature takes.
MessageDigest md = MessageDigest.getInstance(algOID);
byte[] digest = md.digest(message.getBytes(charEncoding));
Cipher c = Cipher.getInstance("RSA/2/PKCS1Padding");
c.init(Cipher.ENCRYPT_MODE, priKey);
byte[] signature = c.doFinal(digest);Note that the resulting byte array is not the message digest but the encrypted message digest. You must use the corresponding public key to decrypt the signature to get the message digest value. It is because the trusted public key can decrypt the correct message digest that we know it was encrypted by the holder of the private key. It is because the decrypted message digest value is equal to my computed message digest value that we know the document has not be altered...
Now PKCS#7 SignedData does not take the message digest of the document, in your case your PDF. It creates a message digest on an ASN.1 object which includes the bytes of your document plus a bunch of meta data.
For more info on the exact format of a PKCS#7 signature file check out
http://www.rsasecurity.com/rsalabs/pkcs/pkcs-7/index.html
Look through this doucment for SignedData as a starting place and follow through all of the sub objects that make up a SignedData object. This will give you an idea of what is involved.

Similar Messages

  • Windows cannot verify the digital signature for the drivers required for this device. A recent hardware or software change might have installed a file that is signed incorrectly or damaged, or that might be malicious software from an unknown source. (Code

    I get this message when I check the Device manager for my Ipod
    Windows cannot verify the digital signature for the drivers required for this device. A recent hardware or software change might have installed a file that is signed incorrectly or damaged, or that might be malicious software from an unknown source. (Code 52)
    How do I resolve this I have reinstalled iTunes but it still doesn't recognise my ipod

    I reinstalled Itunes a couple of times.  I unistalled all programs that I never use, I updated all of my drivers, Windows swept my computer and found no problems.  I have a yellow causion lite when I look at the USB-port with the phone connected.  All other devices work without a problem.

  • I am unable to sign with Digital signature using a .PFX file. The button does not appear.

    When I try to sign with a Digital signature using a .PFX file, the button to select the signature does not appear. I have JAVE SE 7 U11 10.11.2.21 platform in my add ons and JAVA Consol 6.0.33 and 6.0.35 in my extentions.
    This problem is not encountered in Crome or IE-8
    ASHISH

    Regarding your first question about bookmarks, I think you discovered the answer in when you pressed the address bar. The second tab there has your bookmarks.
    As for the keyboard, I'm not sure why your Firefox is reacting so slowly; mine seems to show keyboards even when I don't want them. If you have accumulated a lot of history, perhaps that's an issue?
    Did you use any third party software to move your Firefox data from internal memory to the storage card?

  • Windows cannot verify the digital signature for the drivers required for this device. Code 52

    Windows cannot verify the digital signature for the drivers required for this device. A recent hardware or software change might have installed a file that is signed incorrectly or damaged, or that might be malicious software from an unknown source. (Code 52).
    I have reinstalled, uninstalled, the whole 9 yards.

    See the driver topic of:
    iOS: Device not recognized in iTunes for Windows
    I would start with:
    Removing and reinstalling iTunes, QuickTime, and other software components for Windows Vista or Windows 7

  • Windows cannot verify the digital signature for the drivers required for this device. A recent hardw

    windows cannot verify the digital signature for the drivers required for this device. A recent hardware or software change might have installed a file that is singned incorrectly or damaged, or that might be malicious software from unknown source (Code 52)

    Hi,
    What are you trying to do ? To help us answer question quicker, please read this:
       http://h30434.www3.hp.com/t5/First-Time-Here-Learn-How-to/Welcome-Get-started-here/td-p/699035
    Regards.
    BH
    **Click the KUDOS thumb up on the left to say 'Thanks'**
    Make it easier for other people to find solutions by marking a Reply 'Accept as Solution' if it solves your problem.

  • Verifying a Digital Signature

    I have a smart card, which I am trying to use to digitally sign some bytes. Then I want to verify the signature using the public key which I can get from the certificate on the smart card.
    I am able to send some bytes to the card (using an apdu) and get back some RSA signed bytes. But when I try to verify the signature, it always returns false.
    To verify, I do:
    X509Certificate x = ... //gets the certificate from the card
    Signature sig = Signature.getInstance(x.getSigAlgName()); //"SHA1withRSA"
    sig.initVerify(x.getPublicKey());
    sig.update(dataBytes, 0, dataBytes.length);
    boolean isSigned = sig.verify(signed);
    As far as I can tell, this part is good.
    I think my problem is related to how I am sending the bytes to the card to get signed. Sending dataBytes in plaintext returns errors regarding length. It seems the signing command I am using on the card wants the data to equal the key length (data=128 byte, key=1024 bit). So what I did next was an attempt to hash the data and then pad it before I send it to the card.
         MessageDigest md = MessageDigest.getInstance("SHA1");
         md.update(dataBytes);
         byte [] digest = md.digest();
         PKCS1SignaturePadding pkcs1 = new PKCS1SignaturePadding(PKCS1SignaturePadding.SHA1);
         byte [] padded = pkcs1.encode(digest, 128);
    But after I sign, I try to verify the signature but it still returns false. I'm not sure if I'm doing something wrong or if I'm misunderstanding something (I'm relatively new to digital signatures and smart cards).

    PKCS#7 SignedData objects are far more complex then it looks like you are taking them. First the PKCS#7 SignedData object will contain the OID for the message digest algorithm used and for the encryption algorithm used. From the looks of your code you are simply assuming MD5.
    It also contains all of the data that was signed which is typically much more than just the document. It also of course contains the public keys and signatures which singed the document. In your case it will probably only have one public certificate and one signature.
    Also note that a signature is an encrypted hash. Looking at your code I do not see you use encryption at all or rather for verification decryption.
    Here is the basic process a signature takes.
    MessageDigest md = MessageDigest.getInstance(algOID);
    byte[] digest = md.digest(message.getBytes(charEncoding));
    Cipher c = Cipher.getInstance("RSA/2/PKCS1Padding");
    c.init(Cipher.ENCRYPT_MODE, priKey);
    byte[] signature = c.doFinal(digest);Note that the resulting byte array is not the message digest but the encrypted message digest. You must use the corresponding public key to decrypt the signature to get the message digest value. It is because the trusted public key can decrypt the correct message digest that we know it was encrypted by the holder of the private key. It is because the decrypted message digest value is equal to my computed message digest value that we know the document has not be altered...
    Now PKCS#7 SignedData does not take the message digest of the document, in your case your PDF. It creates a message digest on an ASN.1 object which includes the bytes of your document plus a bunch of meta data.
    For more info on the exact format of a PKCS#7 signature file check out
    http://www.rsasecurity.com/rsalabs/pkcs/pkcs-7/index.html
    Look through this doucment for SignedData as a starting place and follow through all of the sub objects that make up a SignedData object. This will give you an idea of what is involved.

  • How do I do a digital signature using the inbuilt camera

    I am using a MacBook Pro 13" and today I was shown in PC world how to use a digital signature by using the inbuilt camera. Is this possible with this model or is it only with the Retina models?

    You may find this article helpful.
    http://9to5mac.com/2014/02/15/how-to-use-preview-to-put-signatures-on-pdfs-pages -documents-and-mail-messages/
    Click the blue Reader button at the end of Safari address bar for easy viewing.

  • Adobe Form Digital Signature using WDA

    Hi,
    I have implemented Interactive Adobe Form using WDA, with one Signature Field.
    I need to set and verify the status of the Digital Signature.
    1) Is it possible to sign the Adobe form without the user selecting the Digital Id, basically what is needed to implement automatic signatures (setting and verifying)?
    2) Once the data is filled online, the form is to be distributed or downloaded. How to apply security parameters to Form in this case (like pass protected etc). From SAP documentation, I understand we need to use Adobe Policy Server. Can we install this server on the Java stack of SAP WAS. Are there any other alternatives to implement such features?
    Thanks & Regards,
    Chitrali Shah

    I realize there is quite a bit of crossover between the two topics, but becuase you want to know about digital signature and specifically offline security; this question would probably be better asked in the Adobe Interactive Forms forum:
    SAP Interactive Forms by Adobe
    However I'm not going to lock the thread, as the WDA forum you will find many Forms experts as well and might find a solution.  I just think you might have better luck in the other forum. This is perhas a rare case where double posting would be appropriate as long as you go back and update both threads with any solutions.
    It sounds like you may have already found it, but about all I can offer on the subject is the link to the latest version of the online help on the subject:
    http://help.sap.com/saphelp_nw70ehp1/helpdata/en/48/a0677efb696bb9e10000000a42189d/frameset.htm

  • Digital signature with messaging server 6

    Hi all,
    I want to implement the following with my messaging server or even with the communication express.
    Just as outlook, where users can use a digital signature; Is this possible to be done in the messaging/communication express.
    If yes, can you please provide me with the procedure?
    Regards,
    Scotty

    There is full documentation for how to set up and use "S/MIME" in CE.
    http://docs.sun.com/app/docs/doc/819-2650/6n4u4dtul?q=S%2FMIME&a=view

  • Digital Signature error message

    I am using Windows 7 64bit, Adobe Acrobat XI and have imported my personal digital signature certificate as well as the certificate from the signing authority. They both show in preferences, but mine has a yellow triangle with an exclamation point before it. When I try to sign a pdf document I get the following error message: "You do not have any digital IDs suitable for signing this document". Anyone know what the missing pieces are?  Thanks

    Investigating the issue there is indeed a “signing private key lifetime” that Adobe seems to be using. This is a setting on our vendor site and is set to 70% of the Public Key Lifetime (which is 5 years). As a result – despite the fact the key can be used to sign messages it would appear that signing documents is set to 3 years and 6 months by virtue of this setting.

  • Digital Signature using acrobat x

    I've made a form and included a field for a digital signature.  I saved it as user enabled but it still won't let someone using reader only sign the document.   I need help with this.

    Seriously?  Wow.  Ok.  Thanks.  This is the product that was purchased for me through work so I'm stuck I guess.

  • Digital Signatures using JSONP across Domains

    Currently using Apex 3.2.1 through OHS. Not that that really matters in this instance, it would probably work the same in Apex 4.
    I have working code to digitally sign an apex web form. The user presses a "Digitally Sign" button on the page, and an ondemand process runs the pl/sql package to read in the required CGI variables and create a signature string (similar to what you may see on a digitally signed PDF file), then saves that string to the database to mark that form as being digitally signed.
    However, the web server that is running this code must be setup to require user certificates in order for this to work. Instead, what I would like to do is make a call to our login server which is already setup to require user certificates, and get the digital signature from there instead.
    So far, I have created a mod_plsql call on the login server which I can access via a URL to return the signature, but what I don't know how to do is call that URL from an Apex page on the normal server? From my research, I'm thinking this is going to need to use JSONP because it would require cross domain communication. Am I going in the right direction here? I don't know a thing about JSON, and I'm not sure where to start.
    Can anyone provide any guidance?
    Thanks,
    Kris

    Anybody have any guidance on how to handle this?
    Thanks!

  • How to verify the digital signatures?

    Buyer has to send a certified interactive form to vendor, vendor has to digitally sign the form back. We need to verify the signature of the vendor only then start processing the form. How do I achiev this security. 
    1) How do I generate buyer certificate and attach to the form.
    2) I have created a signature field in the form, after the vendor signs it how do we check whether its the correct signature?
    Kindly specify the steps.
    Helpful answers will be rewarded.
    Regards,
    Vishal

    Hi Vishal,
    Check this Thread.
    Implement Digital Signature in Interactive form
    Regards,
    Mithu

  • Unable to add digital signatures using Adobe LCES Digital Security

    I have tried running the Sample Java code to add digital signature fields and add signatures. I am getting errors. The log file shows:
    com/adobe/idp/Context
    Jan 2, 2009 4:00:14 PM com.adobe.livecycle.signatures.common.CommonBaseException logException
    SEVERE: ALC-DSS-300-000 Generic SignaturesBaseException (in the operation : addSignatureField)
    Caused By: com/adobe/idp/Context(EjbMessageDispatcher.java163)
    Caused By: com/adobe/idp/Context(Class.java-2)
    ALC-DSS-300-000 Generic SignaturesBaseException (in the operation : addSignatureField)
    Caused By: com/adobe/idp/Context(EjbMessageDispatcher.java163)
    Caused By: com/adobe/idp/Context(Class.java-2)
    com.adobe.livecycle.signatures.client.SignatureServiceClient.addSignatureField(SignatureSe rviceClient.java:342)
    apple.AddSignatureField.main(AddSignatureField.java:53)
    Caused By: com.adobe.idp.dsc.DSCException
    com.adobe.idp.dsc.provider.impl.ejb.EjbMessageDispatcher.doSend(EjbMessageDispatcher.java: 163)
    com.adobe.idp.dsc.provider.impl.base.AbstractMessageDispatcher.send(AbstractMessageDispatc her.java:57)
    com.adobe.idp.dsc.clientsdk.ServiceClient.invoke(ServiceClient.java:208)
    com.adobe.livecycle.signatures.client.SignatureServiceClient.addSignatureField(SignatureSe rviceClient.java:324)
    apple.AddSignatureField.main(AddSignatureField.java:53)
    Has anyone worked with the JAVA API to add digital signatures on to a PDF file using Adobe LiveCycle ES 8.2? If yes please let me know.
    - Ragha

    This was traced back to one of the ini files still having a folder name from the last instance installed (i.e. SERVER6).
    So it is fixed

  • Add Digital Signature Using C# and Acrobat SDK

    Hi everybody!
    Please, how can I digitally sign PDF documents using Acrobat Professional 8 API and C# language?
    I know that I need use JavaScript APIs (IAC) but I can't find anything really helpful in the Acrobat SDK Documentation. Could anyone post a sample of how use javascript manipulation inside C# (Framework 2.0), or give me a direction?
    Thank in advance!

    Hi Felipe,
    when i am signing the document.
    I believe i am missing something in my code. After executing the code to sign, when i open it i get "The following signature fields are not signed".
    Please let me know where i m doing wrong?
    Here is my VB.NET Code
    Dim gapp As Acrobat.CAcroApp
    Dim gpddoc As Acrobat.CAcroPDDoc
    Dim jso As Object
    gapp = CreateObject("acroexch.app")
    gpddoc = CreateObject("acroexch.pddoc")
    If gpddoc.Open("C:\Test1.pdf") Then
    jso = gpddoc.GetJSObject()
    jso.SetUserPassword("'testpassword12'")
    'jso.ShowMyMessage("SetUserDigitalIDPath")
    jso.SetUserDigitalIDPath("'c:\\DrTest.pfx'")
    jso.app.execMenuItem("ADBESDK:AddSignature")
    jso.AddSignature(jso)
    gapp.Show()
    and here is the javascript
    // password to use the digital signature
    var sigUserPwd = "UNKNOWN";
    // to test the sample without user input, specify:
    // var sigUserPwd = "testpassword";
    // path to the digital signature file
    var sigDigitalIDPath = "UNKNOWN";
    // to test the sample without user input, specify:
    //var sigDigitalIDPath = "/C/DrTest.pfx";
    // other variables the user can modify
    var sigHandlerName = "Adobe.PPKLite";
    var sigFieldname = "sdkSignatureTest";
    var sigReason = "I want to test my digital signature program.";
    var sigLocation = "San Jose, CA";
    var sigContactInfo = "[email protected]";
    /* Add a menu item for AddSignature */
    app.addMenuItem( { cName: "ADBESDK:AddSignature", cUser: "Add My Signature", cParent: "Advanced",
    cEnable: "event.rc = (event.target != null);",
    cExec: "AddSignature(event.target)" });
    // main function
    AddSignature=app.trustedFunction(function (doc)
    app.beginPriv(); // explicitly raise privilege
    // if sigDigitalIDPath is not spcified, ask for user input
    if(sigDigitalIDPath == "UNKNOWN"){
    var cResponse = app.response({
    cQuestion: "Input your digital ID path:",
    cTitle: "Digital Signature",
    cDefault: "/C/DrTest.pfx",
    if ( cResponse == null) {
    app.alert("No input.");
    return;
    else
    SetUserDigitalIDPath(cResponse);
    // if sigUserPwd is not spcified, ask for user input
    if(sigUserPwd == "UNKNOWN"){
    var cResponse = app.response({
    cQuestion: "Input your password:",
    cTitle: "Digital Signature",
    cDefault: "testpassword",
    if ( cResponse == null) {
    app.alert("No input.");
    return
    else
    SetUserPassword(cResponse);
    // create a new signature field
    var signatureField = AddSignatureField(doc);
    // sign it
    if(signatureField) Sign(signatureField, sigHandlerName);
    app.endPriv();
    // create a signature field in the upper left conner with name of sigFieldname
    function AddSignatureField(doc)
    var inch=72;
    var aRect = doc.getPageBox( {nPage: 0} );
    aRect[0] += 0.5*inch; // from upper left hand corner of page.
    aRect[2] = aRect[0]+2*inch; // Make it 2 inch wide
    aRect[1] -= 0.5*inch;
    aRect[3] = aRect[1] - 0.5*inch; // and 0.5 inch high
    var sigField = null;
    try {
    sigField = doc.addField(sigFieldname, "signature", 0, aRect );
    } catch (e) {
    console.println("An error occurred: " + e);
    return sigField;
    // define the Sign function as a privileged function
    Sign = app.trustedFunction (
    function( sigField, DigSigHandlerName )
    try {
    app.beginPriv();
    var myEngine = security.getHandler(DigSigHandlerName);
    myEngine.login( sigUserPwd, sigDigitalIDPath);
    sigField.signatureSign({oSig: myEngine,
    bUI: false,
    oInfo: { password: sigUserPwd,
    reason: sigReason,
    location: sigLocation,
    contactInfo: sigContactInfo}
    app.endPriv
    } catch (e) {
    console.println("An error occurred: " + e);
    // set a correct password for using the signature, so you can quietly sign a doc.
    function SetUserPassword(pwd)
    sigUserPwd = pwd;
    // set path to the digital signature file
    function SetUserDigitalIDPath(idPath)
    sigDigitalIDPath = idPath;
    Sumit

Maybe you are looking for

  • N8 Music Player always showing songs instead of ju...

    Hi there sorry if this has been repeated but couldnt find it had in the forum. I have the problem of when i move my music albums to the N8 music player it always show the songs even when i choose albums view??? How can i make it just show the album s

  • [Solved] no non-ASCII character input in rxvt-unicode

    Hello everyone, For some days now, I can't write any non-ASCII characters any more in rxvt-unicode and rxvt-unicode-patched. Unfortunately, downgrading the rxvt-unicode package doesn't seem to help. To have at least a temporary solution, I'd like to

  • IMovie 08 Fails to Launch

    Have had iLife 08 since it came out. Everything was working. Been using iMovie 06 because of '08s limitations. Went to launch '08 and it refuses to launch. Same thing happens with all the other i'Life programs. The programs open briefly, their icon a

  • What is the relationship between CAF & GP

    hi Experts, I am new to the CAF and GP. Please provide me the following details about the CAF and GP. what is the relation ship between CAF & GP? Is GP is the one of the component of the CAF? What is the GP? Please give me one business scenorio we ca

  • Will Analog Instruments ADXL330 triaxial accelerometer work with the SCXI-1531 input module?

    I have an SCXI-1001 chassis and will soon be purchasing an accelerometer input module (SCXI-1531).  I'm also investigating possible accelerometers to buy.  Does anyone know if the the Analog Instruments ADXL330 accelerometer can be made to work with