package com.ambientideas.cryptography; import java.math.BigInteger; import junit.framework.Assert; import org.junit.Test; public class TestRSASimple { private final static int KEYSIZE = 2048; private final static String MESSAGE = "12312312"; private final static String CRYPTED = "758995786485513414462092706044415763437070607343662203429261018439577843693890928013327852452514451974014839358049972057312799036748216126833464426720517900014204492085183357739577587571758122840848598764020142379595659566433453528465181123563583790611205785634756704026398028570804477241684073189596173878691934466901400971883927384159829954906236048270321479363955733225502368128286312216110890542834091461439256334380148639248077809235602075133315392537047066756746262712581393963175425747359246109264010081098288352365713471860528570683746609083121591934386058616332281186820752440004674789322948149309531634915"; private final static BigInteger PRIVATE_KEY = new BigInteger("13741527684162127270601372024766479515704952866717016006536061806462687228973811857114342481181994421103118408040269747117857235430526714617128942062381056300990608834005178810844074747492810529756974749932255125643266849972716978640878864489069546468721110295071500705817212395707127528253726352516725831860811304027612858182323415731375601986423944357694886136711025709058212021961299787784799571147770580195042679993282930035793964268033750426477346827345286514166711266690624245334405375632673049854384997139668310011254301859632818152515781019376875579337535112195971618971933143827809693022214180663940519338201"); private final static BigInteger PUBLIC_KEY = new BigInteger("65537"); private final static BigInteger MODULUS = new BigInteger("23282192803622795039771518791838907164260373207156823195376383304727001187282043061960721366800867825956802334679433272585067853427016604944618274500200803671001823405656456754641506856762656696793848509250283192504919199262220537969216880174249654014130332861304023726303421544878829834263965873811138823781027976614198111300774021907268969136400821685314313956874696882245598598628555704897318179184652208293867197598462007838672712613453973945380057834251810736186557153147137905152661475534942783707778761439917089073123466787824190391834268379546200448473920221476547456087266322689174990767092477971262043720639"); @Test public void test2048HomegrownRSAWithRandomKeys() { RSASimple rsa = new RSASimple(KEYSIZE); BigInteger message = new BigInteger(MESSAGE); System.out.println("Plaintext: " + message); BigInteger encrypted = rsa.encrypt(message); System.out.println("Encrypted: " + encrypted); BigInteger decrypted = rsa.decrypt(encrypted); System.out.println("Decrypted: " + decrypted); Assert.assertEquals(MESSAGE, decrypted.toString()); } @Test public void showKeys() { RSASimple rsa = new RSASimple(KEYSIZE); System.out.println("Private: " + rsa.getPrivateKey() ); System.out.println("Public : " + rsa.getPublicKey() ); System.out.println("Modulus: " + rsa.getModulus() ); } @Test public void test2048HomegrownRSAWithKnownKeys() { RSASimple rsa = new RSASimple(KEYSIZE); rsa.setKeys(PUBLIC_KEY, PRIVATE_KEY, MODULUS); BigInteger message = new BigInteger(MESSAGE); BigInteger encrypted = rsa.encrypt(message); Assert.assertEquals(CRYPTED, encrypted.toString()); BigInteger decrypted = rsa.decrypt(encrypted); Assert.assertEquals(MESSAGE, decrypted.toString()); } }