不同的编码器提供不同的编码值

时间:2014-03-06 13:44:30

标签: java encoding digital-signature


对不起!我在java中很弱。

我指的是stackoverflow帖子:Digital Signature Creation and Verification。我添加了Bouncy Castle和Apache的Base64编码器。它们返回一些不同的字符串,如([B @ a470b8,[B @ 1e4457d,[B @ 10b4b2f,[B @ 750159 ...])。 “sun.misc.BASE64Encoder”返回正确编码。但是,它是受限制的。请告知如何使用Bouncy Castle Base64或Apache Bas64类。在此先感谢!

我的准则和结果如下:

package abc;

import java.io.FileInputStream;<br>
import java.security.Key;<br>
import java.security.KeyStore;<br>
import java.security.PrivateKey;<br>
import java.security.Security;<br>
import java.security.Signature;<br>
import java.security.cert.X509Certificate;<br>
import java.util.ArrayList;<br>
import java.util.List;<br>
import org.apache.pdfbox.pdfwriter.COSWriter;<br>
import org.bouncycastle.cert.jcajce.JcaCertStore;<br>
import org.bouncycastle.cms.CMSProcessableByteArray;<br>
import org.bouncycastle.cms.CMSSignedData;<br>
import org.bouncycastle.cms.CMSSignedDataGenerator;<br>
import org.bouncycastle.cms.CMSTypedData;<br>
import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder;<br>
import org.bouncycastle.jce.provider.BouncyCastleProvider;<br>
import org.bouncycastle.operator.ContentSigner;<br>
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;<br>
import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;<br>
import org.bouncycastle.util.Store;<br>
import sun.misc.BASE64Encoder;<br>

public class GenerateSignature {
public static void main(String[] args) throws Exception {
    String KEYSTORE_FILE = "resources/my.p12";
    String KEYSTORE_INSTANCE = "PKCS12";
    String KEYSTORE_PWD = "password";
    String KEYSTORE_ALIAS = "signCert";
    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, false);

    BASE64Encoder encoder = new BASE64Encoder();

    System.out.println("================Start : Signed Content Using Sun BASE64Encoder=================================================================================");
    String signedContent = encoder.encode((byte[]) sigData.getSignedContent().getContent());
    System.out.println("Signed content: " + signedContent.toString());
    System.out.println("================End : Signed Content Using Sun BASE64Encoder=================================================================================");
    System.out.println("");
    System.out.println("================Start : Signed Encoded Content Using Sun BASE64Encoder=================================================================================");
    String envelopedData = encoder.encode(sigData.getEncoded());
    System.out.println("Enveloped data: " + envelopedData.toString());
    System.out.println("================End : Signed Encoded Content Using Sun BASE64Encoder=================================================================================");
    System.out.println("");
    System.out.println("");

    //Base64 bcEncoderDecoder = new Base64();
    System.out.println("================Start : Signed Content Using Bouncy Castle BASE64=================================================================================");
    byte[] bcSignedContent = org.bouncycastle.util.encoders.Base64.encode((byte[]) sigData.getSignedContent().getContent());
    System.out.println("Signed content: " + bcSignedContent.toString());
    System.out.println("================End : Signed Content Using Bouncy Castle BASE64=================================================================================");
    System.out.println("");
    System.out.println("================Start : Signed Encoded Content Using Bouncy Castle BASE64=================================================================================");
    byte[] bcEnvelopedData = org.bouncycastle.util.encoders.Base64.encode(sigData.getEncoded());
    System.out.println("Enveloped data: " + bcEnvelopedData.toString());
    System.out.println("================End : Signed Encoded Content Using Bouncy Castle BASE64=================================================================================");
    System.out.println("");
    System.out.println("");

    //Base64 apEncoderDecoder = new Base64();
    System.out.println("================Start : Signed Content Using Apache BASE64=================================================================================");
    byte[] apSignedContent = org.apache.commons.codec.binary.Base64.encodeBase64((byte[]) sigData.getSignedContent().getContent());
    System.out.println("Signed content: " + apSignedContent.toString());
    System.out.println("================End : Signed Content Using Apache BASE64=================================================================================");
    System.out.println("");
    System.out.println("================Start : Signed Encoded Content Using Apache BASE64=================================================================================");
    byte[] apEnvelopedData = org.apache.commons.codec.binary.Base64.encodeBase64(sigData.getEncoded());
    System.out.println("Enveloped data: " + apEnvelopedData.toString());
    System.out.println("================End : Signed Encoded Content Using Apache BASE64=================================================================================");
    System.out.println("");
    System.out.println("");

}

}

Output
================Sun BASE64Encoder=========================================================
Signed content:     Se4JKZuREHmSfxMu3ffB7+bke17g15CKB9IOoNYJ7EFphuvoibD8j/WSydVBDtX0PLWjSsNHLFE6
BZCmhZVeBGju8l7rnkBH9ceBT3x6lQpFvVup16topNNGidfFA4gE97JA2+dco5VtIz15ltV6nit8
ktGFjgscyGlKBxgoWok=

Enveloped data: MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIAwggGUMIH+
oAMCAQICBgFEkafvuDANBgkqhkiG9w0BAQQFADAOMQwwCgYDVQQDEwNTQkkwHhcNMTQwMzA0MDk1
MTQ2WhcNMTYwMzA0MDk1MTQ2WjAOMQwwCgYDVQQDEwNTQkkwgZ8wDQYJKoZIhvcNAQEBBQADgY0A
MIGJAoGBAMaBj6hoxls1fwrImsKDCReYQJ3ldS773c0bg4N5TOM+5r57VKm9LBKcq9XrnzsjZcXT
oHE6wLtHS/XRptGymkwLrgaVC47PTGwEAoWLwzVM6zwYs8LgtAFqR1JsvScJoth02Y1GGdUEbZB3
WMjmRBW/5nt3Ktna3uGPgSA5f/8zAgMBAAEwDQYJKoZIhvcNAQEEBQADgYEAxUAUoTxZt4CzGd0K
FYT/B0QhTq6pqnag9sLSKSFbqPsidxrRkkKDBBXH33RkDH0t0xQTWN8/2RValOrQnYAblSe6kRPr
4/DM+SWcgWGNk8/GwiFzbjjyV76iAGU5+CBiK/M1omsY/7rwjGM6nms3NnVy7VGhYMUSUSVpa3iX
c2sAADGCAR0wggEZAgEBMBgwDjEMMAoGA1UEAxMDU0JJAgYBRJGn77gwCQYFKw4DAhoFAKBdMBgG
CSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTE0MDMwNjExNTExOVowIwYJ
KoZIhvcNAQkEMRYEFAVeTwUS6KYObOs2oVZMvsA9xPPLMA0GCSqGSIb3DQEBAQUABIGAugvdFKvr
7/QSGPDsJEOS3a0U0UqJALV01iDCRhsa1SD23qYJaxbEDnhAJHZ6ymyi0vU5wLkILy3NRwPAjmvQ
ITIX/WRO+qeQBAXK5SiTYhJSsGHYKI0ccVix0skoVEBD9yjox0eEiJitb57LR42kAnhDXyeAJI9O
Yy2IPYn8wU8AAAAAAAA=
================End :Sun BASE64Encoder Results ==========================================

================Start : Bouncy Castle BASE64 Encoding =====================================
Signed content: [B@a470b8<br>
Enveloped data: [B@1e4457d<br>
================End : Bouncy Castle BASE64 Encoding =====================================

================Start : Apache BASE64 Encoding =====================================
Signed content: [B@10b4b2f
Enveloped data: [B@750159
================End : Apache BASE64 Encoding =====================================

1 个答案:

答案 0 :(得分:3)

Bouncy Castle和Apache的 Base64编码器将编码数据(即使它们仅由ASCII字符组成)作为字节数组返回(你知道,毕竟你使用了byte[]作为这些信息的类型)。

如果你想以可读的形式输出内容,你不应该XXX.toString()(因为这只会导致[B@a470b8,[B@1e4457d,[B@10b4b2f, [B@750159,即类型标识符[B和内存地址)但是而是new String(XXX),例如而不是

System.out.println("Signed content: " + bcSignedContent.toString());

使用

System.out.println("Signed content: " + new String(bcSignedContent));