1

我想从签名内容中提取原始数据。

在以下代码中,签名数据为“CMSSignedData signed”

我从 StackOverflow 中找到了几个类似的答案,但所有答案都无法说明如何从签名内容中提取原始数据。

问候

package chapter9;

import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.*;
import java.util.Arrays;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.cms.CMSProcessable;
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;

/**
 * Example of generating a detached signature.
 */
public class SignedDataExample
    extends SignedDataProcessor
{

    public static void main(String[] args)
        throws Exception
    {
        KeyStore        credentials = Utils.createCredentials();
        PrivateKey      key = (PrivateKey)credentials.getKey(Utils.END_ENTITY_ALIAS, Utils.KEY_PASSWD);
    Certificate[]   chain = credentials.getCertificateChain(Utils.END_ENTITY_ALIAS);
    CertStore       certsAndCRLs = CertStore.getInstance("Collection",
                        new CollectionCertStoreParameters(Arrays.asList(chain)), "BC");
    X509Certificate cert = (X509Certificate)chain[0];

    // set up the generator
    CMSSignedDataGenerator gen = new CMSSignedDataGenerator();

    gen.addSigner(key, cert, CMSSignedDataGenerator.DIGEST_SHA256);
    gen.addCertificatesAndCRLs(certsAndCRLs);

    // create the signed-data object

    CMSProcessable  data = new CMSProcessableByteArray("Hello World!".getBytes());
    CMSSignedData signed = gen.generate(data, "BC");

    // recreate
    signed = new CMSSignedData(data, signed.getEncoded());

    //extract public key
    CertStore cs = signed.getCertificatesAndCRLs("Collection", "BC");



    //signed.signedContent
    //signed.g
    CMSProcessable S = signed.getSignedContent();
    String aaa = S.getContent().toString();
    //byte[] K = Base64.decodeBase64((S.getContent()).toString());



    //
    //String K = Base64.decodeBase64(S.getContent());
    //BASE64Decoder.decoder.decodeBuffer()

    //
    //byte[] array = asString.getBytes("UTF8");
    //String s = new String(array, "UTF8");

    // verification step
    X509Certificate rootCert = (X509Certificate)credentials.getCertificate(Utils.ROOT_ALIAS);

    if (isValid(signed, rootCert))
    {
        System.out.println("verification succeeded");
        //System.out.println(K);
        //String asString = new String((byte[])data.getContent());
        //String asString1 = new String(cs.toString());
        //System.out.println(asString);
        //System.out.println(asString1);
        //System.out.println(aaa);
    }
    else
    {
        System.out.println("verification failed");
    }
}

}

4

2 回答 2

1

尽管您还应该指定编码,但您需要使用String aaa = new String(s);而不是,例如. 请为您的方法做同样的事情。String aaa = S.getContent().toString();String aaa = new String(s, Charset.forName("UTF-8"));toBytes()

于 2012-11-26T23:05:50.350 回答
1

用于new String((byte[])S.getContent());获取原始内容

于 2017-06-21T12:34:22.903 回答