導航:首頁 > 編程語言 > java凱撒加密

java凱撒加密

發布時間:2023-06-07 21:54:14

⑴ 請教啊!java加密演算法!要求用戶輸入要加密的字元(英文字元其他的不考慮)題目如下:我主要問的是《加密

public class Swither {
public static void main(String[] args) {
System.out.println("ab,cdx;yz中國");
System.out.println(Encryption.encryption( "ab,cdx;yz中國"));
System.out.println(Decryption.decryption((Encryption.encryption( "ab,cdx;yz中國"))));
}
}
運行結果如下:
ab,cdx;yz中國
de,fga;bc中國
ab,cdx;yz中國

public class Encryption {

public static String encryption(String content)
{
if(content==null)
return null;
String temp="";
for(int i=0;i<content.length();i++)
{
char c=content.charAt(i);
//if(Character.isLetter(c))
if((c>='a' && c<='z')||(c>='A' && c<='Z'))
if(c=='x' || c=='y' || c=='z' || c=='X' || c=='Y' || c=='Z')
c=(char)(c-23);
else
c=(char)(c+3);
temp+=c;

}
return temp;
}

}

public class Decryption {

public static String decryption(String content)
{
if(content==null)
return null;
String temp="";
for(int i=0;i<content.length();i++)
{
char c=content.charAt(i);
//if(Character.isLetter(c))
if((c>='a' && c<='z')||(c>='A' && c<='Z'))
if(c=='a' || c=='b' || c=='c' || c=='A' || c=='B' || c=='C')
c=(char)(c+23);
else
c=(char)(c-3);
temp+=c;

}
return temp;
}

}

⑵ 用java 編寫一個凱撒加密和解密

import java.util.Scanner;

public class Caeser {
private String table; // 定義密鑰字母表
private int key; // 定義密鑰key
public Caeser(String table, int key) {
// 根據不同的字母表和不同的密鑰生成一個新的凱撒演算法,達到通用的目的
super();
this.table = table;
this.key = key;
}
public String encrypt(String from) {
//凱撒加密演算法,傳入明文字元串,返回一個密文字元串
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("請輸入要加密的字元串");
String str =scanner.nextLine(); //輸入字元串 security
String result = caeser.encrypt(str); //調用加密方法進行加密
System.out.print(result); // 可得結果 vhfxulwb
}
}

閱讀全文

與java凱撒加密相關的資料

熱點內容
cad制圖常用命令 瀏覽:856
主角叫楚風重生都市的小說 瀏覽:212
單片機jnb指令 瀏覽:1002
可以觀看vip電視劇的網站 瀏覽:244
如何編譯pfx文件 瀏覽:139
兩個女的吃大便 瀏覽:596
枚舉調用java 瀏覽:410
閃閃發光大團結下載 瀏覽:255
主角叫葉天免費閱讀 瀏覽:908
譚干聰演的全部電影 瀏覽:370
台灣電影她有很強的佔有欲 瀏覽:836
app地推公司怎麼運營 瀏覽:102
做不好程序員改啥工作比較好 瀏覽:199
何家駒電影全集大全 瀏覽:954
床戲最刺激的電影有哪些 瀏覽:817
主角在現代買軍火抗戰 瀏覽:780
linux配置域名的解析 瀏覽:441
周星馳免費國語完整版 瀏覽:554
anatomypdf 瀏覽:707
蘋果app刪除了怎麼還有 瀏覽:634