RSA code: Difference between revisions

Content added Content deleted
m (Phix/mpfr)
Line 693: Line 693:
System.out.println("As text: " + decText);
System.out.println("As text: " + decText);
}
}
</lang>
===Alternative solution - convert to byte array then to BigInteger: ===
<lang java>
import java.math.BigInteger;
import java.util.Random;

public class rsaCode {
public static void main(String[]args){
//Size of primes
int BIT_LENGTH = 4096;
Random rand = new Random();
//Generate primes and other necessary values
BigInteger p = BigInteger.probablePrime(BIT_LENGTH / 2, rand);
BigInteger q = BigInteger.probablePrime(BIT_LENGTH / 2, rand);
BigInteger n = p.multiply(q);
BigInteger phi = p.subtract(BigInteger.valueOf(1)).multiply(q.subtract(BigInteger.valueOf(1)));
BigInteger e;
BigInteger d;
do {
e = new BigInteger(phi.bitLength(), rand);
} while (e.compareTo(BigInteger.valueOf(1)) <= 0 || e.compareTo(phi) >= 0 || !e.gcd(phi).equals(BigInteger.valueOf(1)));
d = e.modInverse(phi);
//Convert message to byte array and then to a BigInteger
BigInteger message = new BigInteger("Hello World! - From Rosetta Code".getBytes());
BigInteger cipherText = message.modPow(e, n);
BigInteger decryptedText = cipherText.modPow(d, n);
System.out.println("Message: " + message);
System.out.println("Prime 1: " + p);
System.out.println("Prime 2: " + q);
System.out.println("Phi p1 * p2: " + phi);
System.out.println("p1 * p2: " + n);
System.out.println("Public key: " + e);
System.out.println("Private key: " + d);
System.out.println("Ciphertext: " + cipherText);
System.out.println("Decrypted message(number form): " + decryptedText);
//Convert BigInteger to byte array then to String
System.out.println("Decrypted message(string): " + new String(decryptedText.toByteArray()));
}
}
{{out}}
<pre>
Message: 32745724963520459128167607565116331713761641910444445962992228853365120918629
Prime 1: 19236082984974163990952162748173714068049389803543876908591023542434008581726901827775282361343539027357627272183474081300821830594665417249999421476471444612045376319151307819458083226682409477411524022807762467877933511178646356355356721278159329226472299570078614830307335816363768128995044913560110586093645150502887099142708810425659608200567299323058631080348801201371891024721397456687923823598994147721120698707559677560198153919129668778208924015134166710369131053236474139106999881687335966483688956493993503452781613364724067448118712996621139377805320411264206346354690147839668674568385780953439324061827
Prime 2: 17979329033868078796981009025342087899891110883452413861085335446537232859657584956199529432436753743100057017453176958641563538906032576785591580481524960405796753482749211728790206586594224271803757384485305551705171358489679998051039658612346599741622638134691446099105450988669667758655426868708523266671158484337114750111466361420411180131539156350206088712722069263680478398363920580976741655559629459204726096055804150577131670026585406567505359058210528861172448235138919989250928848721850118962177309953951127503325970279819172102106621073790956566530104217789478786634899278802600883073967446382373644250257
Phi p1 * p2: 345851865309641725173652598401242058900566653910908938475312921380066114219124630361012048649340940349701681628058518630535276222954695692233147670047674008455691941733381178283159719985229687501019788350058464636500196522521346408404768715926660411771389935137989577763538670548607053837834611583177259789432946335846775973340201166384784256821490809726897985505537384164828673170953111807028144501567927080933277305734847328999516054393161305269105612852357562678721346577533872335946654435942111678921979428611842390495534195723208415846942969477148117377158823876687230703157218761840307200229212313941422321195051355226377851222163854147510935258095666300221082823130614374400292194084084333352100386386600651725502133645428505729406848959785973133150969119775159790464605612595258587741654065352840099419921194642121733262659814580729725479879031162109276830902575494117198577188807279359692336933181285455336126206772237317229756148090589146421446485136412385682723586947367965091078676401379642267664924847358476052429919297388821038943132363371365451994109675932621652038431036384121653367832798502022879709943736316295610602665722391298109723459179009548775436875778616030750642493163185541967992915955989097200396360327456
p1 * p2: 345851865309641725173652598401242058900566653910908938475312921380066114219124630361012048649340940349701681628058518630535276222954695692233147670047674008455691941733381178283159719985229687501019788350058464636500196522521346408404768715926660411771389935137989577763538670548607053837834611583177259789432946335846775973340201166384784256821490809726897985505537384164828673170953111807028144501567927080933277305734847328999516054393161305269105612852357562678721346577533872335946654435942111678921979428611842390495534195723208415846942969477148117377158823876687230703157218761840307200229212313941422321195088570638396693464951787319284451060063606800908079113900290733389263435525468820136075198180380944495959817935065156769349234329286671127186560121733156195482447742397159107289902355166116733169136476049414801282242919450398051834285427541999782759870670431821968638118220066164725772820831757237604760059537040952069757997344764318267517273468518841355988306740438835556131045824464960305329590326517099659355766092152184867080462187317080527339823959005966347609972615672497047496190727232432065795389602582743555233621829974942652963009404343619187532820114040659804327626152774968610262473598342324536209328639539
Public key: 193579940416764762032781091221143757801413904551984303885407040026321908965197971867687112770438003048686335467825121571041736775396425178706013366881005246973351188647629560663037810331045292388828497048500842585316962463760854316963245076613682542629780184301654965436793943481122744003077197338402843407520722504896640189710671321124115630104805979401188537742997889316213029383963089439143905748656345056757946693819634971714186428073990059096062771479710217863926284515341443931092854041306087536208153864059633604334787035313177701948799425284530371404018395643074910533253129726213643193854656336232925658951811658248314007700619000559672273590950586652099230800733098520827807590026231300954320448333065980082309254977488081625340716095420728271744505284703847569933308644755417715559811180764086973488668246118187715534944871349843316776293048731714297576612062481854912918664986385922123428018736434561518771685854566894264705876397229699222060976725454055036743075458563364307745597456824543609383372429743649103452964024219143956601668222718087847554469024737873441872945480724976527827721060219946775750776694792509727838512629661637130805269017888622785492890741411798087685393616016451666522802755139926311245306229041
Private key: 219036175169203544315490853999486820834618560996816043411987210664248102884377915567272406198057873662330646577076795355640818453754512006948660586443609679011052139043660581484406998357727395227184641632029033848943507736672039637904589489626534363470520344933214278535075666039716685014892551021861132000517572948046870070641407067319704943583671179776670601874690644590228314092730047599467716558027443683253737751585758991176998516378325292822247842855150355043416320003512453741917219948896684868393696007264717697967091707113384847407621846752255898098912112528872599250195047315005253431619786863325536528182598792588704861531564144935586585480994816595663012912390628905402008731003235520424397350241617043412307761990786711273480686856614955176903059108533627807572077696388568308581773433971401736018773723311485647784158083000913638874130114372027510789163873963163280849749880619112666293979830817843596818936327354256104400652111217685523782967475741729222155996255475772715455781288940265345618022791173798222315117453828105848080873781770073614996444414329484314974671715475986173113744470587849073918357399130907446602201256421236363869748993676713228862789197394591401079130691347928057018496258825068434610372879057
Ciphertext: 751802077965963630324549365263121640277689482825128224616526073196054301029392106215761462171255492597878612184351606323296123153021421277191908278007450520130118453205040455128556435393691729542297181525367763857641211099694558032110059874156910175402903627759175935900055257215548262301358748652386044838566618270488287858766869634274195146217020325014127218881028662056032737660265341221411932659292844178938078127559747544470249139600492686861162904399444542749100026596955221740375230453344396141534329779530430928001439479395156999496805099234924399049952788417632216512939528956109220706809364166228124854558311835437452608371779980805227059349785343399382366770619320980575920634550115026075376299109862825119723058827075167366432962208711425854647134395509013075755502864140908914499073909257771585668395445413050111450415828406012614877021632277223689796818643848597729370856433117827304201297547912764020996146635163811573223631120712910083451904964228318515972103019499763177402019351409722785807448842923761649884358555226283418059099812653679780193380193208207168410802721122329377467373427784070234188000453256581358361888268040964222190110448131244743936030485464773996914221120717918807573232494076059754978713933
Decrypted message(number form): 32745724963520459128167607565116331713761641910444445962992228853365120918629
Decrypted message(string): Hello World! - From Rosetta Code
</pre>
</lang>
</lang>