130 lines
7.6 KiB
Java
130 lines
7.6 KiB
Java
/**
|
|
*
|
|
* Beschreibung
|
|
*
|
|
* @version 1.0 vom 14.01.2016
|
|
* @author
|
|
*/
|
|
|
|
import java.io.IOException;
|
|
import java.math.BigInteger;
|
|
import java.util.Random;
|
|
|
|
public class RSA {
|
|
|
|
// Anfang Attribute
|
|
private int SIZE = 1024;
|
|
private int BLOCKSIZE = 2;
|
|
private BigInteger n;
|
|
private BigInteger p, q;
|
|
private BigInteger PhiN;
|
|
private BigInteger e, d;
|
|
|
|
// Ende Attribute
|
|
|
|
public RSA() {
|
|
|
|
generateKey();
|
|
}
|
|
|
|
public RSA(int bits) {
|
|
SIZE = bits;
|
|
generateKey();
|
|
}
|
|
|
|
// Anfang Methoden
|
|
|
|
public BigInteger getN() {
|
|
return n;
|
|
}
|
|
|
|
|
|
public void setN(BigInteger n) {
|
|
this.n = n;
|
|
}
|
|
|
|
public void generateKey() {
|
|
/* Step 1: Select two large prime numbers. Say p and q. */
|
|
p = BigInteger.probablePrime(SIZE, new Random());
|
|
q = BigInteger.probablePrime(SIZE, new Random());
|
|
/* Step 2: Calculate n = p.q */
|
|
n = p.multiply(q);
|
|
/* Step 3: Calculate ø(n) = (p - 1).(q - 1) */
|
|
PhiN = p.subtract(BigInteger.valueOf(1));
|
|
PhiN = PhiN.multiply(q.subtract(BigInteger.valueOf(1)));
|
|
/* Step 4: Find e such that gcd(e, ø(n)) = 1 ; 1 < e < ø(n) */
|
|
/* do {
|
|
e = new BigInteger(2 * SIZE, new Random());
|
|
} while ((e.compareTo(PhiN) != 1)
|
|
|| (e.gcd(PhiN).compareTo(BigInteger.valueOf(1)) != 0));
|
|
*/
|
|
/* Als e wird in der Praxis immer 65537 verwendet*/
|
|
e = BigInteger.valueOf(65537);
|
|
/* Step 5: Calculate d such that e.d = 1 (mod ø(n)) */
|
|
d = e.modInverse(PhiN);
|
|
}
|
|
|
|
|
|
|
|
|
|
public Key getPublicKey() {
|
|
return new Key("Ö",e,n);
|
|
}
|
|
|
|
public Key getPrivateKey() {
|
|
return new Key("P",d,n);
|
|
}
|
|
|
|
public static Key getPublicKeyZert() {
|
|
return new Key("ZS",new BigInteger("92020722032510237098687730821345002475852926519687505393637258322619905644760889379395080699744916048107242925967027699181978133434386836838764269376892530783630654811456718501622805"+
|
|
"0429691604569763800205767093030273564571085665400962405818664165449303338714877264756844232275146244572923302600861123085509774896697571445612708416287450226532029033236047907922857453176065518750862719489230457831441"+
|
|
"7173388889137991463283210462111428501392067084189267999492753829846440287839192599579471258088572439889541316908031255266831632514679577179417418047234941358616454031601382865835722476368628684965351829298264441751183"+
|
|
"4765538945638034868762252381408904028140439409187378031978842476147164936819798249655054427494851690069200746638913551812853454469432164195272770583598350122258729847670066946154048177957396760843403465486329401836814"+
|
|
"9990639814827911842782103318445952572226732629881400041393595241900643703238957157012120488385905509895908742240747739986701990654721595793098375922165943034668657422624955521278896354123265004069921640297875759614496"+
|
|
"742155179442639630932330086817709676636648111414906681085732877503585719166804978513181384327884382400889164642851924835156763044152821991745397448110186145769503874106711784628283015"),
|
|
new BigInteger("5139848029926993084030177297754816256127765376419362760006875069769259661872914759939556724394537974685918407976961929857609117001554132888941990323118389734140650301936109978057312726558318809808688530"+
|
|
"99773725611495268508136211646337118765334983074807622287602613120954964306249607670374637031866390157648764929632399710705078606495073542059188462314599000331802944594967393118351191103705051281495305712977081775868511"+
|
|
"76669514447588704897250963384817831024230705289577095855693680350648450932456258694411608347436126446258894555954026134724123826545582729849892381402885181473492517034956094364025058973837607228431208754049905489662832"+
|
|
"09739142470309153125447402018234554037154608185773467018511970065694249410104669788842146660693780223399943991335490758657838309774214025815008528258836495746672516751271291457802304100684624522436687990982028305751185"+
|
|
"48926383285704725365240957188638904019694020065564925826265319075541291979464737277137720345398870073684194532135223107690328725385189503977019793221936587592861157513836453534572733867521266351697630143131367971098119"+
|
|
"501251155238775343770355444640289263220749122642671050995480950737812740946324944965924224677968141284858329942830891138365556546396148300565405098100850501841"));
|
|
}
|
|
public static Key getPrivateKeyZert() {
|
|
return new Key("ZSP", new BigInteger("88405792460374902093708496634850741322105940917351683868541504012830882621245768251091372013156643395250448929783142177860165402492647945415104962343917444383622605496273629917298"+
|
|
"610236655119004745619089104384026215301496147234635587743965575319499761064848213230757151103779005504514500023252356175847116728347255176088139236137082826628487749466300296767988898076628975938940677076738206736367"+
|
|
"124996756783169958055053177519677571981271713255438344078311774536073940876121321362816462939120383991608570431036543882874480977769541084535126636925352742738338245898199820269860018114420772543302483230833480292931"+
|
|
"352334730109447074945409204173176619699849050721926499420093861285836519963502042472293474805213779251720392591031533706581969617690636531135793143059665641797414237435624262623024366515969141242459029652395794721850"+
|
|
"8436689537023284104630131792447652257617740873015159381631615655581919939509498869888015294276578911842474036086993469464623230276020243502751167826145879239288526145360234428912097606211745547385659522887117076882491"+
|
|
"04675147998869220229602216434114268798658103586693108736443392251539998599980668517520134276984180168498165513964507909008004400364235467465743076136818915438381335591270604629182883277111"),
|
|
new BigInteger("5139848029926993084030177297754816256127765376419362760006875069769259661872914759939556724394537974685918407976961929857609117001554132888941990323118389734140650301936109978057312726558318809808688530"+
|
|
"99773725611495268508136211646337118765334983074807622287602613120954964306249607670374637031866390157648764929632399710705078606495073542059188462314599000331802944594967393118351191103705051281495305712977081775868511"+
|
|
"76669514447588704897250963384817831024230705289577095855693680350648450932456258694411608347436126446258894555954026134724123826545582729849892381402885181473492517034956094364025058973837607228431208754049905489662832"+
|
|
"09739142470309153125447402018234554037154608185773467018511970065694249410104669788842146660693780223399943991335490758657838309774214025815008528258836495746672516751271291457802304100684624522436687990982028305751185"+
|
|
"48926383285704725365240957188638904019694020065564925826265319075541291979464737277137720345398870073684194532135223107690328725385189503977019793221936587592861157513836453534572733867521266351697630143131367971098119"+
|
|
"501251155238775343770355444640289263220749122642671050995480950737812740946324944965924224677968141284858329942830891138365556546396148300565405098100850501841"));
|
|
}
|
|
|
|
// Ende Methoden
|
|
public static void main(String[] args) {
|
|
RSA r = new RSA(2048);
|
|
Key o = r.getPublicKey();
|
|
Key p = r.getPrivateKey();
|
|
|
|
/*BigInteger i = new BigInteger("2237891328923");
|
|
System.out.println(i.toString());
|
|
byte[] b = o.convert(i);
|
|
BigInteger j = o.convert(b);
|
|
System.out.println(j.toString());
|
|
|
|
System.out.println(new String(o.encrypt(("Hallo").getBytes())));
|
|
System.out.println("------------");
|
|
System.out.println(new String(p.encrypt(o.encrypt(("Hallo").getBytes()))));
|
|
*/
|
|
System.out.println(o.getE());
|
|
System.out.println("---");
|
|
System.out.println(p.getE());
|
|
System.out.println("---");
|
|
System.out.println(o.getN());
|
|
|
|
}
|
|
} // end of RSA
|