java - Different Encoders Provided Different Encoded Values -
pardon me! weak in java.
i referring stackoverflow post: digital signature creation , verification. added base64 encoders of bouncy castle , apache. return differemt strings ([b@a470b8,[b@1e4457d,[b@10b4b2f, [b@750159..). "sun.misc.base64encoder" returns encoded. but, restricted. please advise how use bouncy castle base64 or apache bas64 classes. in advance!
my code , results follows:
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 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 =====================================
the base64 encoders of bouncy castle , apache return encoded data (even though consist of ascii characters) byte arrays (which know, after have used byte[]
type these information).
if want output contents in readable form, should not xxx.tostring()
(because result in [b@a470b8,[b@1e4457d,[b@10b4b2f, [b@750159
, i.e. type identifier [b
, memory address) instead new string(xxx)
, e.g. instead of
system.out.println("signed content: " + bcsignedcontent.tostring());
use
system.out.println("signed content: " + new string(bcsignedcontent));
Comments
Post a Comment