当前位置:   article > 正文

RSA中e n生成publicKey_n e 转 publickey

n e 转 publickey

有的小伙伴在对接的时候,遇到对方提供RSA的e n值后,无所适从。

介绍下 RSA中e n生成publicKey

1、首先介绍Java里面 exponent + modulus 生成密钥的示例

  1. // 系数
  2. String modulus = "21330634227865858248547732230213326796059798755767117022480868343834112728121984949364474039682511821291005459355824408194322449820429949761626773150661940244698885082336953048639538539221761574334996089292019399728342663302031711145242545287132847203410792305843030027480036740094719887806358969850535921665074928021439069804323347388539217469163012282489425087035482024027310610563829655305073926463856787698917910634027007327194209716815756580502107781551245065651312797085643490047332324369047284014877706912613432148781948805785078164265438082438960238342363126313096095137748510060607006167571409494629737071119";
  3. // 私有指数
  4. String privateExponent = "17052920483616945802743639203347682136087356699416548350944697133021720589548244189343458700201802407261866427170744221476926475668578158716613106374350087693680720585378676442779193161481071393651033539868563779687303134123421112863631809783390121555388636221864591227242459144613313911252061764596319317022515450384712743687786278874786001191239129426492973271045297511832776208937626008179223849422563621849000173566300582651778375960466748385245627442384825880766181208804571273807390738200438475806292235479945127766356494031637226774042823363923324696913386972625296236777640818792883503584370967398335518889953";
  5. // 共有指数
  6. String publicExponent = "65537";
  7. RSAPublicKeySpec publicSpec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));
  8. RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(new BigInteger(modulus), new BigInteger(privateExponent));

2、其次,e n的其实是publicExponent和modulus的base64格式

  1. {
  2. "kty": "RSA",
  3. "e": "AQAB",
  4. "n": "qPieq1PFAJqfvAnvS6aKOV8jZAvdxk-ZD2PiV4dAJykMp1iSuiPVw88x4p0TN-_ouElL0Qo35Kxl76mnhBjmyJxV7NciYyIlc0bj93kONhPi0I82DK8ChDdYYMZ9vZexhg6pdpIX3dc33IOqvjHMXoFJ-Gqq_FS9p5hjry_TyiXkvwsGvufkhfo4XPhK6eEF3WNHaPZKdTygK7ghC23bFK2vKQsf6R8dyc4IAHMCjypCq6f1QZbvb2ptUeE6Ut9452tuhzhXIpM-nOpf-sHi5x-togVVaqrlIlAsNEaJLHBS9RZaZP8rSauLwxk2rs5LqxztenNvbe6fu1gLq1SCDw"
  5. }

 3、所以,publicExponent + modulus 这样就可以很简单的转换成公钥了

  1. BigInteger publicExponent = new BigInteger(1, Base64.getDecoder().decode("AQAB"));
  2. BigInteger modulus = new BigInteger(1, Base64.getDecoder().decode("qPieq1PFAJqfvAnvS6aKOV8jZAvdxk-ZD2PiV4dAJykMp1iSuiPVw88x4p0TN-_ouElL0Qo35Kxl76mnhBjmyJxV7NciYyIlc0bj93kONhPi0I82DK8ChDdYYMZ9vZexhg6pdpIX3dc33IOqvjHMXoFJ-Gqq_FS9p5hjry_TyiXkvwsGvufkhfo4XPhK6eEF3WNHaPZKdTygK7ghC23bFK2vKQsf6R8dyc4IAHMCjypCq6f1QZbvb2ptUeE6Ut9452tuhzhXIpM-nOpf-sHi5x-togVVaqrlIlAsNEaJLHBS9RZaZP8rSauLwxk2rs5LqxztenNvbe6fu1gLq1SCDw"));
  3. RSAPublicKeySpec publicSpec = new RSAPublicKeySpec(modulus, publicExponent);

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/440297
推荐阅读
相关标签
  

闽ICP备14008679号