gpt4 book ai didi

Java 凯撒密码

转载 作者:行者123 更新时间:2023-12-02 02:32:04 25 4
gpt4 key购买 nike

在这个凯撒密码中,我获得了大写字母的错误输出。代码如下:

public class CaesarCipherCaseSensitive
{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

public static String encrypt(String pt, int shiftKey)
{
String ct = "";
for (int i = 0; i < pt.length(); i++)
{
int charPosition = ALPHABET.indexOf(pt.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = ALPHABET.charAt(keyVal);
ct += replaceVal;
}
return ct;
}

public static String decrypt(String ct, int shiftKey)
{
String pt = "";
for (int i = 0; i < ct.length(); i++)
{
int charPosition = ALPHABET.indexOf(ct.charAt(i));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0)
{
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
pt += replaceVal;
}
return pt;
}

public static void main(String[] args)
{
String message1 = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
System.out.println(encrypt(message1, 3));
System.out.println(decrypt(encrypt(message1, 3), 3));
System.out.println(encrypt(message1.toLowerCase(),5));
System.out.println(decrypt(encrypt(message1.toLowerCase(),5),5));

}
}

输出如下:CCCCCCCCCCCCCCCCCCCCCCCCzzzzzzzzzzzzzzzzzzzzzzzzfghijklmnopqrtuvwxyzabcdeabcdefghijklmopqrstuvwxyz

期望的输出是:DEFGHIJKLMOPQRSTUVWXYZABCABCDEFGHIJKLMOPQRSTUVWXYZfghijklmnopqrtuvwxyzabcdeabcdefghijklmopqrstuvwxyz

最佳答案

问题是您将一个大写字母传递给方法 indexOf(),该方法检查 ALPHABET 但没有找到它,因此返回 -1。您需要将大写字母转换为小写,或添加大写字母。

我的解决方案,将大写字母转换为小写字母,然后加密/解密是:

public class CaesarCipherCaseSensitive{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

public static String encrypt(String pt, int shiftKey){
String ct = "";
for (int i = 0; i < pt.length(); i++){
char letter = pt.charAt(i);
boolean upperCase = false;
if((int)letter < 91){
letter = (char)((int)letter + 32);
upperCase = true;
}
int charPosition = ALPHABET.indexOf(letter);
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = ALPHABET.charAt(keyVal);
if(upperCase){
replaceVal = (char)((int)replaceVal - 32);
}
ct += replaceVal;
}
return ct;
}

public static String decrypt(String ct, int shiftKey){
String pt = "";
for (int i = 0; i < ct.length(); i++){
char letter = ct.charAt(i);
boolean upperCase = false;
if((int)letter < 91){
letter = (char)((int)letter + 32);
upperCase = true;
}
int charPosition = ALPHABET.indexOf(letter);
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0){
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
if(upperCase){
replaceVal = (char)((int)replaceVal - 32);
}
pt += replaceVal;
}
return pt;
}

public static void main(String[] args){
String message1 = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
System.out.println(encrypt(message1, 3));
System.out.println(decrypt(encrypt(message1, 3), 3));
System.out.println(encrypt(message1.toLowerCase(),5));
System.out.println(decrypt(encrypt(message1.toLowerCase(),5),5));
}
}

关于Java 凯撒密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46437897/

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