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

📄 polyalphabetic.java

📁 多表加密解密算法实现示例
💻 JAVA
字号:
import java.io.*;


public class Polyalphabetic {
	private String srcFilePath;
	private String prnPath;
	private char[] keys;
	private char[][] table={
			{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'},
			{'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A'},
			{'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B'},
			{'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C'},
			{'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D'},
			{'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E'},
			{'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F'},
			{'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G'},
			{'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H'},
			{'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I'},
			{'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J'},
			{'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K'},
			{'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L'},
			{'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M'},
			{'O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N'},
			{'P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O'},
			{'Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'},
			{'R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q'},
			{'S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R'},
			{'T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S'},
			{'U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T'},
			{'V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U'},
			{'W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V'},
			{'X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W'},
			{'Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X'},
			{'Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y'},
			};
		
	public Polyalphabetic(String filePath,String parentPath,String key){
		this.srcFilePath = filePath;
		this.prnPath = parentPath;
		this.keys = key.toCharArray();
		
		//change characters of the key into numbers as one index of table[26][26]
		for(int i=0;i<keys.length;i++){
			keys[i] -= 97;		// A->0,B->1,...
		}
	}//Polyalphabetic constructer
	
	public void cipher(){
		try{
        	File sourceFile = new File(srcFilePath);	
        	File outFile = new File(prnPath+"\\Polyalphabetic encryption.txt");
			outFile.createNewFile();
			
        	RandomAccessFile sf = new RandomAccessFile(sourceFile, "r");
    		RandomAccessFile of = new RandomAccessFile(outFile, "rw");	
			
    		
    		Byte inChar;	// one byte read in
    		int i = 0;		// index of the keys[]
    		int xi;			// table[xi][]
    		int yi;			// table[][yi]
			while(sf.getFilePointer()<sourceFile.length()){ //file.length以字节为单位
				inChar = sf.readByte();			
				
				//change the character read in to a number as the other
				//index of table[26][26]
				yi = inChar.intValue();
				if(yi>=65&&yi<=90)
					yi -= 65;
				else if(yi>=97&&yi<=122)
					yi -= 97;
				else
				{	
					of.writeByte(yi);	//inChar is not a letter
					continue;
				}
				
				//find the outChar in table[26][26]
				if(i<keys.length)
					xi = keys[i++];
				else
					{
						i -= keys.length;
						xi = keys[i++];
					}
				
				of.writeByte(table[xi][yi]);
			}
			sf.close();
			of.close();			
		}//end try
		catch(FileNotFoundException ex){
			System.out.println("Err when accessing source file");
		}
		catch(IOException ex){
			System.out.println("IOException");
		}
	}//cipher()
	
	public void decipher(){
		try{
        	File sourceFile = new File(srcFilePath);	
        	File outFile = new File(prnPath+"\\Polyalphabetic decryption.txt");
			outFile.createNewFile();
			
        	RandomAccessFile sf = new RandomAccessFile(sourceFile, "r");
    		RandomAccessFile of = new RandomAccessFile(outFile, "rw");	
			
    		
    		Byte inChar;	// one byte read in
    		int intV;
    		int i = 0;		// index of the keys[]
    		int xi=0;			// table[xi][]
    		int yi=0;			// table[][yi]
			while(sf.getFilePointer()<sourceFile.length()){
				inChar = sf.readByte();			
				intV = inChar.intValue();
				
				if(intV>=65&&intV<=90)
					; 
				else if(intV>=97&&intV<=122)
					intV -= 32;
				else
				{	
					of.writeByte(inChar);	//inChar is not a letter
					continue;
				}
				
				//find the outChar in table[26][26]
				if(i<keys.length)
					xi = keys[i++];
				else
					{
						i -= keys.length;
						xi = keys[i++];
					}
				
				for(int index=0;index<26;index++)
					if(table[xi][index]==intV)
						yi = index+97;
				of.writeByte(yi);
			}
			sf.close();
			of.close();			
		}//end try
		catch(FileNotFoundException ex){
			System.out.println("Err when accessing source file");
		}
		catch(IOException ex){
			System.out.println("IOException");
		}
	}// decipher()
}

⌨️ 快捷键说明

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