gpt4 book ai didi

java - 将 StringBuffer 加密转换为 int

转载 作者:行者123 更新时间:2023-12-01 12:07:20 26 4
gpt4 key购买 nike

所以我有一个基本的加密器,解密后,我想将该文件中的数字字符串转换为 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());

最佳答案

我认为您唯一的问题是您正在尝试解析 aaa StringBuffer 的内容。 aaa StringBuffer 仍然包含加密的内容。您将 bbb 缓冲区传递给解密方法,因此这是您应该尝试解析为 int 的缓冲区:

int b = Integer.parseInt(bbb.toString());

关于java - 将 StringBuffer 加密转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27489407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com