写一个加密和解密的method,还有用户输入的密钥,密钥 +就是顺着移动(如果+2,就是则字母A将变为C,字母B将变为D),- 就是倒着移动。
1 是加密
2 是解密
下面是给出的代码
import java.util.Scanner;
public class CaesarCypher {
// Write an encode method
// Write a decode method
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String message = "";
System.out.println("Do you have a message to (1) encode or (2) decode");
int option = in.nextInt();
in.nextLine();
if (option==1){
System.out.println("Type your message to encode");
message = in.nextLine();
}
if (option==2){
System.out.println("Type your message to decode");
message = in.nextLine();
}
}
}
import java.util.Scanner;
public class CaesarCypher {
// Write an encode method
// Write a decode method
public static String Decrypt(String str, int n) {
// TODO Auto-generated method stub
//解密
int k=Integer.parseInt("-"+n);
String string="";
for(int i=0;i<str.length();i++) {
char c=str.charAt(i);
if(c>='a'&&c<='z')//如果字符串中的某个字符是小写字母
{
c+=k%26;//移动key%26位
if(c<'a')
c+=26;//向左超界
if(c>'z')
c-=26;//向右超界
}else if(c>='A'&&c<='Z')//如果字符串中的某个字符是大写字母
{
c+=k%26;//移动key%26位
if(c<'A')
c+=26;//向左超界
if(c>'Z')
c-=26;//向右超界
}
string +=c;//将解密后的字符连成字符串
}
return string;
}
public static String Encryption(String str, int k) {
// TODO Auto-generated method stub
//加密
String string="";
for(int i=0;i<str.length();i++) {
char c=str.charAt(i);
if(c>='a'&&c<='z')//如果字符串中的某个字符是小写字母
{
c+=k%26;//移动key%26位
if(c<'a')
c+=26;//向左超界
if(c>'z')
c-=26;//向右超界
}else if(c>='A'&&c<='Z')//如果字符串中的某个字符是大写字母
{
c+=k%26;//移动key%26位
if(c<'A')
c+=26;//向左超界
if(c>'Z')
c-=26;//向右超界
}
string +=c;//将解密后的字符连成字符串
}
return string;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String message = "";
int key = 1; //你的密钥
System.out.println("Do you have a message to (1) encode or (2) decode");
int option = in.nextInt();
in.nextLine();
if (option==1){
System.out.println("Type your message to encode");
message = in.nextLine();
String r = Encryption(message, key);
System.out.println(r);
}
if (option==2){
System.out.println("Type your message to decode");
message = in.nextLine();
String r = Decrypt(message, key);
System.out.println(r);
}
}
}
import java.util.Scanner;
public class CaesarCypher {
public String transform(String msg, int key){
StringBuilder sb = new StringBuilder(msg);
if(key < 0)
key = 26 - ( (-key) % 26 );
for( int i = 0; i < msg.length(); i++ ) {;
if (msg.charAt(i) - 'a' < 0)
sb.setCharAt(i, (char) ((msg.charAt(i) - 'A' + 26 + key) % 26 + 'A'));
else
sb.setCharAt(i, (char) ((msg.charAt(i) - 'a' + 26 + key) % 26 + 'a'));
}
return sb.toString();
}
public String encode(String msg, int key){
return transform(msg, key);
}
public String decode(String msg, int key){
return transform(msg, -key);
}
public static void main(String[] args) {
CaesarCypher cc = new CaesarCypher();
Scanner in = new Scanner(System.in);
boolean loop = true;
while(loop){
System.out.println("Do you have a message(e.g. abcde +5 ) to:\n (1) Encode\n (2) Decode\n (3) Exit");
int option = in.nextInt(), key;
String message;
switch (option){
case 1:
System.out.println("Type your message to encode:");
message = in.next(); key = in.nextInt();
System.out.println("Message Encoded as [" + cc.encode(message, key) + "]");
break;
case 2:
System.out.println("Type your message to decode:");
message = in.next(); key = in.nextInt();
System.out.println("Message Decoded as [" + cc.decode(message, key) + "]");
break;
default:
loop = false;
}
}
}
}