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

熱點內容
程序員能力關鍵詞 瀏覽:615
plc編程高級視頻教程 瀏覽:610
java遞歸求n 瀏覽:84
python絕對路徑導入 瀏覽:126
nex5g加密 瀏覽:975
18的空島伺服器地址 瀏覽:86
程序員要學什麼硬體 瀏覽:666
股票漲跌源碼怎麼看 瀏覽:576
加密軟體做法 瀏覽:55
美國程序員有多少中國人 瀏覽:739
人民日報app里怎麼看新聞早班車 瀏覽:585
忘了app怎麼辦 瀏覽:529
如何用雲伺服器做雲平台 瀏覽:299
非箍筋加密區剪力 瀏覽:119
利聯科技伺服器卡怎麼辦 瀏覽:386
js和python哪個好 瀏覽:463
c盤的哪些文件夾沒用 瀏覽:83
文件為什麼會超出在線解壓限制 瀏覽:591
python類實例化對象 瀏覽:797
硬碟dos外部命令 瀏覽:794