使用 Java 进行凯撒密码加解密
我使用 Ceaser 的密码(通过移位模式移动字母表)和两个移位模式,然后加密一个句子以将其打印在屏幕上。然后我尝试将加密的消息存储在不同的字符串中并将其解密回原始字符串。到目前为止,我已经尝试了下面的代码,但无法将其解密。基本上尝试用凯撒密码进行加密然后解密,但到目前为止我只能加密而不能解密。任何建议表示赞赏!
public class Solution { //to keep track of index
public static final String alpha = "abcdefghijklmnopqrstuvwxyz";
public static String encrypt(String message, int shiftKey) {
message = message.toLowerCase();
String cipherText = "";
for (int ii = 0; ii < message.length(); ii++) {
int charPosition = alpha.indexOf(message.charAt(ii));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = alpha.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String message = new String();
int key = 0;
System.out.print("Enter the String for Encryption:");
message = sc.next();
System.out.println("\n\nEnter Shift Key:");
key = sc.nextInt();
System.out.println("\nEncrpyted msg:" + encrypt(message, key));
} //main method ends
I'm use Ceaser’s cipher (shifting the alphabet by a shift pattern) with the shift pattern of two and then encrypt a sentence to print it on screen. Then I'm trying to store the encrypted message in a different string and decrypt it back to the original string. I've tried the following code below so far but I can't get it to decrypt back. Basically trying to encrypt and then decrypt in Ceasar's cipher, but so far I can only encrypt and not decrypt it back. Any suggestions are appreciated!
public class Solution { //to keep track of index
public static final String alpha = "abcdefghijklmnopqrstuvwxyz";
public static String encrypt(String message, int shiftKey) {
message = message.toLowerCase();
String cipherText = "";
for (int ii = 0; ii < message.length(); ii++) {
int charPosition = alpha.indexOf(message.charAt(ii));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = alpha.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String message = new String();
int key = 0;
System.out.print("Enter the String for Encryption:");
message = sc.next();
System.out.println("\n\nEnter Shift Key:");
key = sc.nextInt();
System.out.println("\nEncrpyted msg:" + encrypt(message, key));
} //main method ends
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,那么您可以将密码存储在字符串中,然后使用该字符串和 -1 * 密钥调用您的加密方法吗?
Right, so can you store the cryptogram in a String, then call your encrypt method with that string and -1 * key?
如果您使用
shiftKey = 10
加密,则需要使用shiftKey = 16
解密。因此,当您输入-10
来反转该过程时,如果为负数,则需要添加26
。就在你的for(ii ..)
循环之前放置这个。表示如果
shiftKey
小于0
,则向其添加alpha.length()
。否则保持原样。If you encrypt with
shiftKey = 10
you need to decrypt withshiftKey = 16
. So when you enter-10
to reverse the process, you need to add26
if it is negative. Right before yourfor(ii ..)
loop put this.Says if
shiftKey
is less than0
, addalpha.length()
to it. Otherwise keep it as is.