凯撒加密(key为3)
基本思想:
加密是把字符串中的每一个字符+3解密是每一个字符-3
源代码:
package demo;
import java.util.Scanner;
class Encryption{
String a;
public void encryption(String q){
String string= "";
for(int i = 0;i < q.length();i++)
{
char b=q.charAt(i);
if(b >= 'a'&&b <='z')
{
b += 3;
if(b > 'z')
b-=26;
if(b < 'a')
b=+26;
}
else if(b >= 'A'&&b <= 'Z')
{
b+=3;
if(b > 'Z')
b-=26;
if(b < 'A')
b+=26;
}
string += b;
}
System.out.println("加密后为:"+string);
}
}
class Decryption{
String a;
public void decryption(String q){
String string= "";
for(int i = 0;i < q.length();i++)
{
char b=q.charAt(i);
if(b >= 'a'&&b <='z')
{
b -= 3;
if(b > 'z')
b-=26;
if(b < 'a')
b+=26;
}
else if(b >= 'A'&&b <= 'Z')
{
b-=3;
if(b > 'Z')
b-=26;
if(b < 'A')
b+=26;
}
string += b;
}
System.out.println("解密后为:"+string);
}
}
public class Password{
public static void main(String[] args){
System.out.println("a。加密 b.解密");
Scanner choose = new Scanner(System.in);
String a = choose.nextLine();
if(a.equalsIgnoreCase("a"))
{
Scanner c = new Scanner(System.in);
System.out.println("请输入明文:");
String b = c.nextLine();
Encryption en = new Encryption();
en.encryption(b);
}
else if(a.equalsIgnoreCase("b")) // equals 用来比较两个对象所表示的字符是否相同
{ // equalsIgnoreCase 用来比较对象所代表的字符与括号中字符是否相同
Scanner c = new Scanner(System.in);
System.out.println("请输入密码:");
String b = c.nextLine();
Decryption de = new Decryption();
de.decryption(b);
}
}
}
结果截图:
最新文章
- java基础知识总结(1)
- 批处理——服务器的web文件备份
- CF570D:Tree Requests
- 二叉搜索树 POJ 2418 Hardwood Species
- SharePoint 2013 删除母版页报错“This file may not be moved, deleted, renamed, or otherwise edited”
- [MySQL]导入导出
- MYSQL里的索引类型介绍
- Java——泛型(最易懂的方式讲解泛型)
- IAR ERROR --- [Li006]
- 【Android】使用BaseAdapter实现复杂的ListView
- 12.04 ubuntu 更改IP
- mount挂接命令使用
- Ecelipse上添加Server
- Mybatis笔记三:全局配置文件
- vlc-ts
- ili9325--LCD寄存器配置研究
- 黄聪:实用WordPress SQL查询方法
- phalcon7开发环境搭建
- Redis系列一:reids的单机版安装
- Nunit常用的方法说明