2 dwq09 dwq09 于 2015.07.22 22:52 提问

RSA 十六进制公钥获取publicKey java 5C

我用如下代码(1)生成公钥得出其十六进制的字符串为:
//初始化keyPairGenerator
KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
//获取密钥对
KeyPair rsaKpg= kpg.generateKeyPair();
//获取公钥
RSAPublicKey rsaPubKey=(RSAPublicKey) rsaKpg.getPublic();
//十六进制公钥
String hexstring=ConvertUtil.asc2HexString(rsaPubKey.getEncoded());
System.out.println(hexstring);
得出的十六进制字符串为:
30819f300d06092a864886f70d010101050003818d00308189028181008d4890ab47abbc271bd913bf7493d5f2082c4135c421479a33afc9b9ce66ab8974534d48421c24045049d2ef24fded928b4ff678d41272ee0746b02b4bc2f6675e77dbafe63045be6843e14d5ffbe03d51cd945a1fcc93bb99d58a29b71f43d723d0b73f41ac85ed15c780f10186a270a05aa2abc73858933224d1152dc7b2730203010001

从十六进制字符串可以得出:
RSAPublicKey的系数为"8d4890ab47abbc271bd913bf7493d5f2082c4135c421479a33afc9b9ce66ab8974534d48421c24045049d2ef24fded928b4ff678d41272ee0746b02b4bc2f6675e77dbafe63045be6843e14d5ffbe03d51cd945a1fcc93bb99d58a29b71f43d723d0b73f41ac85ed15c780f10186a270a05aa2abc73858933224d1152dc7b273"
RSAPublicKey的指数为"10001"

然后我用系数和指数用如下代码(2)生成publicKey
String big1="8d4890ab47abbc271bd913bf7493d5f2082c4135c421479a33afc9b9ce66ab8974534d48421c24045049d2ef24fded928b4ff678d41272ee0746b02b4bc2f6675e77dbafe63045be6843e14d5ffbe03d51cd945a1fcc93bb99d58a29b71f43d723d0b73f41ac85ed15c780f10186a270a05aa2abc73858933224d1152dc7b273";
String big2="10001";
BigInteger b1=new BigInteger(big1,16);
BigInteger b2=new BigInteger(big2,16);
System.out.println(b1.toString());
System.out.println(b2.toString());
RSAPublicKeySpec rsaPubKS=new RSAPublicKeySpec(new BigInteger(big1,16),new BigInteger(big2,16));
KeyFactory kf=KeyFactory.getInstance("RSA");
RSAPublicKey pbk=(RSAPublicKey) kf.generatePublic(rsaPubKS);
//十六进制公钥
String string=ConvertUtil.asc2HexString(pbk.getEncoded());
System.out.println(string);
得出的十六进制公钥和上述的一致。

现在我有十六进制公钥为:"30818902818100EE317B333A2E72572C998D422210100F3BE6E9DE40FDE66D5ADB150F4608C9BBB5681F6AD7CFAD76202B2B161927C3A8E0B24DE590083A9BEE94CBE735AD50B4D4D7BD2F4F13877098F62DF7FBED96FC7243BBBCCD6155DBA5B751AE186A9409CDFDF161A6E1A9490DB235838378E7641029BD1DB7A46D4986F2A34D7D6A1C850203010001"
那么系数为:EE317B333A2E72572C998D422210100F3BE6E9DE40FDE66D5ADB150F4608C9BBB5681F6AD7CFAD76202B2B161927C3A8E0B24DE590083A9BEE94CBE735AD50B4D4D7BD2F4F13877098F62DF7FBED96FC7243BBBCCD6155DBA5B751AE186A9409CDFDF161A6E1A9490DB235838378E7641029BD1DB7A46D4986F2A34D7D6A1C85
指数为:10001
我想如上述方法一样去验证,但是发现用代码(2)生成十六进制公钥的时候为"30819f300d06092a864886f70d010101050003818d0030818902818100ee317b333a2e72572c998d422210100f3be6e9de40fde66d5adb150f4608c9bbb5681f6ad7cfad76202b2b161927c3a8e0b24de590083a9bee94cbe735ad50b4d4d7bd2f4f13877098f62df7fbed96fc7243bbbccd6155dba5b751ae186a9409cdfdf161a6e1a9490db235838378e7641029bd1db7a46d4986f2a34d7d6a1c850203010001"
这样得出de十六进制公钥就和原来的不一致,我想知道为什么,怎样可以做到一致?

2个回答

devmiao
devmiao   Ds   Rxr 2015.07.22 23:11
apple_4872330
apple_4872330   2016.02.03 14:46

后来你是怎么处理的?我现在也需要如此,能分享下么,谢谢!

Csdn user default icon
上传中...
上传图片
插入图片