我正在用java编写SDES密码,进入我将两个长度为5的数组向左移动的阶段,这对于p10kleft成为shiftp10kleft很好,但是当我将相同的代码应用到p10kright时成为shiftp10kright它在末尾添加一个随机1,而不是将第一个0移到最后一位。
这是我的代码:
package ciphers;
import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
public class SDESNew {
public static void main(String []arg)throws Exception
{
int[] k = {1,1,1,1,0,1,1,0,0,0};
int[] P10 = {3,5,2,7,4,10,1,9,8,6};
int[] P8 = {6,3,7,4,8,5,10,9};
int[] P4 = {2,4,3,1};
int[] IP = {2,6,3,1,4,8,5,7};
int[] EP = {4,1,2,3,2,3,4,1};
int S0[][] = {
{1,0,3,2},
{3,2,1,0},
{0,2,1,3},
{3,1,3,2}
};
int S1[][] = {
{0,1,2,3},
{2,0,1,3},
{3,0,1,2},
{2,1,0,3}
};
//Arrays that we have to calculate
int p10k[]=new int[10]; //Create p10(k) array
int[] p10kleft = new int[5];
int[] p10kright = new int[5];
int[] shiftp10kleft = new int[5];
int[] shiftp10kright = new int[5];
for(int i=0;i<10;i++)
{
System.out.println("Value from array P10: " +P10[i]); //Printing values from array P10
System.out.println("Array position to be used in array k: " +(P10[i]-1)); //Array position begins at 0 so subtracting 1 from P10 position so array begins at 0
System.out.println(k[P10[i]-1]); //Printing values in array positions
p10k[i]=k[P10[i]-1]; //Putting array values in p10k array
System.out.println("");
}
System.out.println("P10k: " +Arrays.toString(p10k)); //Printing out all values in p10k array
System.arraycopy(p10k, 0, p10kleft, 0, p10kleft.length);
System.arraycopy(p10k, p10kleft.length, p10kright, 0, p10kright.length);
System.out.println("Left: " +Arrays.toString(p10kleft));
System.out.println("Right: " +Arrays.toString(p10kright));
// System.arraycopy(p10kleft, 1, shiftp10kleft, 0, p10kleft.length - 1);
int n = p10kleft.length;
shiftp10kleft = p10kleft;
int i = 0;
if (n>0)
{
for(i=0; i<n-1; i++)
{
p10kleft[i]=p10kleft[i+1];
}
}
p10kleft[i] = shiftp10kleft[i];
System.out.println("Shift Left: " +Arrays.toString(shiftp10kleft));
n = p10kright.length;
shiftp10kright = p10kright;
i = 0;
if (n>0)
{
for(i=0; i<n-1; i++)
{
p10kright[i]=p10kright[i+1];
}
}
p10kright[i] = shiftp10kright[i];
System.out.println("Shift Right: " +Arrays.toString(shiftp10kright));
}
}
这是目前的输出:
Value from array P10: 3
Array position to be used in array k: 2
1
Value from array P10: 5
Array position to be used in array k: 4
0
Value from array P10: 2
Array position to be used in array k: 1
1
Value from array P10: 7
Array position to be used in array k: 6
1
Value from array P10: 4
Array position to be used in array k: 3
1
Value from array P10: 10
Array position to be used in array k: 9
0
Value from array P10: 1
Array position to be used in array k: 0
1
Value from array P10: 9
Array position to be used in array k: 8
0
Value from array P10: 8
Array position to be used in array k: 7
0
Value from array P10: 6
Array position to be used in array k: 5
1
P10k: [1, 0, 1, 1, 1, 0, 1, 0, 0, 1]
Left: [1, 0, 1, 1, 1]
Right: [0, 1, 0, 0, 1]
Shift Left: [0, 1, 1, 1, 1]
Shift Right: [1, 0, 0, 1, 1]
我需要Shift Right才能正确输出[1,0,0,1,0]
答案 0 :(得分:1)
与注释中一样,在进入循环之前将第一个值存储在基元中:
int n = p10kleft.length;
shiftp10kleft = p10kleft;
int i = 0;
int first = p10kleft[i];
if (n>0)
{
for(i=0; i<n-1; i++)
{
p10kleft[i]=p10kleft[i+1];
}
}
p10kleft[i] = first;
关于你的另一个问题,重复这个操作3次是显而易见的(而不是特别聪明或有效的方法):
for(int shiftCounter = 0; shiftCounter < 3; shiftCounter++) {
int n = p10kleft.length;
...
p10kleft[i] = first;
}
无论如何,正如我已经建议的那样,有许多更好的方法可以做到这一点。但是,如果您已在数组上设置了死区,则至少可以使用System.arraycopy
。一个简单的例子是:
static int[] shiftLeft(int[] arr, int shift) {
int[] tmp = new int[arr.length];
System.arraycopy(arr, shift, tmp, 0, arr.length-shift);
System.arraycopy(arr, 0, tmp, arr.length-shift, shift);
return tmp;
}
然后像这样使用它:
p10kleft = shiftLeft(p10kleft, 1); // shift once
p10kleft = shiftLeft(p10kleft, 3); // shift 3 times
编辑:修复arraycopy
中的参数顺序。