所以我有一个基本的加密器,在解密后,我想将该文件中的数字字符串转换为int。
public class Encryption {
//Basic Encryptor/Decryptor
public Encryption() throws IOException {
StringBuffer num = new StringBuffer("100");
encrypt(num);
File f = new File("C:\\fun\\a");
if(f.mkdirs()) {
File f2 = new File(f,"b.txt");
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(num.toString());
bw.close();
Scanner s = new Scanner(f2);
String aaa = new String(s.nextLine());
StringBuffer bbb = new StringBuffer(aaa);
decrypt(bbb);
int b = Integer.parseInt(aaa.toString());
System.out.println(b);
}
}
//ENCRYPTOR
public static void encrypt(StringBuffer word) {
for(int i = 0; i < word.length(); i++) {
int temp = 0;
temp = (int)word.charAt(i);
temp = temp * 9;
word.setCharAt(i, (char)temp);
}
}
//DECRYPTOR
public static void decrypt(StringBuffer word) {
for(int i = 0; i < word.length(); i++) {
int temp = 0;
temp = (int)word.charAt(i);
temp = temp / 9;
word.setCharAt(i, (char)temp);
}
}
public static void main(String[] args) {
try {
@SuppressWarnings("unused")
Encryption e = new Encryption();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我试图将字符串缓冲区转换为字符串然后转换为int。当我检查文件时,它仍然是加密的,并且控制台有解密错误。
Exception in thread "main" java.lang.NumberFormatException: For input string: "???"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Encryptor.Encryption.<init>(Encryption.java:28)
at Encryptor.Encryption.main(Encryption.java:62)
哪个指向int b = Integer.parseInt(aaa.toString());
。
答案 0 :(得分:1)
我认为您唯一的问题是您正在尝试解析aaa
StringBuffer的内容。 aaa
StringBuffer仍包含加密内容。您正在将bbb
缓冲区传递给decrypt方法,因此这是您应该尝试解析为int的缓冲区:
int b = Integer.parseInt(bbb.toString());