Java 简单实现Enigma英格玛密码
Posted Wiiix
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 简单实现Enigma英格玛密码相关的知识,希望对你有一定的参考价值。
package egnima;
import java.util.Scanner;
public class enigma
static public void main(String []args)
Scanner cin=new Scanner(System.in);
System.out.print("Input 0 to encode message and 1 to decode message:");
int op=cin.nextInt();
if(op==0)
encode();
else
decode();
static void encode()
String msg="";
String encodemsg="";
int count=0;
System.out.printf("%s","Input the message:");
Scanner cin=new Scanner(System.in);
msg=cin.nextLine();
for(int i=0;i<msg.length();i++)
if((Character.isSpace(msg.charAt(i))))
encodemsg+=" ";
else if((msg.charAt(i)-count)<'a')
encodemsg+=(char)(msg.charAt(i)+26-count);
else
encodemsg+=(char)(msg.charAt(i)-count);
count++;
System.out.println(decodemsg);
static void decode()
String msg="";
String decodemsg="";
int count=0;
System.out.printf("%s","Input the message:");
Scanner cin =new Scanner(System.in);
msg=cin.nextLine();
for(int i=0;i<msg.length();i++)
if(Character.isSpace(msg.charAt(i)))
decodemsg+=" ";
else if(msg.charAt(i)+count>'z')
decodemsg+=(char)(msg.charAt(i)-26+count);
else
decodemsg+=(char)(msg.charAt(i)+count);
count++;
System.out.println(decodemsg);
以上是关于Java 简单实现Enigma英格玛密码的主要内容,如果未能解决你的问题,请参考以下文章