?? monoalphabetic.java
字號:
import java.io.*;
public class Monoalphabetic {
private String srcFilePath;
private String prnPath;
public Monoalphabetic(String filePath,String parentPath){
this.srcFilePath = filePath;
this.prnPath = parentPath;
}
public void cipher(){
try{
File sourceFile = new File(srcFilePath);
File outFile = new File(prnPath+"\\Monoalphabetic encryption.txt");
outFile.createNewFile();
// assuming that we only need to access data from .txt files
RandomAccessFile sf = new RandomAccessFile(sourceFile, "r");
RandomAccessFile of = new RandomAccessFile(outFile, "rw");
Byte inChar; // one byte read in
int i; //intValue of the inChar. It's to be written out after the substitution
int key = 8; //the key for encryption can be changed
while(sf.getFilePointer()<sourceFile.length()){
inChar = sf.readByte(); //讀寫二進(jìn)制文件
i = inChar.intValue();
if((i>=65)&&(i<=90))
{
i += key;
if(i>90)
i-=26;
of.writeByte(i);
}
else if(i>=97&&i<=122)
{
i += key;
if(i>122)
i-=26;
of.writeByte(i);
}
else
of.writeByte(i);
}
sf.close();
of.close();
}
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+"\\Monoalphabetic decryption.txt");
outFile.createNewFile();
RandomAccessFile sf = new RandomAccessFile(sourceFile, "r");
RandomAccessFile of = new RandomAccessFile(outFile, "rw");
Byte inChar;
int i;
int key = 8;
while(sf.getFilePointer()<sourceFile.length()){
inChar = sf.readByte();
i = inChar.intValue();
if((i>=65)&&(i<=90))
{
i -= key;
if(i<65)
i+=26;
of.writeByte(i);
}
else if(i>=97&&i<=122)
{
i -= key;
if(i<97)
i+=26;
of.writeByte(i);
}
else
of.writeByte(i);
}
sf.close();
of.close();
}
catch(FileNotFoundException ex){
System.out.println("Err when accessing source file");
}
catch(IOException ex){
System.out.println("IOException");
}
}// Decipher()
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -