Created
July 13, 2017 19:54
-
-
Save nwrox/d77a2cb2178ec14bfeb6a712f428d63e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const asn1 = require('asn1.js') | |
, BN = require('bn.js') | |
, crypto = require('crypto') | |
const privKey = '{' + | |
'"version": "0",' + | |
'"modulus": "259154769957187194386805172792520197599479908823949686364408864499' + | |
'94962945058195024520647063684248740786360017811823364689336100960942063494732020' + | |
'91812725346950176505638890859938367164068105119839615900697880416966060139081266' + | |
'35977887948210779573734140292700996701877639405960971827664641117294919785640103' + | |
'31282655617782256136304620084543025951581389496160817980710447192869233119415254' + | |
'83228378122999203866207763219299483137490424056323682898122355491412590424064198' + | |
'31175982247590276087656550972189353064884393069002375132476879581449272049054799' + | |
'62070178133928595171728299138526996384644282597592811991631570459402419",' + | |
'"publicExponent": "10001",' + | |
'"privateExponent": "1092098568127752655477642636470429329850023073431192833899' + | |
'41121831320639268020960834758791170780188299214192875780351535329674030096880638' + | |
'53469463269617769197801738709144191110099238489116117415822478965594657551548305' + | |
'62559543752953513406393691144388095454042946305432767999622553512341395849254513' + | |
'98518169722494890114932728957432311412055517373531012830934389856608006058726000' + | |
'52841024174146171389528380824097395876975983314337509964771693522278488935985611' + | |
'08501595128043659375177532749133241511672361132074087312215893704393208168847222' + | |
'5525723772010830699496394782473125402187979005479435127439404805226844156772789",' + | |
'"prime1": "3305774249709802925746146237767283863231849447854777343529861386022' + | |
'99397999543010254564369595600419471030135589862018388839355857683866754708715404' + | |
'07523835679158149763547132636200543369177166246832828069501603572268111855094804' + | |
'23908452333029521327729673440592636932833296803374808046278481189209190890737627' + | |
'33",' + | |
'"prime2": "7839457578808869689691621098655550207909292906724123209867190743293' + | |
'84364559104777094019199116033578520508257910628132268884163762487053320167174566' + | |
'50105323623436528546551551039086519784001703295671434096023551291248414288210905' + | |
'28446414666057009950127126553421956441557648842020895650586487637666249481264534' + | |
'3",' + | |
'"exponent1": "1424646237001224938502738754602239121140813030722336801381232134' + | |
'16249415596243413078409402355468233968617126726557874138175944780183728198459610' + | |
'37811861587019015261021986615504651968995215837068678081089846206518995656643881' + | |
'47247979744981037619930258463406554771040947056216632878980048020590490112262681' + | |
'64921",' + | |
'"exponent2": "4954825652999786552198853811079065379881475898750443236633387930' + | |
'04303426495160613539775558205424282554557814043903652041957484165851481255552115' + | |
'83190212553806993530341241111300676521503317144938700043487747711999998122859292' + | |
'18401190859624452190770398316862731745492159373895651842956440074828286007696337' + | |
'6783",' + | |
'"coefficient": "22374481815305049524627388490262218667614517025257367227320445' + | |
'46830187464353956304396807700099158833440058942635438570192434084085445330956057' + | |
'16661018632898308383631011256372191062197923420834373484339478532597957303744545' + | |
'36606261153012845841893725092995375659932376626139786309632913860528580016307349' + | |
'4996962"' + | |
'}' | |
const pubKey = '{' + | |
'"modulus": "259154769957187194386805172792520197599479908823949686364408864499' + | |
'94962945058195024520647063684248740786360017811823364689336100960942063494732020' + | |
'91812725346950176505638890859938367164068105119839615900697880416966060139081266' + | |
'35977887948210779573734140292700996701877639405960971827664641117294919785640103' + | |
'31282655617782256136304620084543025951581389496160817980710447192869233119415254' + | |
'83228378122999203866207763219299483137490424056323682898122355491412590424064198' + | |
'31175982247590276087656550972189353064884393069002375132476879581449272049054799' + | |
'62070178133928595171728299138526996384644282597592811991631570459402419",' + | |
'"publicExponent": "10001"' + | |
'}' | |
const privateKey = () => { | |
const priv = JSON.parse(privKey) | |
return asn1.define('privKey', function() { | |
this.seq().obj( | |
this.key('version').int(), | |
this.key('modulus').int(), | |
this.key('publicExponent').int(), | |
this.key('privateExponent').int(), | |
this.key('prime1').int(), | |
this.key('prime2').int(), | |
this.key('exponent1').int(), | |
this.key('exponent2').int(), | |
this.key('coefficient').int() | |
) | |
}).encode({ | |
version: new BN(priv.version.toString()), | |
modulus: new BN(priv.modulus.toString()), | |
publicExponent: new BN(priv.publicExponent.toString()), | |
privateExponent: new BN(priv.privateExponent.toString()), | |
prime1: new BN(priv.prime1.toString()), | |
prime2: new BN(priv.prime2.toString()), | |
exponent1: new BN(priv.exponent1.toString()), | |
exponent2: new BN(priv.exponent2.toString()), | |
coefficient: new BN(priv.coefficient.toString()) | |
}, 'pem', { | |
label: 'RSA PRIVATE KEY' | |
}) | |
} | |
const publicKey = () => { | |
const pub = JSON.parse(pubKey) | |
return asn1.define('pubKey', function() { | |
this.seq().obj( | |
this.key('modulus').int(), | |
this.key('publicExponent').int() | |
) | |
}).encode({ | |
modulus: new BN(pub.modulus.toString()), | |
publicExponent: new BN(pub.publicExponent.toString()) | |
}, 'pem', { | |
label: 'RSA PUBLIC KEY' | |
}) | |
} | |
const cipher = crypto.publicEncrypt( | |
{ | |
key: publicKey(), | |
passphrase: '', | |
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING | |
}, Buffer.from('teste') | |
).toString('base64') | |
console.log(cipher, '\n\n') | |
const message = crypto.privateDecrypt( | |
{ | |
key: privateKey(), | |
passphrase: '', | |
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING | |
}, Buffer.from(cipher, 'base64')) | |
console.log(message) | |
// let signer = crypto.createSign('RSA-SHA256') | |
// signer.update('teste') | |
// | |
// let sign = signer.sign(privateKey(), "hex") | |
// | |
// console.log(sign, '\n\n') | |
// | |
// let verifier = crypto.createVerify("RSA-SHA256") | |
// verifier.update('teste') | |
// let result = verifier.verify(publicKey(), sign, "hex") | |
// | |
// console.log(result) | |
// console.log(privateKey()) | |
// console.log() | |
// console.log(publicKey()) | |
// console.log(JSON.parse(privKey)) | |
// console.log() | |
// console.log(JSON.parse(pubKey)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment