How to digitally sign the message using digital certificate and BaseEnc

Discussion in 'Java' started by suresh123kiran, Aug 19, 2012.

  1. From: "suresh123kiran" <suresh123kiran@1:261/38.remove-7rm-this>

    From: "suresh123kiran" <suresh123kiran@1:261/38.remove-rj6-this>

    From: "suresh123kiran" <suresh123kiran@1:261/38.remove-wnx-this>

    From: suresh123kiran <>

    Hi All ... I am working on webservices.... I need to digitally sign a message
    in which AcknowledgementNo using digital Certificate with X.509 certificate
    private key. I tried this code but it showing invalid signature Encoding. Can
    anyone pls us to digitally sign.
    Here is the below Java code to sign the AcknowledgementNo,Pls help me. Thanks
    in advance.


    public class DigiAckNo {
    static class PrivateKeyAndCertChain {
    public PrivateKey mPrivateKey;
    }

    private static KeyStore loadKeyStoreFromPFXFile(String aFileName,
    String aKeyStorePasswd) throws
    GeneralSecurityException,
    IOException {
    String PKCS12_KEYSTORE_TYPE = "PKCS12";
    KeyStore keyStore = KeyStore.getInstance(PKCS12_KEYSTORE_TYPE);
    FileInputStream keyStoreStream = new
    FileInputStream(aFileName);
    char[] password = aKeyStorePasswd.toCharArray();
    keyStore.load(keyStoreStream, password);
    return keyStore;
    }


    private static PrivateKeyAndCertChain getPrivateKeyAndCertChain(
    KeyStore aKeyStore, String aKeyPassword)
    throws GeneralSecurityException {
    char[] password = aKeyPassword.toCharArray();
    Enumeration<String> aliasesEnum = aKeyStore.aliases();
    if (aliasesEnum.hasMoreElements()) {
    String alias = (String) aliasesEnum.nextElement();
    Certificate[] certificationChain = aKeyStore
    .getCertificateChain(alias);
    Certificate certificate509 =
    aKeyStore.getCertificate(alias);

    PrivateKey privateKey = (PrivateKey)
    aKeyStore.getKey(alias,
    password);
    PrivateKeyAndCertChain result = new
    PrivateKeyAndCertChain();
    result.mPrivateKey = privateKey;
    return result;
    } else {
    throw new KeyStoreException("The keystore is empty!");
    }
    }

    private static byte[] signAckno(PrivateKey aPrivateKey)
    throws GeneralSecurityException, IOException {
    String DIGITAL_SIGNATURE_ALGORITHM_NAME = "SHA1withRSA";
    Signature signatureAlgorithm = Signature
    .getInstance(DIGITAL_SIGNATURE_ALGORITHM_NAME);
    String strAckNo = "41516019";

    byte[] ba = strAckNo.getBytes();

    signatureAlgorithm.initSign(aPrivateKey);
    signatureAlgorithm.update(ba);
    byte[] digitalSignature = signatureAlgorithm.sign();
    // Display plain text and signature
    return digitalSignature;
    }

    public static String encodeBase64(byte[] rawData2) {
    return Base64.encode(rawData2);
    }

    public static void main(String args[]) throws GeneralSecurityException,
    IOException, CertificateException {
    String File = "d:\\projects\\GANGADHARAN.pfx";
    String pwd = "1235";
    KeyStore keyStore = loadKeyStoreFromPFXFile(File, pwd);
    PrivateKeyAndCertChain PrivateKeyAndCertChain =
    getPrivateKeyAndCertChain(
    keyStore, pwd);
    PrivateKey aPrivateKey = PrivateKeyAndCertChain.mPrivateKey;
    byte[] bytestr = signDocument(aPrivateKey);
    String strDocumentBase64 = encodeBase64(bytestr);
    System.out.println("strDocumentBase64" + strDocumentBase64);

    }

    }

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
     
    suresh123kiran, Aug 19, 2012
    #1
    1. Advertising

  2. Re: How to digitally sign the message using digital certificate and

    To: suresh123kiran
    From: "Daniele Futtorovic" <daniele.futtorovic@1:261/38.remove-v8v-this>

    To: suresh123kiran
    From: "Daniele Futtorovic" <daniele.futtorovic@1:261/38.remove-7rm-this>

    To: suresh123kiran
    From: "Daniele Futtorovic" <daniele.futtorovic@1:261/38.remove-rj6-this>

    To: suresh123kiran
    From: Daniele Futtorovic <>

    On 17/08/2012 19:53, suresh123kiran allegedly wrote:
    > Hi All ... I am working on webservices....
    > I need to digitally sign a message in which AcknowledgementNo using digital

    Certificate with X.509 certificate private key.
    > I tried this code but it showing invalid signature Encoding. Can anyone pls

    us to digitally sign.
    > Here is the below Java code to sign the AcknowledgementNo,Pls help me.
    > Thanks in advance.
    >
    >
    > public class DigiAckNo {
    > static class PrivateKeyAndCertChain {
    > public PrivateKey mPrivateKey;
    > }
    >
    > private static KeyStore loadKeyStoreFromPFXFile(String aFileName,
    > String aKeyStorePasswd) throws

    GeneralSecurityException,
    > IOException {
    > String PKCS12_KEYSTORE_TYPE = "PKCS12";
    > KeyStore keyStore = KeyStore.getInstance(PKCS12_KEYSTORE_TYPE);
    > FileInputStream keyStoreStream = new

    FileInputStream(aFileName);
    > char[] password = aKeyStorePasswd.toCharArray();
    > keyStore.load(keyStoreStream, password);
    > return keyStore;
    > }
    >
    >
    > private static PrivateKeyAndCertChain getPrivateKeyAndCertChain(
    > KeyStore aKeyStore, String aKeyPassword)
    > throws GeneralSecurityException {
    > char[] password = aKeyPassword.toCharArray();
    > Enumeration<String> aliasesEnum = aKeyStore.aliases();
    > if (aliasesEnum.hasMoreElements()) {
    > String alias = (String) aliasesEnum.nextElement();
    > Certificate[] certificationChain = aKeyStore
    > .getCertificateChain(alias);
    > Certificate certificate509 =

    aKeyStore.getCertificate(alias);
    >
    > PrivateKey privateKey = (PrivateKey)

    aKeyStore.getKey(alias,
    > password);
    > PrivateKeyAndCertChain result = new

    PrivateKeyAndCertChain();
    > result.mPrivateKey = privateKey;
    > return result;
    > } else {
    > throw new KeyStoreException("The keystore is empty!");
    > }
    > }
    >
    > private static byte[] signAckno(PrivateKey aPrivateKey)
    > throws GeneralSecurityException, IOException {
    > String DIGITAL_SIGNATURE_ALGORITHM_NAME = "SHA1withRSA";
    > Signature signatureAlgorithm = Signature
    > .getInstance(DIGITAL_SIGNATURE_ALGORITHM_NAME);
    > String strAckNo = "41516019";
    >
    > byte[] ba = strAckNo.getBytes();
    >
    > signatureAlgorithm.initSign(aPrivateKey);
    > signatureAlgorithm.update(ba);
    > byte[] digitalSignature = signatureAlgorithm.sign();
    > // Display plain text and signature
    > return digitalSignature;
    > }
    >
    > public static String encodeBase64(byte[] rawData2) {
    > return Base64.encode(rawData2);
    > }
    >
    > public static void main(String args[]) throws GeneralSecurityException,
    > IOException, CertificateException {
    > String File = "d:\\projects\\GANGADHARAN.pfx";
    > String pwd = "1235";
    > KeyStore keyStore = loadKeyStoreFromPFXFile(File, pwd);
    > PrivateKeyAndCertChain PrivateKeyAndCertChain =

    getPrivateKeyAndCertChain(
    > keyStore, pwd);
    > PrivateKey aPrivateKey = PrivateKeyAndCertChain.mPrivateKey;
    > byte[] bytestr = signDocument(aPrivateKey);
    > String strDocumentBase64 = encodeBase64(bytestr);
    > System.out.println("strDocumentBase64" + strDocumentBase64);
    >
    > }
    >
    > }
    >


    > I tried this code but it showing invalid signature Encoding


    Where and how exactly does it show that? Is it an exception? Or the result of
    some processing? If the latter, what processing?

    What exactly are your requirements? Do you really just need the signature, or
    perhaps some kind of CMS envelope?

    --
    DF.

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
     
    Daniele Futtorovic, Aug 19, 2012
    #2
    1. Advertising

  3. RE: How to digitally sign the message using digital certificate and Bas

    To: suresh123kiran
    From: "suresh123kiran" <suresh123kiran@1:261/38.remove-v8v-this>

    To: suresh123kiran
    From: "suresh123kiran" <suresh123kiran@1:261/38.remove-7rm-this>

    To: suresh123kiran
    From: "suresh123kiran" <suresh123kiran@1:261/38.remove-rj6-this>

    To: suresh123kiran
    From: suresh123kiran <>

    Thanks for ur reply... As i am working on Income Tax (DIT) project. They have
    given third party wsdl.
    I need to get the response in which i need to sent four request parameter. One
    of which is signature.
    So i try to sign a "AcknowledgmentNo" it showing "Signature encoding
    errorUnable to process digital signature". The requirement for the signature
    parameter is "AcknowledgementNo digitally signed by digital certificate and
    after digitally signed it should be Base64 encoded". I need ur help whether i
    am doing correct in signature parameter. If so why it showing Signature
    Encoding Error.
    Is any other any to process the signature. If anyother information is needed to
    verify i can send to u.

    --http://compgroups.net/comp.lang.java.programmer/how-to-digitally-sign-the-mes
    sage-u/1693781

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    -+- BBBS/Li6 v4.10 Dada-1
    + Origin: Prism bbs (1:261/38)
    -+- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
     
    suresh123kiran, Aug 19, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. suresh123kiran
    Replies:
    2
    Views:
    372
    suresh123kiran
    Aug 18, 2012
  2. suresh123kiran
    Replies:
    2
    Views:
    325
    suresh123kiran
    Aug 18, 2012
  3. suresh123kiran
    Replies:
    2
    Views:
    316
    suresh123kiran
    Aug 19, 2012
  4. suresh123kiran
    Replies:
    2
    Views:
    343
    suresh123kiran
    Aug 20, 2012
  5. suresh123kiran
    Replies:
    0
    Views:
    260
    suresh123kiran
    Aug 20, 2012
Loading...

Share This Page