我已经获得了有关加密的具体步骤,本质上是将字符串转换为字节数组,并用特定数字“异或”每个字节数组元素,并将这个新的字节数组转换为字符串。
至于解密,我要将解密的字符串重新转换为字节数组,将每个元素与加密中使用的数字进行异或运算,然后将新的解密数组转换为字符串,以提供我开始时使用的确切字符串。
不幸的是,我无法获得我开始使用的字符串,例如,当我尝试加密 6、7、8、9、10 时,解密返回 B@722b9406 而不是 6、7、8、9、10。任何想法为什么会这样?
我的加密代码如下:
//Take each array element and do "Exclusive or" with "50"
public void XORUnencryptedByteArray() {
System.out.println("\nPART 3:\nXORUnencryptedByteArray() - " + "UnencryptedStringAsByteArray.length IS: " + UnencryptedStringAsByteArray.length);
byte[] UnencryptedByteArraybyXOR = new byte[UnencryptedStringAsByteArray.length];
int XOR_Value = 50;
byte XOR_Value_as_byte = (byte) XOR_Value;
System.out.println("\nPART 3:\nXORUnencryptedByteArray() - " + "XOR_Value_as_byte IS: " + XOR_Value_as_byte + "\n");
for ( int j = 0; j < UnencryptedStringAsByteArray.length; j++ ) {
System.out.println("\nPOSITION " + j + " OF UnencryptedStringAsByteArray[j] CONTAINS: " + UnencryptedStringAsByteArray[j]);
System.out.println("\n" + UnencryptedStringAsByteArray[j] + " ^ " + XOR_Value_as_byte + " IS: " + (UnencryptedStringAsByteArray[j] ^ XOR_Value_as_byte));
}
for (int i = 0; i < UnencryptedByteArraybyXOR.length; i++) {
UnencryptedByteArraybyXOR[i] = (byte) (UnencryptedStringAsByteArray[i] ^ XOR_Value_as_byte);
System.out.println( "\nPOSITION " + i + " OF UnencryptedByteArraybyXOR[i] IS: " + UnencryptedByteArraybyXOR[i]);
}
//Collect all encrypted array elements and convert the byte array to string
System.out.println("\nUnencryptedByteArraybyXOR.length IS: " + UnencryptedByteArraybyXOR.length);
System.out.println("\nUnencryptedByteArraybyXOR.toString() AS STRING IS: " + UnencryptedByteArraybyXOR.toString());
UnencryptedByteArraybyXORAsString = new String(UnencryptedByteArraybyXOR);
//System.out.println("\nUnencryptedByteArraybyXORAsString IS: " + UnencryptedByteArraybyXORAsString);
WriteStringToFile(UnencryptedByteArraybyXOR.toString());
}
public void WriteStringToFile(String UnencryptedByteArraybyXORAsString) {
try {
String str = UnencryptedByteArraybyXORAsString;
File newTextFile = new File("/Users/anmonari/Desktop/textfile.txt");
FileWriter fw = new FileWriter(newTextFile);
fw.write(str);
fw.close();
} catch (IOException iox) {
//do stuff with exception
iox.printStackTrace();
}
}
我的解密代码如下:
public void XORDecryptedByteArray(byte[] encryptedStringAsByteArray) {
System.out.println("\nXORencryptedByteArray() - " + "encryptedStringAsByteArray.length IS: " + encryptedStringAsByteArray.length);
byte[] encryptedByteArraybyXOR = new byte[encryptedStringAsByteArray.length];
int XOR_Value = 50;
byte XOR_Value_as_byte = (byte) XOR_Value;
System.out.println("\nXORencryptedByteArray() - " + "XOR_Value_as_byte IS: " + XOR_Value_as_byte + "\n");
for ( int j = 0; j < encryptedStringAsByteArray.length; j++ ) {
System.out.println("\n" + encryptedStringAsByteArray[j] + " ^ " + XOR_Value_as_byte + " IS: " + (encryptedStringAsByteArray[j] ^ XOR_Value_as_byte));
}
for (int i = 0; i < encryptedByteArraybyXOR.length; i++) {
encryptedByteArraybyXOR[i] = (byte) (encryptedStringAsByteArray[i] ^ XOR_Value_as_byte);
System.out.println("\n" + "POSITION " + i + " OF encryptedByteArraybyXOR[i] CONTAINS: " + encryptedByteArraybyXOR[i]);
}
System.out.println("\ndecryptedByteArraybyXOR.length IS: " + encryptedByteArraybyXOR.length);
System.out.println("\ndecryptedByteArraybyXOR.toString() AS STRING IS: " + encryptedByteArraybyXOR.toString());
}
任何帮助表示赞赏!!!!