用java 编写一个凯撒加密和解密

Python017

用java 编写一个凯撒加密和解密,第1张

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 = 0i <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

}

}

import java.*

public class Practise {

public static void main(String[] args) {

String P = new String()// 明文

String K = new String()// 密钥

String C = new String()// 密文

short LR=-1//间隔的方向,向左为-1,向右为1

P = "benrencainiaoyizhi"

K = "P"

C = "QTCGTCRPXCXPDNXOWX"

System.out.println("明文:"+P)

System.out.println("密钥:"+K)

System.out.println("密文:"+C+"\n")

CaesarCode caesar=new CaesarCode()

LR=1

System.out.println("加密:"+caesar.encrypt(P, K, LR))

LR=-1

System.out.println("解密:"+caesar.decrypt(K, C, LR).toLowerCase())

}

}

class CaesarCode {

private char alphabet[] = new char[26]//存储字母表

//加密

protected String encrypt(String P,String K,short LR)

{

int i=0,j=0,n=0//n是间隔

String C=new String()//密文

P=P.toUpperCase()

P=getNewP(P)

K=K.toUpperCase()

n=getN(K)

//将明文转换成密文

for(i=0i<P.length()i++)

{

j=String.valueOf(alphabet).indexOf(P.charAt(i))//获取密文字母在字母表所在的下标

j=(j+n*LR+26)%26//向左或向右移动n位

C+=(char)(j+65)

}

return C

}

//解密

protected String decrypt(String K,String C,short LR)

{

int i=0,j=0,n=0//n是间隔

String P=new String()//明文

K=K.toUpperCase()

C=C.replaceAll(" +"," ")

C=C.toUpperCase()

n=getN(K)

//将密文转换成明文

for(i=0i<C.length()i++)

{

j=String.valueOf(alphabet).indexOf(C.charAt(i))//获取密文字母在字母表所在的下标

j=(j+n*LR+26)%26//向左或向右移动n位

P+=(char)(j+65)

}

return P

}

//获取经过处理的明文

private String getNewP(String P)

{

int i=0

char p[] = P.toCharArray()

for (i = 0i <P.length()i++) {

if (p[i] <'A' || p[i] >'Z')// 将非字母换成空格

{

p[i] = ' '

}

}

P = String.valueOf(p)

P = P.replaceAll(" +", "")// 将明文的所有空格去掉

return P

}

//获取间隔

private int getN(String K)

{

int i=0,n=0

//生成字母表

for(i=0i<26i++)

{

alphabet[i]=(char)(i+65)//字母A在ASCII表中的值是065

}

if(isNum(K))

{

n=Integer.parseInt(K)

}

else

{

n=String.valueOf(alphabet).indexOf(K)//当K不是数字时适用

}

return n

}

//判断密钥是否为数字

private boolean isNum(String K)

{

return K.matches("[0-9]+")//+表示1个或多个(如"3"或"225")

}

}

public class Swither {

public static void main(String[] args) {

System.out.println("ab,cdxyz中国")

System.out.println(Encryption.encryption( "ab,cdxyz中国"))

System.out.println(Decryption.decryption((Encryption.encryption( "ab,cdxyz中国"))))

}

}

运行结果如下:

ab,cdxyz中国

de,fgabc中国

ab,cdxyz中国

public class Encryption {

public static String encryption(String content)

{

if(content==null)

return null

String temp=""

for(int i=0i<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=0i<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

}

}