caesar.java

来自「一个简单的程序:Caesar加密算法,用java实现的源代码!」· Java 代码 · 共 43 行

JAVA
43
字号
import java.io.*;
class Caesar
{

   public static void main(String args [])
       throws IOException
	{
		InputStreamReader reader1 = new InputStreamReader(System.in);
		BufferedReader input1 = new BufferedReader(reader1);
		System.out.print("请输入密钥key:" + " ");
		String text = input1.readLine();
		int key = new Integer(text).intValue();

		InputStreamReader reader2 = new InputStreamReader(System.in);
		BufferedReader input2 = new BufferedReader(reader2);
		System.out.print("请输入明文:" + " ");
		String data = input2.readLine();
		
		
        String es=""; 

		
		for (int i = 0; i < data.length(); i++)
		{
			char c = data.charAt(i);
			if (c >= 'a' && c <= 'z')
			{
				c += key % 26;
				if (c < 'a') c += 26;
				if (c > 'z') c -= 26;
			}
			else if (c >= 'A' && c <= 'Z')
			{
				c += key % 26;
				if (c < 'A') c += 26;
				if (c > 'Z') c -= 26;
			}
			es += c;
		}
		System.out.print("产生的密文是:"+es);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?