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