You are on page 1of 7
RSA java \der, choose License Headers in Project Properties. this template file, choose Tools | Templates nn the template in the editor Ween 6 package rsa; 7 8 import java.math BigInteger; import java.util. LinkedList; 10 import java.util.Random; i 12 13 * Kelas RSA 14 * Implementasi sistem sandi RSA 15 * @author 16 * 17 public class RSA { 18 19 20 * PembangkitKunci 21 * @param panjang_n: panjang bit Sn$ yang ingin dibangkitkan 22 * @return KuneiPublik dan KunciPriva 2B 24 public LinkedList pembangkitKunei(int panjang_n){ 25 in 3 26 BigInteger p = null,q = null,n = null,e = null,d = null, phi_n = null; 27 Biglnteger{] pq = pilihPQ(panjang_n); 28 29 30 31 pal]; 32 n=p.multiply(q); 33 d=RSA.inversPerkalian(e,phi_n); 34 KunciPublikRSA kunciPublik = new KunciPublikRSA(n,e); 35 obj.add(kunciPublik); 36 KunciPrivatRSA kuneiPrivat = new KunciPrivatRSA(d); 37 obj.add(kuneiPrivat); 38 return obj; 39} 40 4 oe 42 * fungsi pilihPQ 43 © @param panjang_n : panjang bit n 44 * @return ret[0] =p, ret{I] = 4 45 46 protected BigInteger{] pilihPQ(int panjang_n) { 47 boolean ujiKriteria = false; 48 49 Random r= new Random(); 50 temukan p dan q yang memenuhi kriteria keamanan rsa 51 Bighnteger{] ret = new Biginteger(4]; 52 Biglnteger p= null,q = null,phi_n = null,e = null; 53 while (ujiKriteria = false) { 34 p= new Biginteger(panjang_n/2,10,t); 55 q = new BigInteger(panjang_n/2,10,r); 56 37 58 59 60, 61 62 63 65 66 67 68 69 70 n nD 73 74 15 16 1 18 79 80 81 82 83 85 86 87 88 89 90 a1 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 MW 112 113 Integer pl Biginteger ql >. subtract(Biginteger. ONE) divide(BigInteger.valueOf2)); .subtract(Biginteger.ONE) divide(BigInteger.valueOf(2)); if (pI .ged(p).compareTo(Biginteger. ONE) != 0) continue; if (ql. ged(q).compareTo(Biginteger.O.E) != 0) continue; BigInteger p_minus_1 = psubtract(Biginteger. ONE); BigInteger q_minus_I = q.subtract(Biginteger. ONE), phi_n=p_minus_1.multiply(q_minus_1); BigInteger ged = p_minus_l.ged(q_minus_1); ujiKriteria = gcd.compareTo(BigInteger. TEN)< 0; if (ujiKriteria) continue; e= new Bighnteger(r.nextInt(panjang_n),1); ged = phi_n.ged(e); if (ged.compareTo(BigInteger. ONE) != 0) { ujiKriteria = false; continue; Fungsi Enkripsi @param p eks asli am kuneiPublik : kunci publik RSA @return teks sandi public byte{] enkripsi(byte[] p, KunciPublikRSA kunciPublik) { BigInteger bigP = new BigInteger(1,p); C= Prfe} bigP = bigP:modPow(kunciPublik.getEQ, kuneiPublik.getN()); return bigP.toByteArray(); $ Fungsi dekripsi RSA ne reks sand n kuneiPrivat : kunci Privat RSA @return ks asli public byte{] dekripsi(bytc[] c, KunciPrivatRSA kuneiPrivat, KunciPublikRSA kuncipublik) { BigInteger bigC = new Biginteger(1,c); n=p*q BigInteger bigN = kuncipublik.getNQ; 114 1s 116 117, 118 119) 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143, 144 145 146 147 148, 149) 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 } mod igC.modPow(kuneiPrivat.getD(), bigN); bigC.toByteArray(); P bigC return } public static Biginteger inversPerkalian(BigInteger c, BigInteger phi_n) { Biginteger A = phi_n, B=e; BigInteger q = null; BigInteger r= null; Biginteger S1 = Biglnteger.ONE,S2 = BigInteger. ZEROS = null; Biginteger TI ~ BigInteger. ZERO, T2 = BigInteger. OVE, T = null; while (B.compareTo(Biginteger. ZERO) != 0) { BigInteger{] res = A.divideAndRemainder(B); 4= res{0]; r=res[i]; T =T1.subtract(q.multiply(T2)); Tl = 72; T2=T; } if (TL.signum() = -1) return phi_n.add(T1); return TI; $ public static void main(String[] args) { int panjang_n = 128; buat objek RSA RSA rsa = new RSAQ; panggil pembangkit kune LinkedList kunciRSA = rsa.pembangkitKunci(panjang_n); System.our.printin("Kunci Publik \n" + kunciRSA.get(0)); System.out.printin("Kunci Privat \n" + kunciRSA.get(1)); KunciPublikRSA kunciPublik = (KunciPublikRSA)kunciRSA.get(0); KunciPrivatRSA kuneiPrivat = (KuneiPrivatRSA)kunciRSA. get(1); siapkan teks asli byte[] plainteks = new byte[panjang_n/8 - 1]; Random r= new Random(); rnextBytes(plainteks); Biginteger bigP = new Bignteger( ,plainteks); System.out.printIn("plainteks +" + ByteArrayUtil.getHexSiring(bigPtoByteArray())); panggil enkripsi RSA byte[] cipherteks = rsa.enkripsi(bigP-toByteArray(), kunciPublik); System.out.printIn("‘cipherteks : " + ByteArrayUtil.getHexString(cipherteks)); 172 panggil dekripsi RSA 173 plainteks = rsa.dekripsi(cipherteks, kunciPrivat,kuneiPublik); 174 System.oue,printin("plainteks :" + ByteArrayUtil,gerHexSiring(plainteks)); 15} 176} 17 KunciPublikRSA java 1 2 * To change this licen: 1, choose License Headers in Project Properties, 3 * To change this temp jose Tools 4 * and open the template itor 3° 6 package rsa; 7 8 import java.math Bighnteger; 9 10° nM 12 * @author Sena 13 14 class KunciPublikRSA { 15 _ private BigInteger n; 16 private Biginteger e; 7 18 KunciPublikRSA(Biginteger n, Biginteger e) { 19 this 20 D1 22 23. public Biginteger getN(){ 24 return n; 25} 26 27 public void setN(BigInteger n){ 28 thisn=n; 29 } 30 31 public Biginteger getE(){ 32 retuume; 33} 34 35. public void setE(Biginteger ¢){ 36 thise=e; 37} 38 39 @Override 40 public String toString(){ 41 retum"(e="+e4"," + "n="4n4")"; 42 4 4} 44 KuneiPrivatRSA java i 2 * To change this licen: t, choose License Headers in Project Properties. 3 * To change this temp }oose Tools 4 * and open the template in the editor 3° 6 package rsa; 7 8 import java.math Bighnteger; 9 10° mM 12 * @author Sena 13 14 class KunciPrivatRSA { 15 private Biginteger d; 16 17 KuneiPrivatRSA (BigInteger d) { 18 thisd=d; 19 4} 20 21 public BigInteger getD() { 22 return d; 23} 24 25 public void setD(Biginteger d){ 26 this.d 27} 28 29 @Override 30 public String toString(){ 31 retum "d="+d; 32} 33 34) 35 ByteArrayUtil,java 1 2 * To change this licen 1, choose License Headers in Project Properties. 3. * To change this temp ;oose Tools | Templates 4 * and open the template itor 5 6 package rsa; 7 8 9 « 10 * @author Sena i 12 public class ByteArrayUtil { 13 static final byte{] EX_CHAR_TABL. 14 (byte) '0', (byte) '1', (byte) '2", (byte) 15 (byte) 4, (byte) '5', (byte), (byte) "7, 16 (byte) '8', (byte), (byte)'A’, (byte) 17 (byte) 'C’, (byte) 'DY, (byte) 'E’, (byte) 'F* 18}; 19 20 static String gerHexString(byte[] raw) { 21 byte[] hex = new byte[2 * raw.length]; 22 int index = 0; 23 24 for (int i= 0; i < raw.length; i+) { 25 byte b= rawfi]; 26 int v= b & OxFF; 27 hex{index++] = HEX_CHAR_TABLE|y >>> 4]; 28 hex[index++] = HEX. CHAR_TABLE|y & OXF]; 2} 30 return new String(hex); 31} 32 33} 34

You might also like