Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package RSAkeamanansistem;
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.math.BigInteger;
- import java.util.Random;
- /**
- *
- * @author meciwp
- */
- public class RSAKeamananSistem
- {
- private BigInteger a;
- private BigInteger b;
- private BigInteger D;
- private BigInteger phi;
- private BigInteger f;
- private BigInteger g;
- private int bitlength = 724;
- private Random R;
- public RSAKeamananSistem()
- {
- R = new Random();
- a = BigInteger.probablePrime(bitlength, R);
- b = BigInteger.probablePrime(bitlength, R);
- D = a.multiply(b);
- phi = a.subtract(BigInteger.ONE).multiply(b.subtract(BigInteger.ONE));
- f = BigInteger.probablePrime(bitlength / 2, R);
- while (phi.gcd(f).compareTo(BigInteger.ONE) > 0 && f.compareTo(phi) < 0)
- {
- f.add(BigInteger.ONE);
- }
- g = f.modInverse(phi);
- }
- public RSAKeamananSistem(BigInteger f, BigInteger g, BigInteger D)
- {
- this.f = f;
- this.g = g;
- this.D = D;
- }
- @SuppressWarnings("deprecation")
- public static void main(String[] args) throws IOException
- {
- RSAKeamananSistem rsa = new RSAKeamananSistem();
- DataInputStream in = new DataInputStream(System.in);
- String teststring;
- System.out.println("Masukan kata yang Akan di Enkripsi:");
- teststring = in.readLine();
- System.out.println("Kata Yang Anda Masukan Adalah: " + teststring);
- System.out.println("String in Bytes: "
- + bytesToString(teststring.getBytes()));
- // encrypt
- byte[] encrypted = rsa.encrypt(teststring.getBytes());
- // decrypt
- byte[] decrypted = rsa.decrypt(encrypted);
- System.out.println("Decrypting Bytes: " + bytesToString(decrypted));
- System.out.println("Decrypted String: " + new String(decrypted));
- }
- private static String bytesToString(byte[] encrypted)
- {
- String test = "";
- for (byte b : encrypted)
- {
- test += Byte.toString(b);
- }
- return test;
- }
- // Encrypt message
- public byte[] encrypt(byte[] message)
- {
- return (new BigInteger(message)).modPow(f, D).toByteArray();
- }
- // Decrypt message
- public byte[] decrypt(byte[] message)
- {
- return (new BigInteger(message)).modPow(g, D).toByteArray();
- }
- }
Add Comment
Please, Sign In to add comment