views:

685

answers:

5

Hi, I'm a German student an for computer classes I need to implement the DES-encryption in Java(by myself, not by using the Java-API) and explain it in detail. I didn't find any Java-code-examples using google, however I did find an easy implementation in C(I do not know C, I know a little C++, but not that well, pointer still get me now and then).

http://cppgm.blogspot.com/2008/01/data-encryption-standard.html

So I tried simply converting the code from C to Java, which did work out about halfway, however I'm having a problem with the last part, especially with him using:

printf("%c",M);

Which from what Google told me seems to be him converting numbers(Integer) to Ascii-Characters, but I'm not really sure. My Code seems to be working until that last part, so I would be thankful for anyone that can give me fix/hint. My Code:

import java.util.Scanner;


public class DES {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Scanner eingabe = new Scanner(System.in);

        int p, q, key2, fn, encryption_key, temp1 , temp2 , t, s =0 , privatekey1=1, b=0 , passwort_s=0, klartext;
        int[] Cipher = new int [100];
        String passwort;

        System.out.println("Enter the value of p and q");

        p = eingabe.nextInt();
        q = eingabe.nextInt();

        System.out.println(p);
        System.out.println(q);

        key2= p*q;


        fn=(p-1)*(q-1);
        System.out.println("Enter Enryption key e: ");
        encryption_key = eingabe.nextInt();

        do {

            s=(privatekey1*encryption_key)%fn;
            privatekey1++; 

        } while (s!=1); 

        privatekey1=privatekey1-1;



        System.out.println("Public Key : "+ encryption_key + ","+ key2 );
        System.out.println("Private Key: "+ privatekey1 + "," +key2 );
        System.out.println("Enter Message: ");
        passwort= eingabe.next();
        for 
        ( temp2 = 0; temp2 < passwort.length(); temp2++) 

        {
        t = passwort.charAt(temp2);
        passwort_s=1;
        for(temp1 =0 ; temp1< encryption_key ; temp1++){
        passwort_s= passwort_s*t%key2;
        }
        passwort_s= passwort_s%key2;
        System.out.println(passwort_s);

        }

        System.out.println("Enter cipher Text: ");
        for(temp1=0;temp1< passwort.length(); temp1++ )
        {
            Cipher[temp1]= eingabe.nextInt();
        }

        System.out.println("Plainttext: ");
        for(temp2 =0; temp2 < passwort.length(); temp2++);
        {
            klartext=1;

            for(temp1 =0; temp1 < privatekey1; temp1 ++){
            klartext=klartext*Cipher[temp2]%key2;
            }
            klartext=klartext%key2;
            System.out.println(klartext);


        }



    }


}
A: 

printf("%c",M);

In java you could do:

int a = 70;
char c = (char)a;
System.out.print(a);
fmsf
A: 

Or, more concisely:

output.print((char)M);

This assumes M is a numeric value representing an ASCII character from 0-127 (or an ISO 8859-1 character from 0-255, or a Unicode character from 0-65535).

It also assumes output is a PrintWriter or PrintStream, one of which is System.out (a FileOutputStream is easily wrapped by a PrintOutputStream, or PrintWriter(new OutputStreamWriter)).

Software Monkey
+2  A: 

Its late and I'm tired but this isn't a DES implementation, is it?

It looks more like RSA.

Yeah the link on your post is an asymmetrical cryptosystem. DES is symmetrical.

John Nolan
A: 

Des is actually a fairly tough algotrithm to get your head around. I was about to blog about it but when I dug down. I couldn't break it down into pieces I was happy with.

Here is how very roughtly DES works.

  1. Choose a 64bit key. (throw away 8bits)
  2. For each 64bit block split in half
  3. Do some xor based perumutation based on sub-keys genererated and do the same to the other half.
  4. Do some substitution.
  5. Do more permutatation

to Decrypt run in reverse number.

*this is a gross simplification.

John Nolan
A: 

Hey Buddy, this program is for RSA algorithm and not for DES. DES code is 8 times larger than RSA :)

Did you look at the accepted answer?
Michael Myers