tutorials.de Buch-Aktion 02/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
1279
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    spliff spliff ist offline Grünschnabel
    Registriert seit
    Aug 2003
    Ort
    Wien
    Beiträge
    3
    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 :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    
    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
     

  2. #2
    spliff spliff ist offline Grünschnabel
    Registriert seit
    Aug 2003
    Ort
    Wien
    Beiträge
    3
    Das Problem lag wohl an den int-Castings.
    So sollte es funktionieren.

    Michael
    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    
    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);
        }
    }
     

  3. #3
    Fulk Fulk ist offline Mitglied Bronze
    Registriert seit
    Jan 2008
    Beiträge
    48
    Oh, das wollte ich auch schonmal machen.

    Danke für Dein posting!
     

Ähnliche Themen

  1. sha-256 Algorithmus lib ?
    Von Anime-Otaku im Forum Algorithmen & Datenstrukturen mit Java
    Antworten: 3
    Letzter Beitrag: 18.12.06, 13:15
  2. Was ist Algorithmus?
    Von SMoeller im Forum Java
    Antworten: 1
    Letzter Beitrag: 28.09.05, 20:42
  3. Algorithmus für AVG
    Von FireFlow im Forum C/C++
    Antworten: 6
    Letzter Beitrag: 23.01.05, 21:43
  4. DES-Algorithmus...
    Von DoRiMaN im Forum Coders Talk
    Antworten: 7
    Letzter Beitrag: 29.08.04, 12:15
  5. Algorithmus
    Von Husky im Forum PHP
    Antworten: 19
    Letzter Beitrag: 10.10.01, 22:10