0

I spent a day to check out a lot of combinations of this last lines of my code but I'absolutely not going to understand how i can get an encrypted Input as BigInteger and decrypt it to a String... So this is the last block of the Code:

 package rsa;
import java.io.IOException;
import static java.lang.System.exit;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class RSA {
  private BigInteger n, d, e;

  private int bitlen = 1024;

  /** Create an instance that can encrypt using someone elses public key. */
  public RSA(BigInteger newn, BigInteger newe) {
    n = newn;
    e = newe;
  }

  /** Create an instance that can both encrypt and decrypt. */
  public RSA(int bits) {
    bitlen = bits;
    SecureRandom r = new SecureRandom();
    BigInteger p = new BigInteger(bitlen / 2, 100, r);
    BigInteger q = new BigInteger(bitlen / 2, 100, r);
    n = p.multiply(q);
    BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q
        .subtract(BigInteger.ONE));
    e = new BigInteger("3");
    while (m.gcd(e).intValue() > 1) {
      e = e.add(new BigInteger("2"));
    }
    d = e.modInverse(m);
  }

  /** Encrypt the given plaintext message. */
  public synchronized String encrypt(String message) {
    return (new BigInteger(message.getBytes())).modPow(e, n).toString();
  }

  /** Encrypt the given plaintext message. */
  public synchronized BigInteger encrypt(BigInteger message) {
    return message.modPow(e, n);
  }

  /** Decrypt the given ciphertext message. */
  public synchronized String decrypt(String message) {
    return new String((new BigInteger(message)).modPow(d, n).toByteArray());
  }

  /** Decrypt the given ciphertext message. */
  public synchronized BigInteger decrypt(BigInteger message) {
    return message.modPow(d, n);
  }

  /** Generate a new public and private key set. */
  public synchronized void generateKeys() {
    SecureRandom r = new SecureRandom();
    BigInteger p = new BigInteger(bitlen / 2, 100, r);
    BigInteger q = new BigInteger(bitlen / 2, 100, r);
    n = p.multiply(q);
    BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q
        .subtract(BigInteger.ONE));
    e = new BigInteger("3");
    while (m.gcd(e).intValue() > 1) {
      e = e.add(new BigInteger("2"));
    }
    d = e.modInverse(m);
  }

  /** Return the modulus. */
  public synchronized BigInteger getN() {
    return n;
  }

  /** Return the public key. */
  public synchronized BigInteger getE() {
    return e;
  }

  /** Trivial test program. */
  public static void main(String[] args) throws InterruptedException, IOException {
    RSA rsa = new RSA(1024);
    loop:   

    /** Input  Scanner switch*/
    Scanner first = new Scanner(System.in);
    int a = first.nextInt();

    switch (a) {
        case 1:
       /** Output Text zum verschlüsseln */
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("### Text zum verschlüsseln eingeben: ###");

       /** Input Text zum verschlüsseln*/
        Scanner scanner = new Scanner(System.in);
        String plaintext = scanner.nextLine();


        /** Output eingegebenen Text*/
        System.out.println("Eingegebener Text: ");
        System.out.println("------------------------");
        System.out.println(plaintext);
        BigInteger bigplaintext = new BigInteger(plaintext.getBytes());

        /** Output */
        System.out.println();
        System.out.println();
        System.out.println();

    /** Output verschlüsselten Text*/
    BigInteger vbigplaintext = rsa.encrypt(bigplaintext);
    System.out.println("Veschlüsselter Text: ");
    System.out.println("------------------------");
    System.out.println(vbigplaintext);
     /** ---------------------------------------------------- */
            break;
        case 2:
            /** Output Text zum Entschlüsseln */
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("### Verschlüsselten Text eingeben: ###");

         /** Input verschlüsselter Text*/
        Scanner scanner3 = new Scanner(System.in);  
        String vertext = scanner3.nextLine();
        System.out.println(vertext);



        /**----Here is the Problem------*/




        BigInteger decbigtext = rsa.decrypt(vertext);
        BigInteger entbigtext = new BigInteger(decbigtext.toByteArray());


    System.out.println("Entschlüsselter Text:");
    System.out.println("------------------------");
    System.out.println("Plaintext: " + entbigtext);


            break;
        default:
            System.exit(0);
    }
  }
}

Would be nice if somebody could give me anwer how to solve this Problem. If this post already exist please move or delete and sorry for my bad english.

  • Are you required to implement this yourself? if you are just trying to do RSA encryption/decryption there are numerous libraries that implement it already – Alex May 11 '17 at 17:37
  • 1
    @xTRX16 Have you tried to use `new BigInteger(1, messageBytes)`? Please don't use this code for anything critical. This is an implementation of textbook RSA without padding which is not that secure. – Artjom B. May 11 '17 at 18:24

1 Answers1

0

You are generating a new key pair each time you run your application. Obviously you cannot decrypt with a brand new private key.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263