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

📄 hill.java

📁 古典密码学的程序实现
💻 JAVA
字号:

public class Hill
{
	int[][] encryptKey = {{3,2},{4,3}};
	int[][] decodeKey =  {{3,-2},{-4,3}};
	int rtInt1 = 0;
	int rtInt2 = 0;
	public String getCipher(String message)
	{
		int[] msgArray = new ConvertCharToInt().convert(message);
		int[] cipArray = new int[msgArray.length];
		int i=0;
		for (i=0; i<(msgArray.length/2)*2 ;i=i+2 )
		{
			martixMulit(msgArray[i],msgArray[i+1],encryptKey);
			cipArray[i] = rtInt1;
			cipArray[i+1] = rtInt2;
		}
		if (i<msgArray.length)
		{
			cipArray[msgArray.length-1] = msgArray[msgArray.length-1];
		}
		String cipher = new String(new ConvertIntToChar().convert(cipArray));
		return cipher;
	}
	public String getMessage(String cipher)
	{
		int[] cipArray = new ConvertCharToInt().convert(cipher);
		int[] msgArray = new int[cipArray.length];
		int i=0;
		for (i=0; i<(cipArray.length/2)*2 ;i=i+2 )
		{
			martixMulit(cipArray[i],cipArray[i+1],decodeKey);
			msgArray[i] = rtInt1;
			msgArray[i+1] = rtInt2;
		}
		if (i<cipArray.length)
		{
			msgArray[cipArray.length-1] = cipArray[cipArray.length-1];
		}
		String message = new String(new ConvertIntToChar().convert(msgArray));
		return message;
	}
	public void martixMulit(int a,int b,int[][] martix)
	{
		rtInt1 = (a*martix[0][0] + b*martix[1][0])%26;
		if (rtInt1<0)
		{
			rtInt1 = rtInt1 + 26;
		}
		rtInt2 = (a*martix[0][1] + b*martix[1][1])%26;
		if (rtInt2<0)
		{
			rtInt2 = rtInt2 + 26;
		}
	}
	/*
	*test if correct
	*/
	public static void main(String [] args)
	{
		String message = new String("Richard");
		String cipher = new Hill().getCipher(message);
		System.out.println("The cipher is: "+cipher);
		message = new Hill().getMessage(cipher);
		System.out.println("The message is:"+message);
	}
}

⌨️ 快捷键说明

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