gpt4 book ai didi

java - java语言中的凯撒密码密文失败

转载 作者:搜寻专家 更新时间:2023-11-01 03:09:14 25 4
gpt4 key购买 nike

我用 Java 语言制作凯撒密码加密器,这是我的代码

private void encCaesar() {
tempCipher = "abcdef";
char[] chars = tempCipher.toCharArray();
for (int z = 0; z < tempCipher.length(); z++) {
char c = chars[z];
if (c >= 32 && c <= 126) {
int x = c - 32;
x = (x + keyCaesar) % 96;
if (x < 0)
x += 96;
chars[z] = (char) (x + 32);
}
}
ciphertext = chars.toString();
etCipher.setText(ciphertext);
}

我查不出有什么不对,但是密文是这样的405888,明文为“abcdef”,默认 key 为3,这是胡说八道

怎么了?

正确:

private void encCaesar() {
tempCipher = "abcdef";
char[] chars = tempCipher.toCharArray();
for (int z = 0; z < tempCipher.length(); z++) {
char c = chars[z];
if (c >= 32 && c <= 126) {
int x = c - 32;
x = (x + keyCaesar) % 96;
if (x < 0)
x += 96;
chars[z] = (char) (x + 32);
}
}
ciphertext = new String(chars);
etCipher.setText(ciphertext);
}

最佳答案

您应该使用 new String(chars) 而不是 chars.toString() 创建 ciphertext:

ciphertext = new String(chars);

关于java - java语言中的凯撒密码密文失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13991379/

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