I'm a German student and 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.)
我是德国学生,对于计算机类,我需要用Java实现DES加密(我自己,而不是使用Java-API)并详细解释。我没有找到任何使用谷歌的Java代码示例,但我确实在C中找到了一个简单的实现。(我不知道C,我知道一点点C ++,但不是那么好,指针仍然偶尔得到我。 )
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 the example using:
所以我试着简单地将代码从C转换为Java,这在大约中途完成了,但是我在最后一部分遇到了问题,特别是使用的示例:
printf("%c",M);
Which, from what Google told me, seems to be 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.
从Google告诉我的,似乎是将数字(整数)转换为ASCII字符,但我并不确定。我的代码似乎一直工作到最后一部分,所以我会感谢任何可以给我修复/提示的人。
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);
}
}
}
How can I convert the
我怎样才能转换
printf("%c",M);
to Java, and is this then a full DES implementation?
到Java,这是一个完整的DES实现吗?
4 个解决方案
#1
Its late and I'm tired but this isn't a DES implementation, is it?
它已经晚了我累了,但这不是DES实现,是吗?
It looks more like RSA.
它看起来更像RSA。
Yeah the link on your post is an asymmetrical cryptosystem. DES is symmetrical.
是的,你的帖子上的链接是一个不对称的密码系统。 DES是对称的。
#2
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).
这假设M是一个数值,表示0-127的ASCII字符(或0-255的ISO 8859-1字符,或0-65535的Unicode字符)。
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)).
它还假设输出是PrintWriter或PrintStream,其中一个是System.out(FileOutputStream很容易被PrintOutputStream或PrintWriter(new OutputStreamWriter)包装)。
#3
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.
Des实际上是一个相当艰难的算法,让你的头脑。我正要写博客,但是当我挖掘时。我无法把它分解成我满意的碎片。
Here is how very roughtly DES works.
以下是DES的工作方式。
- Choose a 64bit key. (throw away 8bits)
- For each 64bit block split in half
- Do some xor based perumutation based on sub-keys genererated and do the same to the other half.
- Do some substitution.
- Do more permutatation
选择64位密钥。 (丢掉8位)
每个64位块分成两半
基于生成的子密钥进行一些基于xor的perumutation,并对另一半执行相同的操作。
做一些替代。
做更多的permutatation
to Decrypt run in reverse number.
解密以反向编号运行。
*this is a gross simplification.
*这是一个粗略的简化。
#4
this program is for RSA algorithm and not for DES. DES code is 8 times larger than RSA :)
此程序适用于RSA算法,不适用于DES。 DES代码比RSA大8倍:)
#1
Its late and I'm tired but this isn't a DES implementation, is it?
它已经晚了我累了,但这不是DES实现,是吗?
It looks more like RSA.
它看起来更像RSA。
Yeah the link on your post is an asymmetrical cryptosystem. DES is symmetrical.
是的,你的帖子上的链接是一个不对称的密码系统。 DES是对称的。
#2
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).
这假设M是一个数值,表示0-127的ASCII字符(或0-255的ISO 8859-1字符,或0-65535的Unicode字符)。
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)).
它还假设输出是PrintWriter或PrintStream,其中一个是System.out(FileOutputStream很容易被PrintOutputStream或PrintWriter(new OutputStreamWriter)包装)。
#3
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.
Des实际上是一个相当艰难的算法,让你的头脑。我正要写博客,但是当我挖掘时。我无法把它分解成我满意的碎片。
Here is how very roughtly DES works.
以下是DES的工作方式。
- Choose a 64bit key. (throw away 8bits)
- For each 64bit block split in half
- Do some xor based perumutation based on sub-keys genererated and do the same to the other half.
- Do some substitution.
- Do more permutatation
选择64位密钥。 (丢掉8位)
每个64位块分成两半
基于生成的子密钥进行一些基于xor的perumutation,并对另一半执行相同的操作。
做一些替代。
做更多的permutatation
to Decrypt run in reverse number.
解密以反向编号运行。
*this is a gross simplification.
*这是一个粗略的简化。
#4
this program is for RSA algorithm and not for DES. DES code is 8 times larger than RSA :)
此程序适用于RSA算法,不适用于DES。 DES代码比RSA大8倍:)