导航:首页 > 编程语言 > 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凯撒加密相关的资料

热点内容
取小数位数php 浏览:715
mdk编译重复代码 浏览:100
容器怎么连接云服务器 浏览:846
程序员多久能提升 浏览:147
bpmx3源码 浏览:123
通信类单片机 浏览:817
加密植发的区别 浏览:538
程序员跑需求 浏览:224
s7服务器怎么设置 浏览:344
2k17连接不上服务器怎么办 浏览:494
人力资源系统开源源码 浏览:687
河北视频加密有哪些 浏览:651
桌面两个微信怎么都加密码 浏览:278
长沙单身程序员 浏览:886
服务器下载异常是因为什么 浏览:705
java防刷 浏览:844
3dmax取消当前命令 浏览:361
显示当前模式下所有可执行的命令 浏览:760
为什么程序员拿了股份还要高薪 浏览:949
电脑运行命令里的记录能删吗 浏览:699