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

热点内容
单片机学习指导 浏览:586
胸7椎体轻度压缩 浏览:108
sk5服务器什么意思 浏览:554
什么是廊坊交警app 浏览:294
衣柜造价算法 浏览:984
默认的web服务器地址 浏览:694
单片机与发光二极管 浏览:320
pythonwebmodule 浏览:328
空调压缩机不停了 浏览:115
python序列怎么取 浏览:199
线上数据库加密怎么查询 浏览:794
js中数据加密 浏览:470
穴pdf 浏览:549
阿里云服务器云数据库还需要吗 浏览:146
在程序设计中常用的算法有哪些 浏览:977
为什么苏州公积金app一直维护 浏览:805
有ip地址但是dhcp服务器 浏览:446
三星手机加密中断怎么回事 浏览:538
训练模型init源码 浏览:840
程序编译是谁的功能 浏览:505