使用 Java 进行凯撒密码加解密

发布于 2025-01-19 10:06:42 字数 1095 浏览 3 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕巷 2025-01-26 10:06:42

是的,那么您可以将密码存储在字符串中,然后使用该字符串和 -1 * 密钥调用您的加密方法吗?

Right, so can you store the cryptogram in a String, then call your encrypt method with that string and -1 * key?

哆兒滾 2025-01-26 10:06:42

如果您使用 shiftKey = 10 加密,则需要使用 shiftKey = 16 解密。因此,当您输入-10来反转该过程时,如果为负数,则需要添加26。就在你的 for(ii ..) 循环之前放置这个。

 shiftKey = shiftKey < 0 ? alpha.length()+shiftKey : shiftKey;

表示如果 shiftKey 小于 0,则向其添加 alpha.length()。否则保持原样。

If you encrypt with shiftKey = 10 you need to decrypt with shiftKey = 16. So when you enter -10 to reverse the process, you need to add 26 if it is negative. Right before your for(ii ..) loop put this.

 shiftKey = shiftKey < 0 ? alpha.length()+shiftKey : shiftKey;

Says if shiftKey is less than 0, add alpha.length() to it. Otherwise keep it as is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文