-1
我正在嘗試使用此代碼question來簽署消息。無法使用bouncycastle簽署內容
我面臨的問題是,而不是原來的內容我有一些毫無意義的廢話。 略有改變代碼:
public class BCTestSign2 {
static final String KEYSTORE_FILE = "c:\\clientkeystore";
static final String KEYSTORE_INSTANCE = "JKS";
static final String KEYSTORE_PWD = "javacaps";
static final String KEYSTORE_ALIAS = "client";
public static void main(String[] args) throws Exception {
String text = "This is a message";
Security.addProvider(new BouncyCastleProvider());
KeyStore ks = KeyStore.getInstance(KEYSTORE_INSTANCE);
ks.load(new FileInputStream(KEYSTORE_FILE), KEYSTORE_PWD.toCharArray());
Key key = ks.getKey(KEYSTORE_ALIAS, KEYSTORE_PWD.toCharArray());
//Sign
PrivateKey privKey = (PrivateKey) key;
Signature signature = Signature.getInstance("SHA1WithRSA", "BC");
signature.initSign(privKey);
signature.update(text.getBytes());
//Build CMS
X509Certificate cert = (X509Certificate) ks.getCertificate(KEYSTORE_ALIAS);
List certList = new ArrayList();
CMSTypedData msg = new CMSProcessableByteArray(signature.sign());
certList.add(cert);
Store certs = new JcaCertStore(certList);
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider("BC").build(privKey);
gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider("BC").build()).build(sha1Signer, cert));
gen.addCertificates(certs);
CMSSignedData sigData = gen.generate(msg, true);
System.out.print ("Signed content: ");
sigData.getSignedContent().write (System.out);
}
}
,輸出是:
簽名內容:7Ѓ「(2XжS^р««Ц8в@üqШ<€&чcеЫR,ьћIк¤еџ」рМр 「Гx|ЛЗжzҐЎНD,Y•*ґ№‰•^d1г,qNюТЉG°yюЄЭќ2ЉшОuхcS-Ѕљg[Яμр·№У_С`|еo」ќў‰†і
我使用同樣的罐子:bcprov-jdk16- 1.46,bcmail-jdk16-1.46用v1.6編譯a nd jdk 我也嘗試了相同的代碼,爲後來的jdks和jar。
任何想法?
upd1: 我有一個包含顯式簽名消息的簽名文件的示例。因此,您可以打開文件並查看符號之間的原始消息。當我得到「Enveloped data」(匹配原始文章)時,我可以看到我的證書的詳細信息,但是我找不到原始消息 - 只有sigData.getSignedContent()中的散列值。
我做到了。相同。看起來這個輸出實際上是簽名消息的散列。但是原始信息在哪裏呢?而這個哈希不依賴於JcaContentSignerBuilder()中的參數 – Denis