encryption - How to get RSA Public Key and Private Key and get the modulus and exponent in plain text? -
i kinda new cryptography, , requirement needs me
"create or retrieve user’s rsa-oaep key. there should 1 public/private key pair each user device"
and send server in form:
{"modulus":"qmbrpdyray5ammo31neruizh5sbwegusmh4wlk6ujeidl+kwtlrone34kofexetbjsx0wygpi+vwl0ynq7buimukpytossaawut5kho3cqjxtk7g2gnepnuuxhiexggnrlzcslv8yilfvalhorwyc67kol+a+3tanq3h+bhewhm=","exponent":"aqab"}
i tried openssl commands generate rsa public/private key pair, , comes out this:
public key (have changed value bit security):
ssh-rsa aaaab3nzac1yc2eaaaadaqabaaabaqc4nmwqhswk6py+n1olpbn3jelqedz8ywsn4j1kvp5hk+ps/5gcabkx/89budhhtkvz8mfsnkhkhu2wubpiikgavekbbbq8dhkubha07x1xtgy+qmyxtlahnaec6ooi6bixpfo6qam+snqxbwuvdpbzgqss+/p5r7aydlbalkoz+dzpbbahiik72c+xqcl8oofz4qqsvmakdkq+fvdm1e8u2ak4ygholeqjgcl8+xh7tfjsvuofsgsmlz2fmnxwhcyudokglp8zuvmuiaw9kejafta4xef3bbgrdyss7mlqf5/rhbpzyenwq1rvp8bhtumpqytqjzshuj8da3oqmfur xyz@xyz.xyz
private key (have altered value bit security):
-----begin rsa private key----- proc-type: 4,encrypted dek-info: aes-128-cbc,67a126ee3266b641af4ac698aa18fe01 3w4gi7hfymd0a8+lokubsiii50cjfszbjo5pr4tw532cckvmqrkywbmanuwx5b66 mkr/uy9lh8mjohwrwwixcufkmtdgcc06nhbiwiau/tp85tidwhtgabqsykjh+8hm slwtme2xjpq+x8xc8+vw6ng8mvxst0ezz4ditka9yeh0evxiq4wipvo7x9793xsd foxq5scy7mr6gbnxln4f9cy5bwesega86dtzxvkk8gu8xry28ge4w7d6/enkgl6b oveuuc7idibp/qrgaq/nxdrl6qrnftd2fcqfbr4jearc+iaexaxxgqhhtjh9ysk8 zneamiruegvrbchwzlvqs/scmcs8eod22di94gmkv4thsiwuk1mtrmtkftmagvob 8shckgcr5fjxjphv8suhiwk4trqwx70fwm3qmbq3ue2angonwxhzxqu7jb36zatm dhpshz2/80bub/hmnfjpsjcyu16pm9bunsms7tymw3x3f91x6lic3j2itujekpme p6eze2kdm+qxldlfebl+bgmn6ryvemfvko44nwnimnjm9j3zynm9kgt87b4lovwn tewigrcq9srcpiavbzj+m9ddduqxsoa0wxsdrcyjwt8trzs20awj7lsxbqguvpbx nufaqgmgt4dk9x2z9esplxi/l2uz0idbtn4sehi3or3ar2rwsjoqrttfog7cylf1 ewlr6toceevturc4vlywyrdiu3p/jisz6eistei9w02rq/qiluroukx1lwviikr2 ogqnkzm3iinq0jykzobwcsdluy6ra4nv/zbsjldwb34gvekszortx4dzqmtcv0kq ndua6xdapmpv3n4slrd1pxswngkb4qwlyuqmpklhcxuq4yg59iooh7yfxs5uz7wa yndgmlmpmylchdlx02u90x3feucc9iie7z6poily4uc28z2x5kyjok07pwa+5lnt 9rvryak4ixysjz5zqsbuaeylqqbatcepyn3yxbt5csaxkv4li36g6ig7/qma3pgt 1l57kbw2xnusrqm5xtzzmrssu2iz9hiuh73srkodjg7toemtwleckn1trl9pveqj qhaxauwlec+2yb0+1xh7/cywkyk2hxenqjjdswu+xjm/rfgfnv481lwfu0hw58sz ai4r2lmddoy4wwtcqgky13zht8h/jxp4/sr36gjdvkhhucdnpffdnhebgflj0pqg ethvy/6gqfnkvneogtdrr4ekbfopuv5olzurx3z/rlhrv1ipcaqhool9xo320jyq xy5ymwq7tvgze5jtqo65awo8wws12ntzg1krhcxcyye4da1t6k7l8++moevgzy1v qdxeddtfiazivcme2leiyob2/uunahem+ave/lfwmirw5dsaoppzenhunuzh9evd ifvswraxcd4bqa5ggczzu9eiqdzpsptrng3hxof+txexf3ttah/stvfqgqho1ird uth9fgghk3wmswbnyyfpbsor8mghab966rryp2xbcvjevcymyuue11x6vsvqfpgs x2sehopzginukzrtuvmhk1oxrt3bzo8yq13t+wtqcp6a8azs+sc436adqblwqfj2 -----end rsa private key-----
how can extract modulus , exponent in form server expects data?
you can format rfc 4253 need find out mpint
(multi-precission integer) format rfc 4251.
here's thing in java (just java), because jsch library horrible.
package nl.owlstead.crypto; import java.math.biginteger; import java.nio.bytebuffer; import java.nio.charset.standardcharsets; import java.security.keyfactory; import java.security.nosuchalgorithmexception; import java.security.interfaces.rsapublickey; import java.security.spec.invalidkeyspecexception; import java.security.spec.rsapublickeyspec; import java.util.arrays; import java.util.base64; import java.util.base64.encoder; public class sshpublickeydecoder { private static byte[] ssh_rsa_header = "ssh-rsa".getbytes(standardcharsets.us_ascii); private static rsapublickey decodersapublickey(byte[] encodedrsapublickey) throws nosuchalgorithmexception, invalidkeyspecexception { bytebuffer buf = bytebuffer.wrap(encodedrsapublickey); byte[] header = getsshencodedvaluefrombuffer(buf); if (!arrays.equals(header, ssh_rsa_header)) { throw new illegalargumentexception("not rsa public key"); } byte[] edatasigned = getsshencodedvaluefrombuffer(buf); biginteger e = new biginteger(edatasigned); byte[] ndatasigned = getsshencodedvaluefrombuffer(buf); biginteger n = new biginteger(ndatasigned); keyfactory rsakeyfactory; try { rsakeyfactory = keyfactory.getinstance("rsa"); } catch (nosuchalgorithmexception ex) { throw new illegalstateexception( "keyfactory should exist rsa", ex); } rsapublickeyspec spec = new rsapublickeyspec(n, e); return (rsapublickey) rsakeyfactory.generatepublic(spec); } private static byte[] getsshencodedvaluefrombuffer(bytebuffer buf) { int size = buf.getint(); if (size < 0) { throw new illegalargumentexception("bad ssh encoded value in format"); } byte[] data = new byte[size]; buf.get(data); return data; } private static byte[] tounsigned(biginteger value) { if (value.compareto(biginteger.zero) <= 0) { throw new illegalargumentexception("negative numbers cannot encoded unsigned integers"); } if (value.equals(biginteger.zero)) { return value.tobytearray(); } final byte[] signedbigendian = value.tobytearray(); if (signedbigendian[0] == 0x00) { return arrays.copyofrange(signedbigendian, 1, signedbigendian.length); } return signedbigendian; } private sshpublickeydecoder() { } public static void main(string[] args) throws exception { string[] parts = args[0].split("\\s+"); string part2 = parts[1]; byte[] encodedrsapublickey = base64.getdecoder().decode(part2); rsapublickey pubkey = decodersapublickey(encodedrsapublickey); string format = encodeserverpublickey(pubkey); system.out.println(format); } private static string encodeserverpublickey(rsapublickey pubkey) { byte[] ndata = tounsigned(pubkey.getmodulus()); byte[] edata = tounsigned(pubkey.getpublicexponent()); encoder base64encoder = base64.getencoder(); string format = string.format( "{\"modulus\":\"%s\",\"exponent\":\"%s\"}%n", base64encoder.encodetostring(ndata), base64encoder.encodetostring(edata)); return format; } }
Comments
Post a Comment