【发布时间】:2012-11-14 21:43:17
【问题描述】:
我正在尝试将 Integer 转换为 String,然后使用 XOR 加密对 String 进行加密。但是当我再次解密我的 Strin 时,我得到了一个不同的答案,即我在加密之前输入的字符串,我不知道我做错了什么?
public class Krypte {
public static void main (String [] args) {
int i = 12345;
String k = Integer.toString(i);
String G = secure(k.getBytes());
System.out.println("Encrypted: " + G);
String U = secure(G.getBytes());
System.out.println("Decrypted: " + U);
int X = Integer.parseInt(U);
System.out.println("As an int: " + X);
}
public static String secure(byte[] msg) {
// Variables
int outLength = msg.length;
byte secret = (byte) 0xAC; // same as 10101100b (Key)
// XOR kryptering
for (int i = 0; i < outLength; i++) {
// encrypting each byte with XOR (^)
msg[i] = (byte) (msg[i] ^ secret);
}
return new String(msg);
}
}
【问题讨论】:
-
对不起,我的错。但是你能帮我解决我的编码问题,而不是我对加密的理解(误解)吗?
-
问问自己:“什么是字符串”?字符串是 not 任意字节的序列。阅读String 的 Java 文档以帮助回答这个问题,您将开始理解为什么您的代码不起作用。
标签: java string encryption character-encoding int