【发布时间】:2015-04-04 17:45:34
【问题描述】:
我已经有几年没有用 Java 做过任何事情了。但我正在尝试从头开始编写一个计算 S-DES 加密和解密的程序。我正在在线查看当前代码,只是为了获得有关如何设置程序的帮助。但我不确定原作者想要做什么......
这是一段代码..再次它不是我的,我没有复制它,我只是想了解他们在这里做什么?
public class SDESProject {
public static void main( String args[]) throws Exception
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please Enter the 10 Bit Key :");
int K = Integer.parseInt(keyboard.nextLine(),2);
SDES A = new SDES( K);
System.out.println("Enter the 8 Bit Plaintext : ");
int m = Integer.parseInt(keyboard.nextLine(),2);
System.out.print("\nKey K1: ");
SDES.printData( A.K1, 8);
System.out.print("\nKey K2: ");
SDES.printData( A.K2, 8);
m = A.encrypt( m);
System.out.print("\nEncrypted Message: ");
SDES.printData( m, 8);
m = A.decrypt( m);
System.out.print("\nDecrypted Message: ");
SDES.printData( m, 8);
keyboard.close();
}
}
class SDES
{
public int K1, K2;
public static final int P10[] = { 3, 5, 2, 7, 4, 10, 1, 9, 8, 6};
public static final int P10max = 10;
public static final int P8[] = { 6, 3, 7, 4, 8, 5, 10, 9};
public static final int P8max = 10;
public static final int P4[] = { 2, 4, 3, 1};
public static final int P4max = 4;
public static final int IP[] = { 2, 6, 3, 1, 4, 8, 5, 7};
public static final int IPmax = 8;
public static final int IPI[] = { 4, 1, 3, 5, 7, 2, 8, 6};
public static final int IPImax = 8;
public static final int EP[] = { 4, 1, 2, 3, 2, 3, 4, 1};
public static final int EPmax = 4;
public static final int S0[][] = {{ 1, 0, 3, 2},{ 3, 2, 1, 0},{ 0, 2, 1,
3},{ 3, 1, 3, 2}};
public static final int S1[][] = {{ 0, 1, 2, 3},{ 2, 0, 1, 3},{ 3, 0, 1,
2},{ 2, 1, 0, 3}};
public static int permute( int x, int p[], int pmax)
{
int y = 0;
for( int i = 0; i < p.length; ++i)
{
y <<= 1;
y |= (x >> (pmax - p[i])) & 1;
}
return y;
}
public static int F( int R, int K)
{
int t = permute( R, EP, EPmax) ^ K;
int t0 = (t >> 4) & 0xF;
int t1 = t & 0xF;
t0 = S0[ ((t0 & 0x8) >> 2) | (t0 & 1) ][ (t0 >> 1) & 0x3 ];
t1 = S1[ ((t1 & 0x8) >> 2) | (t1 & 1) ][ (t1 >> 1) & 0x3 ];
t = permute( (t0 << 2) | t1, P4, P4max);
return t;
}
【问题讨论】:
-
查看 java 中的移位运算符 - docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
标签: java