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

📄 playfair.java

📁 playfair加密算法、解密算法的实现示例
💻 JAVA
字号:
import java.io.*;
import java.util.*;

public class Playfair {
	private String srcFilePath;
	private String prnPath;
	private Character[][] table= new Character[5][5];
	private Character lastChar;
	private RandomAccessFile of;
	
	public Playfair(String filePath,String parentPath,String key){
		this.srcFilePath = filePath;
		this.prnPath = parentPath;
		key = key+"abcdefghijklmnopqrstuvwxyz";
		
		Set set = new LinkedHashSet();
		for(int i=0;i<key.length();i++)
			set.add(key.charAt(i));

		Iterator iterator = set.iterator();
		for(int i=0;i<5;i++){
			for(int j=0;j<5;j++){
				table[i][j]= (Character)iterator.next();
				System.out.print(" "+table[i][j]);
			}
			if(i<4)
				System.out.print("\n");
		}

		lastChar = (Character)iterator.next();
		System.out.print("/"+lastChar+"\n");
	}// Playfair constructor
	
	public void cipher(){
		try{
        	File sourceFile = new File(srcFilePath);	
        	File outFile = new File(prnPath+"\\Playfair encryption.txt");
			outFile.createNewFile();
			
        	RandomAccessFile sf = new RandomAccessFile(sourceFile,"r");
    		of = new RandomAccessFile(outFile,"rw");	
		    		
    		Byte inChar;	// one byte read in
    		int intV;		//intValue of inChar
    		int[] couple={0,0};	//a couple of letters
    		int p=0;  // pointer of couple[2]
			while(sf.getFilePointer()<sourceFile.length()){    		
				inChar = sf.readByte();
				intV = inChar.intValue();
				
				//divide input strings into couples of letters
				if(p==0||p==1)
				{
					if(intV>=65&&intV<=90)
						couple[p++] = intV+32;
					else if(intV>=97&&intV<=122)
						couple[p++] = intV;
					else
						of.writeByte(intV);	//inChar is not a letter
				}
				
				//write out a couple of letters
				if(p==2){
					if(couple[0]==couple[1])
					{
						substitition(couple[0], lastChar);
						couple[0]=couple[1];
						p=1;
					}
					else
					{
						substitition(couple[0],couple[1]);
						p=0;
					}					
				}
			}
			if(p==1)
				substitition(couple[0], lastChar);
    		sf.close();
    		of.close();			
		}//end try
		catch(FileNotFoundException ex){
			System.out.println("Err when accessing source file");
		}
		catch(IOException ex){
			System.out.println("Err when reading in");
		}
	}// cipher()
	
	public void substitition(int c0, int c1){
		int c0x=4,c0y=4,c1x=4,c1y=4;
		//find c0 in the table, and get the index
		if(c0!=lastChar){
			for(int i=0;i<5;i++)
				for(int j=0;j<5;j++)
				{
					if(c0==table[i][j])
						{
							c0x=i;
							c0y=j;
						}
				}
		}
		
		//find c1 in the table, and get the index
		if(c1!=lastChar){
			for(int i=0;i<5;i++)
				for(int j=0;j<5;j++)
				{
					if(c1==table[i][j])
						{
							c1x=i;
							c1y=j;
						}
				}
		}

		if(c0x==c1x){	//c0,c1 are in the same row			
			c0y = (c0y+1)%5;
			c1y = (c1y+1)%5;
			try{
				this.of.writeByte(table[c0x][c0y]);
				this.of.writeByte(table[c1x][c1y]);
			}
			catch(IOException ex){
				System.out.println("Err when writing out");
			}
		}
		
		else if(c0y==c1y){	//c0,c1 are in the same column			
			c0x = (c0x+1)%5;
			c1x = (c1x+1)%5;
			try{
				this.of.writeByte(table[c0x][c0y]);
				this.of.writeByte(table[c1x][c1y]);
			}
			catch(IOException ex){
				System.out.println("Err when writing out");
			}
		}
			
		else{
			try{
				this.of.writeByte(table[c0x][c1y]);
				this.of.writeByte(table[c1x][c0y]);
			}
			catch(IOException ex){
				System.out.println("Err when writing out");
			}
		}
	}// substitution(int c0, int c1)
	
	public void decipher(){
		try{
        	File sourceFile = new File(srcFilePath);	
        	File outFile = new File(prnPath+"\\Playfair decryption.txt");
			outFile.createNewFile();
			
        	RandomAccessFile sf = new RandomAccessFile(sourceFile,"r");
    		of = new RandomAccessFile(outFile,"rw");	
		    		
    		Byte inChar;	// one byte read in
    		int intV;		//intValue of inChar
    		int[] couple={0,0};	//a couple of letters
    		int p=0;  // pointer of couple[2]
			while(sf.getFilePointer()<sourceFile.length()){    		
				inChar = sf.readByte();
				intV = inChar.intValue();
				
				//divide input strings into couples of letters
				if(p==0||p==1)
				{
					if(intV>=65&&intV<=90)
						couple[p++] = intV+32;
					else if(intV>=97&&intV<=122)
						couple[p++] = intV;
					else
						of.writeByte(intV);	//inChar is not a letter
				}
				
				//write out a couple of letters
				if(p==2){
					if(couple[0]==couple[1])
					{
						resub(couple[0], lastChar);
						couple[0]=couple[1];
						p=1;
					}
					else
					{
						resub(couple[0],couple[1]);
						p=0;
					}					
				}
			}
			if(p==1)
				resub(couple[0], lastChar);
    		sf.close();
    		of.close();			
		}//end try
		catch(FileNotFoundException ex){
			System.out.println("Err when accessing source file");
		}
		catch(IOException ex){
			System.out.println("Err when reading in");
		}
	}// decipher()
	
	public void resub(int c0, int c1){
		int c0x=4,c0y=4,c1x=4,c1y=4;
		//find c0 in the table, and get the index
		if(c0!=lastChar){
			for(int i=0;i<5;i++)
				for(int j=0;j<5;j++)
				{
					if(c0==table[i][j])
						{
							c0x=i;
							c0y=j;
						}
				}
		}
		
		//find c1 in the table, and get the index
		if(c1!=lastChar){
			for(int i=0;i<5;i++)
				for(int j=0;j<5;j++)
				{
					if(c1==table[i][j])
						{
							c1x=i;
							c1y=j;
						}
				}
		}

		if(c0x==c1x){	//c0,c1 are in the same row			
			c0y = (c0y-1)%5;
			c1y = (c1y-1)%5;
			try{
				this.of.writeByte(table[c0x][c0y]);
				this.of.writeByte(table[c1x][c1y]);
			}
			catch(IOException ex){
				System.out.println("Err when writing out");
			}
		}
		
		else if(c0y==c1y){	//c0,c1 are in the same column			
			c0x = (c0x-1)%5;
			c1x = (c1x-1)%5;
			try{
				this.of.writeByte(table[c0x][c0y]);
				this.of.writeByte(table[c1x][c1y]);
			}
			catch(IOException ex){
				System.out.println("Err when writing out");
			}
		}
			
		else{
			try{
				this.of.writeByte(table[c0x][c1y]);
				this.of.writeByte(table[c1x][c0y]);
			}
			catch(IOException ex){
				System.out.println("Err when writing out");
			}
		}
	}//resub(int c0,int c1)
}

⌨️ 快捷键说明

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