⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vigenere.java

📁 古典密码学的程序实现
💻 JAVA
字号:
/*
*密钥为 Richard
*结果不区分大小写
*/
public class Vigenere
{
	String key = new String("RICHARD");
	
	public String getCipher(String message)
	{
		int msgArray[] = new ConvertCharToInt().convert(message);
		int cipArray[] = new int[msgArray.length];
		int keyArray[] = new ConvertCharToInt().convert(key);
		int i=0;
		while (i<msgArray.length)
		{
			for (int j=0;j<key.length() ;j++ )
			{
				cipArray[i] = msgArray[i] + keyArray[j];
				if (cipArray[i]>25)
				{
					cipArray[i] = cipArray[i] - 25;
				}
				i++;
				if (i==msgArray.length)
					break;
			}
		}
		String cipher = new ConvertIntToChar().convert(cipArray);
		return cipher;
	}
	public String getMessage(String cipher)
	{
		int cipArray[] = new ConvertCharToInt().convert(cipher);
		int msgArray[] = new int[cipArray.length];
		int keyArray[] = new ConvertCharToInt().convert(key);
		int i=0;
		while (i<cipArray.length)
		{
			for (int j=0;j<key.length() ;j++ )
			{
				msgArray[i] = cipArray[i] - keyArray[j];
				if (msgArray[i]<0)
				{
					msgArray[i] = msgArray[i] + 25;
				}
				i++;
				if (i==cipArray.length)
					break;
			}
		}
		String message = new ConvertIntToChar().convert(msgArray);
		return message;
	}
	/*
	*test if correct
	*/
	public static void main(String [] args)
	{
		String message = new String("Hello this is Richard");
		String cipher = new Vigenere().getCipher(message);
		System.out.println("The cipher is:  "+cipher);
		message = new Vigenere().getMessage(cipher);
		System.out.println("The message is: "+message);
	}
}

⌨️ 快捷键说明

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