RSA Algorithmus

spliff

Grünschnabel
Hallo,

Ich habe in einem kleinem Programm den RSA-Algorithmus programmiert.
Nur erfolgt die Dekodierung nicht so wie erwünscht.
Ich codierte die Zahl (2), beim decodieren wird jedoch eine falsch Zahl zurückgelifert (1).

Kann mir bitte jemand erklären, wo mein Denkfehler liegt.

Danke
Michael

Code:
import java.math.BigInteger;

public class RSAAlgorit {

    public static void main(String[] args) {

        String ps = "7";    //"307";
        String qs = "11";   //"859"
        String es = "13";   //"1721";
        int type_id = 2;  //"1027";

        // erste Primzahl
        BigInteger p = new BigInteger(ps);
        // zweite Primzahl
        BigInteger q = new BigInteger(qs);
        // Modulus
        BigInteger n = p.multiply(q);
        
        // öffentlicher Exponent - teilerfremd zu (p-1)(q-1) zufällig
        BigInteger e = new BigInteger(es);

        // geheime Exponent - d = e-1 mod(p - 1)(q -1)
        BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)); // (p- 1) * (q - 1);
        BigInteger d = e.modInverse(phi);

        System.out.println("p=" + p + "\nq=" + q + "\nn=" + n + "\nphi=" + phi + "\nd=" +d + "\ne=" +e);// + "\nx=" + x);// + "\nd=" + d);
        System.out.println("--------------------");

        // Encrypt ->   enc = type_id^e mod n
        System.out.println("type_id=" + type_id);
        int type_id_encr = (int)Math.pow(type_id, e.intValue()) % n.intValue();
        System.out.println("type_id_encr=" + type_id_encr);

        // Decrypt ->   dec = type_id_encr^d mod n
        int type_id_decr = (int)Math.pow(type_id_encr, d.intValue()) % n.intValue();
        System.out.println("type_id_decr=" + type_id_decr);
    }
}

Ausgabe:
p=7
q=11
n=77
phi=60
d=37
e=13
--------------------
type_id=2
type_id_encr=30
type_id_decr=1
 
Das Problem lag wohl an den int-Castings.
So sollte es funktionieren.

Michael
Code:
import java.math.BigInteger;
import java.util.Random;

public class RSAAlgorit {

    public static void main(String[] args) {

        long ps = 307;    //"307";
        long qs = 293;   //"859"
        long es = 101;   //"1721";
        long tIDs = 127;  //"1027";

        BigInteger type_id = BigInteger.valueOf(tIDs);
        // erste Primzahl
        BigInteger p = BigInteger.valueOf(ps);
        // zweite Primzahl
        BigInteger q = BigInteger.valueOf(qs);
        // Modulus
        BigInteger n = p.multiply(q);

        // öffentlicher Exponent - teilerfremd zu (p-1)(q-1) zufällig
        BigInteger e = BigInteger.valueOf(es);

        // geheime Exponent - d = e-1 mod(p - 1)(q -1)
        BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)); // (p- 1) * (q - 1);
        BigInteger d = e.modInverse(phi);

        System.out.println("p=" + p + "\nq=" + q + "\nn=" + n + "\nphi=" + phi + "\nd=" +d + "\ne=" +e);// + "\nx=" + x);// + "\nd=" + d);
        System.out.println("--------------------");

        // Encrypt ->   enc = type_id^e mod n
        System.out.println("type_id=" + type_id);
        BigInteger type_id_encr = type_id.pow(e.intValue()).mod(n);
        System.out.println("type_id_encr=" + type_id_encr);

        // Decrypt ->   dec = type_id_encr^d mod n
        BigInteger type_id_decr = type_id_encr.pow(d.intValue()).mod(n);
        System.out.println("type_id_decr=" + type_id_decr);
    }
}
 
Zurück