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);
}
}
}