我已经在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
实例。这样做会很慢,而且可能并不安全。 - AdderFileInputStream
和FileOutputStream
加密/解密文件中的字节,回答Q2。 - Maarten Bodewes