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

熱點內容
android文件分區 瀏覽:368
南京開通數控螺紋編程 瀏覽:290
伺服器與ups用什麼線 瀏覽:967
unix網路命令 瀏覽:48
程序員表白代碼大全可復制 瀏覽:365
手機如何共享web伺服器 瀏覽:956
php介面有什麼用 瀏覽:382
iis如何安裝php 瀏覽:791
k5嗜血魔鍵安卓怎麼調好用 瀏覽:834
建行app中如何添加銀行卡 瀏覽:281
簡便演算法100點 瀏覽:161
如何創新我的世界伺服器 瀏覽:882
戰地怎麼看伺服器地址 瀏覽:348
vue怎麼打包放上伺服器 瀏覽:165
為什麼安卓服夏日活動沒有兔子頭 瀏覽:894
pubg為什麼顯示伺服器連接失敗 瀏覽:650
阿里雲掃碼登錄伺服器 瀏覽:971
化學基礎pdf 瀏覽:896
51單片機晶碼管 瀏覽:281
怎麼查伺服器假死原因日誌在哪看 瀏覽:277