2015-04-04 115 views
-5

自从我用Java做了什么以来,这已经过去了几年。但我正试图编写一个计算S-DES加密和解密的程序。我在网上查看当前的代码,只是为了帮助我设置我的程序。但我不知道原作者是试图做...在Java中,>>或<<是什么意思?

这里是一块code..again的它不是我的,&我不是复制它,我只是想明白他们在这里做什么?

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; 

} 
+0

请参阅java中的移位运算符 - https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html – Razib 2015-04-04 17:50:01

回答

1

< <二元左移运算符。左操作数值左移了右操作数指定的位数。
A < < 2将给出240即1111 0000
二进制右移运算符>>。左操作数值右移右操作数指定的位数。
A >> 2将给出15这是1111

相关问题