JAVA實作caesar凱撒加密演算法
public class Caesar { public static final String SOURCE = "abcdefghijklmnopqrstuvwxyz"; public static final int LEN = SOURCE.length(); /** * @param args */ public static void main(String[] args) { String result = caesarEncryption("newyork"); System.out.println("encryption result:" + result); System.out.println("decryption result:" + caesarDecryption(result)); } //Encryption public static String caesarEncryption(String s) { StringBuilder sb = new StringBuilder(); if (s == null || s.length() < 1) { System.out.println("you Input nothing."); return null; } if (!isAlp(s)) { System.out.println("input ABC... only"); return null; } s = s.toLowerCase(); int len = s.length(); for (int j = 0; j < len; j++) { char c = s.charAt(j); int a = SOURCE.indexOf(c); if (a == LEN -1) a = -1; if (a == LEN -2) a = -2; if (a == LEN - 3) a = -3; sb.append(SOURCE.charAt(a + 3)); } return sb.toString(); } //Decryption public static String caesarDecryption(String s) { StringBuilder sb = new StringBuilder(); if (s == null || s.length() < 1) { System.out.println("you Input nothing."); return null; } if (!isAlp(s)) { System.out.println("input ABC... only"); return null; } s = s.toLowerCase(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); int a = SOURCE.indexOf(c); if (a == 2) a = LEN + 2; if (a == 1) a = LEN + 1; if (a == 0) a = LEN; sb.append(SOURCE.charAt(a - 3)); } return sb.toString(); } public static boolean isAlp(String s) { String p = "^[A-Za-z]+$"; Pattern pattern = Pattern.compile(p); Matcher matcher = pattern.matcher(s); if (matcher.find()) { return true; } return false; } }
登入後複製
更多JAVA實作caesar凱撒加密演算法相關文章請關注PHP中文網!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前
By DDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。
