【发布时间】:2019-03-24 07:11:11
【问题描述】:
我在 java 中创建了一次性加密,但我有两个问题是:
在加密中,如何使密钥的大小根据明文的大小灵活地随机生成例如,明文的大小是4个字母,所以数组密钥的大小必须是32 位,因为每个字母都有 8 位。
在解密中,如何以二进制形式读取文件和这两个文件,然后在它们之间进行异或,然后将其打印为 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;
}
}
【问题讨论】:
-
一次性填充是真正的随机序列。计算机中的 RNG 不提供一次性便笺簿。您创建的是流密码。
-
我相信你不应该在每次获取 8 位时都创建
SecureRandom的新实例。它很慢,甚至可能不安全。 -
是否需要将每一位存储在 32 位整数中?每 8 位使用 一个字节 似乎更合乎逻辑。在这种情况下,Q1 的答案是:返回一个特定大小的字节数组。同样,您可以使用
FileInputStream和FileOutputStream加密/解密文件中的字节,回答 Q2。
标签: java arrays list encryption random