java如何改进我的编码器/解码器?我的加密方法像样吗?
我一直在用java制作一个加密/解密程序只是为了好玩,我想知道我的编码方式是否是加密消息的好方法?它的工作原理是首先输入两个密钥来确定加密。然后我把信息的后半部分移到前面。然后将消息放入一个和Ascii字符对应的数字数组中。然后,我将所有偶数索引数移位一个数,奇数索引数也是如此。然后针对每组3、4、5和7个索引。换档装置运行多次。这是一个好的加密系统吗?要破解有多难
import java.util.*;
public class Bored {
static String message;
static int arr[];
static String messagefinal[];
//starter helps determine if even/odd numbers are effected
//mover is where the shifter variable goes
//signchanger determines if shifter is added/subtracted
public static void shift(int starter, int mover, int signchanger, int adder) {
for (int i = starter; i < message.length(); i = i + adder) {
arr[i] = arr[i] + mover * signchanger;
if (arr[i] > 126) {
arr[i] = arr[i] - 95;
} else if (arr[i] < 32) {
arr[i] = arr[i] + 95;
}
}
}
//This creates the shifters
//theBound is one of the keys and theRandom is one of the randoms
public static int shiftercreate(int theBound, Random theRandom) {
int x = 0;
for (int i = 0; i < theBound; i++) {
x = theRandom.nextInt(23) + 1;
}
return x;
}
//This prints out the message
public static void printMessage(int printInitial, int printBound) {
for (int i = printInitial; i < printBound; i++) {
String printed = messagefinal[i];
System.out.print(printed);
}
}
public static void main(String[] args) {
//This gets the keys, encoding/decoding, and the message
Scanner scnr = new Scanner(System.in);
System.out.println("Enter the first key: ");
int key1 = scnr.nextInt();
System.out.println("Enter the second key: ");
int key2 = scnr.nextInt();
System.out.println("Encoding or Decoding (1/2): ");
int decider = scnr.nextInt();
System.out.println("Enter Message: ");
scnr.nextLine();
message = scnr.nextLine();
scnr.close();
//This changes one of the keys if they are the same
if (key1 == key2) {
key2 = key1 + key2 + key1 * key2;
}
if (decider == 1) {
//This mixes up the message
int len = message.length();
String firsthalf = message.substring(0, len / 2), secondhalf = message.substring(len / 2);
message = secondhalf + firsthalf;
}
//This makes the message into an array of Ascii
arr = new int[message.length()];
for (int i = 0; i < message.length(); i++) {
arr[i] = message.charAt(i);
}
//This creates the rands
Random rand1 = new Random(1006030090 / key1);
Random rand2 = new Random(2070090000 / key2);
Random r1 = new Random(93432098 % key1);
Random r2 = new Random(427492379 % key2);
//This determines the shifters
int shifter1, shifter2, s1, s2, s3, s4;
shifter1 = 0;
shifter2 = s1 = s2 = s3 = s4 = shifter1;
shifter1 = shiftercreate(key2, rand1);
shifter2 = shiftercreate(key1, rand2);
s1 = shiftercreate(key1, r2);
s2 = shiftercreate(key2, r1);
s3 = shiftercreate(key1, r1);
s4 = shiftercreate(key2, r2);
//This creates the bound variables
Random boundRand = new Random(Math.abs(key1 + key2 + shifter1 + shifter2 + s1 + s2 - s3 + s4));
int mainBound = boundRand.nextInt(10) + 10;
int evenBound = boundRand.nextInt(7) + boundRand.nextInt(2) + 14;
int oddBound = boundRand.nextInt(5) + boundRand.nextInt(5) + 8;
int secondRound = boundRand.nextInt(10) + 17;
int thirdRound = boundRand.nextInt(4) + boundRand.nextInt(5) + 3;
int fourthRound = boundRand.nextInt(8) + boundRand.nextInt(9);
int fifthRound = boundRand.nextInt(10) + 15;
//This is the encoder
if (decider == 1) {
for (int i = 0; i < mainBound; i++) {
//This shifts the even numbers by the "shifter1" value
for (int j = 0; j < evenBound; j++) {
shift(0, shifter1, 1, 2);
}
//This shifts the odd numbers by the "shifter2" value
for (int j = 0; j < oddBound; j++) {
shift(1, shifter2, 1, 2);
}
//This is the second round of shifting done by fours
for (int j = 0; j < secondRound; j++) {
shift(0, s1, 1, 4);
shift(1, s2, 1, 4);
shift(2, s3, 1, 4);
shift(3, s4, 1, 4);
}
//This is the third round of shifting done by threes
for (int j = 0; j < thirdRound; j++) {
shift(0, s3, 1, 3);
shift(1, s2, 1, 3);
shift(2, s4, 1, 3);
}
//This is the fourth round of shifting done by fives
for (int j = 0; j < fourthRound; j++) {
shift(0, shifter2, 1, 5);
shift(1, s1, 1, 5);
shift(2, s2, 1, 5);
shift(3, shifter1, 1, 5);
shift(4, s4, 1, 5);
}
//This is the fifth round of shifting done by sevens
for (int j = 0; j < fifthRound; j++) {
shift(0, shifter1 + shifter2, 1, 7);
shift(1, s1 + s3, 1, 7);
shift(2, s3 + s4, 1, 7);
shift(3, shifter2 + s2, 1, 7);
shift(4, shifter1 + s3, 1, 7);
shift(5, shifter2 + s4, 1, 7);
shift(6, s2 + s4, 1, 7);
}
}
//This is the decoder
} else if (decider == 2) {
for (int i = 0; i < mainBound; i++) {
//This decodes the fifth round of shifting
for (int j = 0; j < fifthRound; j++) {
shift(0, shifter1 + shifter2, -1, 7);
shift(1, s1 + s3, -1, 7);
shift(2, s3 + s4, -1, 7);
shift(3, shifter2 + s2, -1, 7);
shift(4, shifter1 + s3, -1, 7);
shift(5, shifter2 + s4, -1, 7);
shift(6, s2 + s4, -1, 7);
}
//This decodes the fourth round of shifting
for (int j = 0; j < fourthRound; j++) {
shift(0, shifter2, -1, 5);
shift(1, s1, -1, 5);
shift(2, s2, -1, 5);
shift(3, shifter1, -1, 5);
shift(4, s4, -1, 5);
}
//This decodes the third round of shifting
for (int j = 0; j < thirdRound; j++) {
shift(0, s3, -1, 3);
shift(1, s2, -1, 3);
shift(2, s4, -1, 3);
}
//This decodes the secound round of shifting
for (int j = 0; j < secondRound; j++) {
shift(0, s1, -1, 4);
shift(1, s2, -1, 4);
shift(2, s3, -1, 4);
shift(3, s4, -1, 4);
}
//This reshifts the odd numbers by the "shifter2" value
for (int j = 0; j < oddBound; j++) {
shift(1, shifter2, -1, 2);
}
//This reshifts the even numbers by the "shifter1" value
for (int j = 0; j < evenBound; j++) {
shift(0, shifter1, -1, 2);
}
}
}
//This makes the numbers in the array into letters
messagefinal = new String[message.length()];
for (int i = 0; i < message.length(); i++) {
messagefinal[i] = Character.toString(arr[i]);
}
//This prints out the final message
if (decider == 1) {
printMessage(0, message.length());
} else if (decider == 2) {
if (message.length() % 2 == 0) {
printMessage(message.length() / 2, message.length());
printMessage(0, message.length()/2);
} else {
printMessage((message.length() / 2) + 1, message.length());
printMessage(0, (message.length()/2) + 1);
}
}
}
}
我认为加密将每420个字符重复一次(420是2、3、4、5和7的LCM),因此这可能是一个缺陷
共 (0) 个答案