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

📄 standardhead.java

📁 古典密码学的程序实现
💻 JAVA
字号:
/*
*标准字头密码体制的实现
*key = richard 因为有2个 'r' ,所以实际用的key 是 'richad'
*区分字母的大小写
*/
public class StandardHead
{
	char lowCaseKey[] = {'r','i','c','h','a','d','b','e','f','g','j','k','l','m','n','o','p','q','s','t','u','v','w','x','y','z'};
	char upCaseKey[] = {'R','I','C','H','A','D','B','E','F','G','J','K','L','M','N','O','P','Q','S','T','U','V','W','X','Y','Z'};
	public String getCipher(String message)//to encrypt
	{
		char msgChar[] = message.toCharArray();
		int len = msgChar.length;
		char cipChar[] = new char[len];
		for (int i=0 ; i<len ;i++ )
		{
			int num = (int)msgChar[i];
			if (num>=65 && num<=90)//if it is up case
			{
				cipChar[i] = upCaseKey[num-65];
			}
			else if (num>=97 && num<=122)//if it is low case
			{
				cipChar[i] = lowCaseKey[num-97];
			}
			else//if it is not a character
			{
				cipChar[i] = msgChar[i];
			}
		}
		String cipher = new String(cipChar);
		return cipher;
	}
	public String getMessage(String cipher)//to decode
	{
		char cipChar[] = cipher.toCharArray();
		int len = cipChar.length;
		char msgChar[] = new char[len];
		for (int i=0 ; i<len ;i++ )
		{
			int num = (int)cipChar[i];
			if (num>=65 && num<=90)//if it is up case
			{
				msgChar[i] =  (char)(65+getIndex(cipChar[i],upCaseKey));
			}
			else if (num>=97 && num<=122)//if it is low case
			{
				msgChar[i] =  (char)(97+getIndex(cipChar[i],lowCaseKey));
			}
			else//if it is not a character
			{
				msgChar[i] = cipChar[i];
			}
		}
		String message = new String(msgChar);
		return message;
	}
	public int getIndex(char c, char key[])
	{
		for (int j=0;j<key.length ;j++ )
		{
			if (key[j] == c)
			{
				return j;
			}
		}
		return -1;
	}
	/*
	*test if correct
	*/
	public static void main(String [] args)
	{
		String message = new String("ABCDEFghijklmn");
		String cipher = new StandardHead().getCipher(message);
		System.out.println("The Cipher is :  "+cipher);
		message = new StandardHead().getMessage(cipher);
		System.out.println("The Message is : "+message);
	}
}

⌨️ 快捷键说明

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