我想从签名内容中提取原始数据。
在以下代码中,签名数据为“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");
}
}
}