课后作业加密
源代碼:
package test2;import java.util.Scanner;public class Caeser {private String table; // 定義密鑰字母表private int key; // 定義密鑰keypublic Caeser(String table, int key) {// 根據(jù)不同的字母表和不同的密鑰生成一個(gè)新的凱撒算法,達(dá)到通用的目的super();this.table = table;this.key = key;}public String encrypt(String from) {//凱撒加密算法,傳入明文字符串,返回一個(gè)密文字符串String to = "";for (int i = 0; i < from.length(); i++) {to += table.charAt((table.indexOf(from.charAt(i))+key)%table.length());}return to;}public static void main(String[] args) {Caeser caeser = new Caeser("abcdefghijklmnopqrstuvwxyz", 3);Scanner scanner = new Scanner(System.in);System.out.println("請(qǐng)輸入要加密的字符串");String str =scanner.nextLine(); //輸入字符串 securityString result = caeser.encrypt(str); //調(diào)用加密方法進(jìn)行加密System.out.print(result); // 可得結(jié)果 vhfxulwb } }截圖:
轉(zhuǎn)載于:https://www.cnblogs.com/zyldbk/p/4906682.html
總結(jié)
- 上一篇: 转:mac下安装homebrew
- 下一篇: String、StringBuffer、