署名されたコンテンツから元のデータを抽出したい。
次のコードでは、署名されたデータは「CMSSignedDatasigned」です。
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");
}
}
}