導航:首頁 > 編程語言 > 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凱撒加密相關的資料

熱點內容
非箍筋加密區剪力 瀏覽:116
利聯科技伺服器卡怎麼辦 瀏覽:383
js和python哪個好 瀏覽:460
c盤的哪些文件夾沒用 瀏覽:80
文件為什麼會超出在線解壓限制 瀏覽:588
python類實例化對象 瀏覽:794
硬碟dos外部命令 瀏覽:792
做演算法還是開發 瀏覽:872
按鍵精靈自定義圖層命令 瀏覽:351
魅藍3手機音視頻文件夾 瀏覽:945
安卓手機製表怎麼換行 瀏覽:215
牆柱搭接箍筋怎麼加密 瀏覽:456
怎麼加密不讓人打開 瀏覽:336
2g3g演算法 瀏覽:206
python可以在net開發 瀏覽:934
編程里的hr啥意思 瀏覽:411
上海php兼職 瀏覽:728
順豐app如何驗證學生 瀏覽:380
伺服器mac地址過濾器 瀏覽:942
程序員一年內被開除 瀏覽:456