Java 中的凯撒密码加密

Sheeraz Gul 2023年10月12日
  1. Java 中的凯撒密码
  2. Java 中的凯撒密码加密
  3. Java 中的凯撒密码解密
Java 中的凯撒密码加密

凯撒密码是执行加密的最简单方法之一。本教程演示如何在 Java 中使用 Caesar Cipher 执行加密和解密。

Java 中的凯撒密码

凯撒密码是最早的加密方法之一。它可以用任何编程语言实现。算法简单;例如,如果我们对文本 delftstack 执行加密,Caesar Cipher 算法将通过将字符替换为按字母顺序排列在字符下方的固定位置来加密文本。

我们传递一个整数密钥,以决定我们要在加密字符时向下多少个位置。

例如,如果文本是 ABCD 并且密钥是 2,则加密的文本将是 CDEF。让我们尝试在 Java 中实现 Caesar Cipher 加密方法。

Java 中的凯撒密码加密

下面的代码演示了 Java 中 Caesar Cipher 加密的实现。

package delftstack;

import java.util.Scanner;

public class Caesar_Cipher_Encrypt {
  public static void main(String... s) {
    String Original_Message, Encrypted_Message = "";
    int Cipher_Key;
    char Message_Char;
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter a message: ");
    Original_Message = sc.nextLine();
    System.out.println("Enter the key: ");
    Cipher_Key = sc.nextInt();

    for (int i = 0; i < Original_Message.length(); ++i) {
      Message_Char = Original_Message.charAt(i);
      if (Message_Char >= 'a' && Message_Char <= 'z') {
        Message_Char = (char) (Message_Char + Cipher_Key);

        if (Message_Char > 'z') {
          Message_Char = (char) (Message_Char - 'z' + 'a' - 1);
        }

        Encrypted_Message += Message_Char;
      } else if (Message_Char >= 'A' && Message_Char <= 'Z') {
        Message_Char = (char) (Message_Char + Cipher_Key);

        if (Message_Char > 'Z') {
          Message_Char = (char) (Message_Char - 'Z' + 'A' - 1);
        }

        Encrypted_Message += Message_Char;
      } else {
        Encrypted_Message += Message_Char;
      }
    }
    System.out.println("The Encrypted Message is : " + Encrypted_Message);
  }
}

上面的代码实现了 Caesar Cipher 加密方法。查看输出:

Please enter a message:
This is delftstack.com
Enter the key:
3
The Encrypted Message is : Wklv lv ghoiwvwdfn.frp

Java 中的凯撒密码解密

解密将是反之亦然的操作。请参阅使用 Caesar Cipher 方法的解密实现。

package delftstack;

import java.util.Scanner;

public class Caesar_Cipher_Decrypt {
  public static void main(String... s) {
    String Original_Message, Decrypted_Message = "";
    int Cipher_Key;
    char Message_Char;
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter an encrypted message: ");
    Original_Message = sc.nextLine();
    System.out.println("Enter the key: ");
    Cipher_Key = sc.nextInt();

    for (int i = 0; i < Original_Message.length(); ++i) {
      Message_Char = Original_Message.charAt(i);
      if (Message_Char >= 'a' && Message_Char <= 'z') {
        Message_Char = (char) (Message_Char - Cipher_Key);

        if (Message_Char < 'a') {
          Message_Char = (char) (Message_Char + 'z' - 'a' + 1);
        }

        Decrypted_Message += Message_Char;
      } else if (Message_Char >= 'A' && Message_Char <= 'Z') {
        Message_Char = (char) (Message_Char - Cipher_Key);

        if (Message_Char < 'A') {
          Message_Char = (char) (Message_Char + 'Z' - 'A' + 1);
        }

        Decrypted_Message += Message_Char;
      } else {
        Decrypted_Message += Message_Char;
      }
    }
    System.out.println("The Decrypted Message is : " + Decrypted_Message);
  }
}

上面的代码在 Java 中实现了 Caesar Cipher 解密。查看输出:

Please enter an encrypted message:
Wklv lv ghoiwvwdfn.frp
Enter the key:
3
The Decrypted Message is : This is delftstack.com
作者: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

相关文章 - Java Encryption