凯撒加密问题程序

设计思想:加密的过程是将字母在字母表中的位置向后移两位,即A编程D,字符串+3,解密时字符串-3

程序流程图:

源代码:

//王冶雯   凯撒问题加密和解密     关键点:加密是字符串+3,解密时-3

package string11;

import java.util.Scanner;

public class jiami 

{

 

public static void main(String[] args)throws Exception

{

// TODO Auto-generated method stub

System.out.println("[A 加密 ] [J 解密],Please Choose One");

Scanner s=new Scanner(System.in);//创建Scanner对象

String s1=s.nextLine();//获取本行字符串

if (s1.equalsIgnoreCase("A"))//判断s1与A的大小

 

{

int key;

System.out.println("请输入明文:");

Scanner sc=new Scanner(System.in);

String ss=sc.nextLine();

System.out.println("请输入密钥:");

Scanner scc=new Scanner(System.in);

key=scc.nextInt();//将输入的字符转化成int型

Encryption(ss,key);//调用Encryption方法

}

else if(s1.equalsIgnoreCase("J"));

{

int key;

System.out.println("请输入密文:");

Scanner sc=new Scanner(System.in);

String ss =sc.nextLine();

System.out.println("请输入密钥:");

Scanner scc=new Scanner(System.in);

key=scc.nextInt();

Decrypt(ss,key);//调用Encryption方法

}

}

//加密程序 

public static void Encryption(String str,int t)

{

String string="";

int i;

char c;

for(i=1;i<str.length();i++)

{

c=str.charAt(i);

if(c>='a'&&c<='z')//如果字符中的某个字符是小写的

{

c+=t % 26;///移动26位

if(c<'a')

c+=26;//向左超界

if(c>'z')

c-=26;//向右超界

}

else if(c>='A'&&c<='Z')//如果字符中的某个字符是大写的

{

c+=t % 26;

if(c<'A')

c+=26;//向左超界

if(c>'Z')

c-=26;//向右超界

}

string +=c;//将加密后的字符连成字符串

}

System.out.println(str +"加密后为:" + string);

}

public static void Decrypt(String str,int n)

{

int t;

t=Integer.parseInt("-" +n);

String string="";

int i;

for(i=0;i<str.length();i++)

{

char c=str.charAt(i);

if(c>='a'&&c<='z')//如果字符中的某个字符是小写的

{

c+=t % 26;//移动26位

if(c<'a')

c+=26;//向左超界

if(c>'z')

c-=26;//向右超界

}

else if(c>='A'&&c<='Z')//如果字符中的某个字符是大写的

{

c+=t % 26;

if(c<'A')

c+=26;//向左超界

if(c>'Z')

c-=26;//向右超界

}

string +=c;//将加密后的字符连成字符串

}

System.out.println(str +"解密后为:" + string);

}

}

 

截图:

转载于:https://www.cnblogs.com/jingjing0629/p/4908024.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值