57 LAMPIRAN A : LISTING PROGRAM /* * Source Code Program Algoritma Kombinasi RSA dan ElGamal * oleh : * Nama : Hasnan Aulia Haq * NIM : 100823019 * Prodi : Ekstensi Matematika * Jurusan : Matematika FMIPA USU 2010 * */ package rsaelgamal; import java.math.biginteger; import java.util.random; import java.io.*; public class Kombinasi { private BigInteger p; private BigInteger q; private BigInteger N; private BigInteger g; private BigInteger phi; private BigInteger x; private BigInteger e; private BigInteger y; private BigInteger d; private BigInteger c; public static BigInteger ZERO = BigInteger.ZERO; public static BigInteger ONE = new BigInteger ("1"); public static BigInteger TWO = new BigInteger ("2"); private BigInteger batas = new BigInteger ("256"); private Random r; public Kombinasi() { r = new Random(); p = PrimaAman(batas); q = PrimaAman(batas); while (p.compareto(q)==0){ p = PrimaAman(batas); q = PrimaAman(batas); N = p.multiply(q); g = Primitif (N); e = PrimaAman(batas.divide(TWO)); x = PrimaAman(batas.divide(TWO));
58 phi = p.subtract(biginteger.one).multiply (q.subtract(biginteger.one)); while (N.gcd(e).compareTo(BigInteger.ONE) == 1 && e.compareto(n) == 1 ) { e.add(biginteger.one); while (N.gcd(x).compareTo(BigInteger.ONE) == 1 && x.compareto(n) == 1 ) { x.add(biginteger.one); while (N.gcd(g).compareTo(BigInteger.ONE) == 1 && g.compareto(n) == 1 ) { g.add(biginteger.one); d = e.modinverse(phi); y = g.modpow(x, N); public static void header (){ System.out.println ("***************************************"); System.out.println ("* Program Pesan Rahasia *"); System.out.println ("* Dgn Algoritma Kombinasi RSA-ElGamal *"); System.out.println("* *"); System.out.println ("* Copyright (c) 2012 Hasnan Aulia Haq *"); System.out.println ("* Program Studi Ekstensi Matematika FMIPA USU *"); System.out.println ("*************************************** ); System.out.println (""); System.out.println (""); public static int CekPrima(BigInteger a){ int cekprima; BigInteger b,c; b=one; do{ b=b.add(one); c=a.mod(b); while (c.compareto(zero)>0); if (a.compareto(b)==0) cekprima=1; else cekprima=0; return cekprima;
59 public static BigInteger PrimaAman(BigInteger batas){ BigInteger rand; Random r= new Random(); do{ rand = new BigInteger(10,r); while (rand.compareto(batas) == 1); BigInteger p; int cekprima; do{ int cekpri; do { rand=rand.add(one); cekpri = CekPrima(rand); while (cekpri==0); p = (TWO.multiply(rand).add(ONE)); cekprima=cekprima(p); while (cekprima==0); return p; public static int CekPrimitif (BigInteger alpha, BigInteger p){ int cekprimitif; BigInteger b,q; q = (p.subtract(one).divide(two)); b = alpha.modpow(two, p); if (b.compareto(one)==0){ cekprimitif = 0; else { b= alpha.modpow(q, p); if (b.compareto(one)==0){ cekprimitif = 0; else { cekprimitif = 1; return cekprimitif; public static BigInteger Primitif(BigInteger p) { BigInteger alfa; Random r= new Random(); BigInteger nilaik; do {
60 do{ nilaik = new BigInteger(10,r); while (nilaik.compareto(p) == 1); alfa = nilaik.add(one); while (CekPrimitif(alfa,p) < 1); return alfa; public static void cetakkunci(biginteger N, BigInteger e, BigInteger x, BigInteger d, BigInteger y, BigInteger g){ System.out.println("Kunci Publik (N, e, x ) = (" + N +", "+ e +", "+ x +")"); System.out.println("Kunci Privat (d,y) = (" + d +", "+ y +")"); System.out.println("Bilangan acak g = " + g); // for mencetak ascii public static void stringkeascii (byte[] m){ System.out.println("Plainteks :"); for (int i=0; i<m.length;i++){ System.out.println ("\tm"+i+" = " +m [i]); public static void cetakchiper (BigInteger [][] a){ System.out.println("Chiperteks (c1,c2) :"); for (int i=0; i<a.length;i++){ System.out.println("\tm"+i+"("+ a [i][0]+ ","+ a [i][1]+")"); public static void cetakdekripsi (BigInteger [] dekripsi){ System.out.println("Hasil Dekripsi :"); for (int i=0; i<dekripsi.length;i++){ System.out.println ("\tm"+i+" = "+dekripsi[i]+" "); System.out.print("Pesan Asli = "); for (int i=0; i<dekripsi.length;i++){ String xs=string.valueof(dekripsi[i]); int x = Integer.valueOf(xS); System.out.print((char) x);
61 public BigInteger [][] Enkripsi (byte[] m, BigInteger y, BigInteger e,biginteger N,BigInteger g){ BigInteger [][] chiper = new BigInteger [m.length][2]; // For Enkripsi for (int i=0; i<m.length;i++){ // mendapatkan BigInteger m BigInteger me = new BigInteger(String.valueOf(m[i])); // mendapatkan nilai acak c c = PrimaAman(batas.divide(TWO)); // mencari nilai minus c BigInteger minc = c.negate(); chiper [i][0] = ((me.multiply(y.modpow(minc, N))).modPow(e, N).mod(N)); chiper [i][1] = g.modpow(c, N); return chiper; private BigInteger[] dekripsi (BigInteger[][] chiper1, BigInteger d, BigInteger x, BigInteger N) { BigInteger[] dekripsi = new BigInteger [chiper1.length]; // For Dekripsi for (int i=0; i<chiper1.length;i++){ BigInteger a = chiper1 [i][0]; BigInteger b = chiper1 [i][1]; dekripsi [i] = ((a.modpow(d, N).multiply(b.modPow(x, N))).mod(N)); return dekripsi; public static void main (String[] args) throws IOException { Kombinasi Kombinasi = new Kombinasi(); BigInteger p = Kombinasi.p; BigInteger q = Kombinasi.q; BigInteger N = Kombinasi.N; BigInteger g = Kombinasi.g; BigInteger e = Kombinasi.e; BigInteger x = Kombinasi.x; BigInteger phi = Kombinasi.phi;
62 BigInteger d = Kombinasi.d; BigInteger y = Kombinasi.y; DataInputStream in=new DataInputStream(System.in); String teststring; header (); cetakkunci(n,e,x,d,y,g); // memasukkan plaintext System.out.println("Masukkan pesan yang akan di enkripsi :"); System.out.print("Pesan = "); teststring=in.readline(); byte [] m = teststring.getbytes(); System.out.println("Jumlah Plainteks " + m.length+ " huruf"); // mencetak nilai ascii plainteks stringkeascii(m); // Enkripsi BigInteger [][] chiper = Kombinasi.Enkripsi(m,y,e,N,g); cetakchiper(chiper); // DEKRIPSI BigInteger [] dekripsi = Kombinasi.dekripsi(chiper, d, x, N); // mencetak hasil Dekripsi cetakdekripsi(dekripsi);
63 LAMPIRAN B : TABEL ASCII KODE ASCII ( 0 127 )
64 KODE ASCII Extendeed ( 128 255 )