Class CaesarUtil

java.lang.Object
com.sure.tool.codec.CaesarUtil

public final class CaesarUtil extends Object
凯撒密码编解码工具。

原理:英文字母按指定位移量轮转,非英文字母字符原样保留,大小写分别处理。 经典位移:ROT-13(shift 13)、ROT-3(shift 3,军事用途)。

示例:

String encoded = CaesarUtil.encode("Hello, World!", 3);   // "Khoor, Zruog!"
String plain   = CaesarUtil.decode("Khoor, Zruog!", 3);  // "Hello, World!"
String rot13   = CaesarUtil.encode("suretool", 13);      // "fhergbby"

null 语义:入参为 null 时返回 null(空安全)。

Since:
1.19.1
Author:
suretool
  • Method Details

    • encode

      public static String encode(String text, int shift)
      凯撒密码编码。

      位移量可正可负,内部统一按 26 取模归一,保证 encode(encode(s, n), -n) == s。

      Parameters:
      text - 原文,可为 null
      shift - 位移量(按 26 取模)
      Returns:
      密文;null 返回 null
    • decode

      public static String decode(String text, int shift)
      凯撒密码解码(位移量取反)。
      Parameters:
      text - 密文,可为 null
      shift - 编码时使用的位移量
      Returns:
      原文;null 返回 null