Я создал одноразовое шифрование в java, но у меня есть две проблемы:
В шифровании, как я могу сделать размер ключа гибким в соответствии с размером открытого текста и сгенерированным случайным образом, например, размер открытого текста составляет 4 буквы, поэтому размер ключа массива должен быть 32-битным, потому что каждый письмо имеет 8 бит.
В расшифровке, как я могу читать файлы и эти два файла в двоичной форме, а затем выполнять XOR между ними, а затем распечатывать его как форму ASCLL.
Мой код:
public class onetimepad {
public static void main(String[] args) throws Exception {
int[] key = generate8BitKey();
Scanner in = new Scanner(System.in);
System.out.println(" One Time Pad encryption and decryption ");
System.out.println(" For encryption Enter 1 ");
System.out.println(" For decryption Enter 2 ");
System.out.println(" Exit Enter 3 ");
int a = in.nextInt();
switch (a) {
case 1:
File input = new File("message.txt");
Scanner sc = new Scanner(input);
String msg = sc.nextLine();
System.out.println("Key: ");
//Write the Key in file.
PrintWriter writer2 = new PrintWriter("Output.txt", "UTF-8");
writer2.println("------ Key ------- ");
for (int i : key) {
System.out.print(key[i]);
writer2.print(key[i]);
}
writer2.close();
System.out.println();
String ciphertext = encrypt(msg, key);
System.out.println("Encrypted Message: " + ciphertext);
break;
case 2:
File input2 = new File("ciphertext.txt");
Scanner sc2 = new Scanner(input2);
String msg2 = sc2.nextLine();
File input3 = new File("Key.txt");
Scanner sc3 = new Scanner(input3);
String msg3 = sc2.nextLine();
System.out.println("Decrypted Message: " + decrypt(msg3, key));
break;
default:
}
}// End the main.
//------------------- Methods.
public static String encrypt(String msg, int[] key) {
int[] binmsg = stringToBinary(msg);
int[] result = xor(binmsg, repeatArray(key, msg.length()));
String r = "";
for (int i : result) {
r += (char) (result[i] + '0');
}
return r;
}
//---------------------
public static String decrypt(String ciphertext, int[] key) {
int[] bin = new int[ciphertext.length()];
for (int i = 0; i < ciphertext.length(); i++) {
bin[i] = ciphertext.charAt(i) - '0';
}
int[] result = xor(bin, repeatArray(key, bin.length / 8));
return binaryToString(result);
}
//---------------------
public static int[] stringToBinary(String msg) {
int[] result = new int[msg.length() * 8];
for (int i = 0; i < msg.length(); i++) {
String bin = Integer.toBinaryString((int) msg.charAt(i));
while (bin.length() < 8) {
bin = "0" + bin;
}
for (int j = 0; j < bin.length(); j++) {
result[i * 8 + j] = bin.charAt(j) - '0';
}
}
return result;
}
//---------------------
public static String binaryToString(int[] bin) {
String result = "";
for (int i = 0; i < bin.length / 8; i++) {
String c = "";
for (int j = 0; j < 8; j++) {
c += (char) (bin[i * 8 + j] + '0');
}
result += (char) Integer.parseInt(c, 2);
}
return result;
}
//---------------------
public static int[] generate8BitKey() {
int[] key = new int[8];
for (int i = 0; i < 8; i++) {
SecureRandom sr = new SecureRandom();
key[i] = sr.nextInt(2);
}
return key;
}
//---------------------
public static int[] xor(int[] a, int[] b) {
int[] result = new int[a.length];
for (int i = 0; i < a.length; i++) {
result[i] = a[i] == b[i] ? 0 : 1;
}
return result;
}
//---------------------
public static int[] repeatArray(int[] a, int n) {
int[] result = new int[a.length * n];
for (int i = 0; i < result.length; i++) {
result[i] = a[i % a.length]; // mod
}
return result;
}
}
SecureRandom
каждый раз, когда вы извлекаете 8 бит. Это медленно и, возможно, даже не безопаснее. - person Adder   schedule 19.10.2018FileInputStream
иFileOutputStream
, отвечая на Q2. - person Maarten Bodewes   schedule 22.10.2018