(一)、加密:
Encrypt.java类:
package cn.hncu;
import java.util.ArrayList;
import java.util.Arrays;
public class Encrypt {
private String message;
private String key;
public String getMessage () {
return message;
}
public void setMessage (String message) {
this .message = message;
}
public String getKey () {
return key;
}
public void setKey (String key) {
this .key = key;
}
public Encrypt () {
}
public char [][] letterTable (){
char [][] a=new char [6 ][6 ];
a[0 ][1 ]='A' ;a[0 ][2 ]='D' ;a[0 ][3 ]='F' ;a[0 ][4 ]='G' ;a[0 ][5 ]='X' ;
a[1 ][0 ]='A' ;a[2 ][0 ]='D' ;a[3 ][0 ]='F' ;a[4 ][0 ]='G' ;a[5 ][0 ]='X' ;
a[1 ][1 ]='b' ;a[1 ][2 ]='t' ;a[1 ][3 ]='a' ;a[1 ][4 ]='l' ;a[1 ][5 ]='p' ;
a[2 ][1 ]='d' ;a[2 ][2 ]='h' ;a[2 ][3 ]='o' ;a[2 ][4 ]='z' ;a[2 ][5 ]='k' ;
a[3 ][1 ]='q' ;a[3 ][2 ]='f' ;a[3 ][3 ]='v' ;a[3 ][4 ]='s' ;a[3 ][5 ]='n' ;
a[4 ][1 ]='g' ;a[4 ][2 ]='j' ;a[4 ][3 ]='c' ;a[4 ][4 ]='u' ;a[4 ][5 ]='x' ;
a[5 ][1 ]='m' ;a[5 ][2 ]='r' ;a[5 ][3 ]='e' ;a[5 ][4 ]='w' ;a[5 ][5 ]='y' ;
return a;
}
public ArrayList midRes (){
char [] chMess=message.toCharArray();
char [][] chLetter=letterTable();
ArrayList chList=new ArrayList();
ArrayList strList=new ArrayList();
ArrayList list=new ArrayList();
list.add(chList);
list.add(strList);
for (int k=0 ;kfor(int i=0 ;i
(二)解密:
Decrypted.java类:
package cn.hncu;
import java.util.Arrays;
public class Decrypted {
private String ciphertext;
private String key;
public Decrypted (){
}
public String getCiphertext () {
return ciphertext;
}
public void setCiphertext (String ciphertext) {
this .ciphertext = ciphertext;
}
public String getKey () {
return key;
}
public void setKey (String key) {
this .key = key;
}
private char [][] sortLetter (){
String key2=key;
char [] chKey=key.toCharArray();
char [] chKey2=key2.toCharArray();
Arrays.sort(chKey2);
char [] chCiphertext=ciphertext.replaceAll(" " , "" ).toCharArray();
int row=chCiphertext.length/chKey.length;
int col=chKey.length;
char [][] chCipher=new char [row][col];
for (int i=0 ;ifor(int j=0 ;jint len=col*j;
if (chKey[i]==chKey2[j]){
for (int i2=0 ;i2
(三)、值对象:
FileMessage.java类:
package cn.hncu;
public class FileMessage {
private String fileurlMessSrc;
private String fileurlCry;
private String fileurlCry2;
private String fileurlMessSrc2;
private String key;
private char [][] letters;
public String getFileurlMessSrc () {
return fileurlMessSrc;
}
public void setFileurlMessSrc (String fileurlMessSrc) {
this .fileurlMessSrc = fileurlMessSrc;
}
public String getFileurlCry () {
return fileurlCry;
}
public void setFileurlCry (String fileurlCry) {
this .fileurlCry = fileurlCry;
}
public String getFileurlCry2 () {
return fileurlCry2;
}
public void setFileurlCry2 (String fileurlCry2) {
this .fileurlCry2 = fileurlCry2;
}
public String getFileurlMessSrc2 () {
return fileurlMessSrc2;
}
public void setFileurlMessSrc2 (String fileurlMessSrc2) {
this .fileurlMessSrc2 = fileurlMessSrc2;
}
public String getKey () {
return key;
}
public void setKey (String key) {
this .key = key;
}
public char [][] getLetters () {
return letters;
}
public void setLetters (char [][] letters) {
this .letters = letters;
}
}
(四)、文件工具:
FileUtils.java类:
package cn.hncu .utils
import java.awt .HeadlessException
import java.io .BufferedReader
import java.io .BufferedWriter
import java.io .File
import java.io .FileNotFoundException
import java.io .FileReader
import java.io .FileWriter
import java.io .IOException
import java.util .ArrayList
import javax.swing .JFileChooser
import cn.hncu .Decrypted
import cn.hncu .Encrypt
import cn.hncu .FileMessage
public class FileUtils {
public static String openFile() {
File file=null
try {
JFileChooser jfile=new JFileChooser()
jfile.showDialog (null, "打开" )
file=jfile.getSelectedFile ()
System.out .println (file.toString ())
} catch (HeadlessException e) {
// TODO Auto-generated catch block
e.printStackTrace ()
}finally{
return file.toString ()
}
}
}
(五)、图形界面:
package cn.hncu .ui
import java.awt .event .ActionEvent
import java.awt .event .MouseEvent
import java.io .BufferedReader
import java.io .BufferedWriter
import java.io .File
import java.io .FileReader
import java.io .FileWriter
import java.io .IOException
import javax.swing .JOptionPane
import cn.hncu .Decrypted
import cn.hncu .Encrypt
import cn.hncu .FileMessage
import cn.hncu .utils .FileUtils
public class mainFrame extends javax.swing .JFrame {
public mainFrame() {
initComponents()
setTitle("ADFGX密码加密和解密" )
}
//GEN-BEGIN:initComponents
// "collapsed" desc="Generated Code" >
//初始化所有参数:
FileMessage filemess=new FileMessage()
private void initComponents() {
jSplitPane1 = new javax.swing .JSplitPane ()
jLabel1 = new javax.swing .JLabel ()
jLabel2 = new javax.swing .JLabel ()
tfdkey = new javax.swing .JTextField ()
jLabel3 = new javax.swing .JLabel ()
jScrollPane1 = new javax.swing .JScrollPane ()
jtrepl = new javax.swing .JTextArea ()
btnSave = new javax.swing .JButton ()
jLabel4 = new javax.swing .JLabel ()
tfdCry2 = new javax.swing .JTextField ()
jLabel5 = new javax.swing .JLabel ()
tfdmessSrc2 = new javax.swing .JTextField ()
jLabel6 = new javax.swing .JLabel ()
tfdmessSrc = new javax.swing .JTextField ()
jLabel7 = new javax.swing .JLabel ()
tfdCry = new javax.swing .JTextField ()
jLabel8 = new javax.swing .JLabel ()
jScrollPane2 = new javax.swing .JScrollPane ()
jtRepRes = new javax.swing .JTextArea ()
jLabel9 = new javax.swing .JLabel ()
jLabel10 = new javax.swing .JLabel ()
jScrollPane3 = new javax.swing .JScrollPane ()
jtMess = new javax.swing .JTextArea ()
jScrollPane4 = new javax.swing .JScrollPane ()
jtCry = new javax.swing .JTextArea ()
btnEncrypt = new javax.swing .JButton ()
btnDecrypt = new javax.swing .JButton ()
menuBar = new javax.swing .JMenuBar ()
fileMenu = new javax.swing .JMenu ()
openMenuItem = new javax.swing .JMenuItem ()
saveMenuItem = new javax.swing .JMenuItem ()
saveAsMenuItem = new javax.swing .JMenuItem ()
exitMenuItem = new javax.swing .JMenuItem ()
editMenu = new javax.swing .JMenu ()
cutMenuItem = new javax.swing .JMenuItem ()
copyMenuItem = new javax.swing .JMenuItem ()
pasteMenuItem = new javax.swing .JMenuItem ()
deleteMenuItem = new javax.swing .JMenuItem ()
helpMenu = new javax.swing .JMenu ()
contentsMenuItem = new javax.swing .JMenuItem ()
aboutMenuItem = new javax.swing .JMenuItem ()
setDefaultCloseOperation(javax.swing .WindowConstants .EXIT _ON_CLOSE)
setFocusCycleRoot(false)
setMinimumSize(new java.awt .Dimension (1400 , 700 ))
getContentPane().setLayout (null)
jLabel1.setFont (new java.awt .Font ("宋体" , 1 , 24 ))
jLabel1.setForeground (new java.awt .Color (255 , 0 , 0 ))
jLabel1.setText ("ADFGX\u5bc6\u7801" )
getContentPane().add (jLabel1)
jLabel1.setBounds (490 , 10 , 130 , 30 )
jLabel2.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
jLabel2.setText ("\u8bbe\u7f6e\u5bc6\u94a5\uff1a" )
getContentPane().add (jLabel2)
jLabel2.setBounds (60 , 80 , 90 , 20 )
tfdkey.setFont (new java.awt .Font ("Microsoft YaHei UI" , 0 , 18 ))
getContentPane().add (tfdkey)
tfdkey.setBounds (130 , 80 , 400 , 30 )
jLabel3.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
jLabel3.setText ("\u8bbe\u7f6e\u66ff\u6362\u65b9\u683c\uff1a" )
getContentPane().add (jLabel3)
jLabel3.setBounds (60 , 120 , 110 , 20 )
jtrepl.setColumns (20 )
jtrepl.setFont (new java.awt .Font ("Monospaced" , 1 , 18 ))
jtrepl.setRows (5 )
jScrollPane1.setViewportView (jtrepl)
getContentPane().add (jScrollPane1)
jScrollPane1.setBounds (60 , 150 , 470 , 180 )
/////初始化字母表
Encrypt en = new Encrypt()
final char[][] chs = en.letterTable ()
for (int i = 0
StringBuffer sb = new StringBuffer()
for (int j = 0
if (j == 0 ) {
sb.append (chs[i][j])
} else {
sb.append ("\t" + chs[i][j])
}
}
String s = new String(sb)
jtrepl.append (s + "\n" )
s = ""
}
btnSave.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
btnSave.setText ("\u4fdd\u5b58\u8bbe\u7f6e" )
btnSave.addActionListener (new java.awt .event .ActionListener () {
public void actionPerformed(java.awt .event .ActionEvent evt) {
btnSaveActionPerformed(evt)
}
private void btnSaveActionPerformed(ActionEvent evt) {
String key=tfdkey.getText ()
filemess.setKey (key)
filemess.setLetters (chs)
System.out .println ("保存设置" )
}
})
getContentPane().add (btnSave)
btnSave.setBounds (230 , 350 , 100 , 30 )
jLabel4.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
jLabel4.setText ("\u660e\u6587\u6587\u4ef6\uff1a" )
getContentPane().add (jLabel4)
jLabel4.setBounds (870 , 140 , 80 , 30 )
tfdCry2.setFont (new java.awt .Font ("Microsoft YaHei UI" , 0 , 18 ))
tfdCry2.addMouseListener (new java.awt .event .MouseAdapter () {
public void mouseClicked(java.awt .event .MouseEvent evt) {
tfdCry2MouseClicked(evt)
}
private void tfdCry2MouseClicked(java.awt .event .MouseEvent evt) {
System.out .println ("解密密文文件" )
String fileurlCry2=FileUtils.openFile ()
tfdCry2.setText (fileurlCry2)
filemess.setFileurlCry 2(fileurlCry2)
}
})
getContentPane().add (tfdCry2)
tfdCry2.setBounds (630 , 140 , 210 , 30 )
jLabel5.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
jLabel5.setText ("\u5bc6\u6587\u6587\u4ef6\uff1a" )
getContentPane().add (jLabel5)
jLabel5.setBounds (550 , 140 , 80 , 30 )
tfdmessSrc2.setFont (new java.awt .Font ("Microsoft YaHei UI" , 0 , 18 ))
tfdmessSrc2.addMouseListener (new java.awt .event .MouseAdapter () {
public void mouseClicked(java.awt .event .MouseEvent evt) {
tfdmessSrc2MouseClicked(evt)
}
private void tfdmessSrc2MouseClicked(MouseEvent evt) {
System.out .println ("解密明文文件" )
String fileurlMessSrc2=FileUtils.openFile ()
tfdmessSrc2.setText (fileurlMessSrc2)
filemess.setFileurlMessSrc 2(fileurlMessSrc2)
}
})
getContentPane().add (tfdmessSrc2)
tfdmessSrc2.setBounds (950 , 140 , 150 , 30 )
jLabel6.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
jLabel6.setText ("\u5bc6\u6587\u6587\u4ef6\uff1a" )
getContentPane().add (jLabel6)
jLabel6.setBounds (870 , 80 , 80 , 30 )
tfdmessSrc.setFont (new java.awt .Font ("Microsoft YaHei UI" , 0 , 18 ))
tfdmessSrc.addMouseListener (new java.awt .event .MouseAdapter () {
public void mouseClicked(java.awt .event .MouseEvent evt) {
tfdmessSrcMouseClicked(evt)
}
private void tfdmessSrcMouseClicked(MouseEvent evt) {
System.out .println ("加密明文文件" )
String fileurlMessSrc=FileUtils.openFile ()
filemess.setFileurlMessSrc (fileurlMessSrc)
tfdmessSrc.setText (fileurlMessSrc)
}
})
getContentPane().add (tfdmessSrc)
tfdmessSrc.setBounds (630 , 80 , 210 , 30 )
jLabel7.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
jLabel7.setText ("\u660e\u6587\u6587\u4ef6\uff1a" )
getContentPane().add (jLabel7)
jLabel7.setBounds (550 , 80 , 80 , 30 )
tfdCry.setFont (new java.awt .Font ("Microsoft YaHei UI" , 0 , 18 ))
tfdCry.addMouseListener (new java.awt .event .MouseAdapter () {
public void mouseClicked(java.awt .event .MouseEvent evt) {
tfdCryMouseClicked(evt)
}
private void tfdCryMouseClicked(MouseEvent evt) {
System.out .println ("加密密文文件" )
String fileurlCry=FileUtils.openFile ()
filemess.setFileurlCry (fileurlCry)
tfdCry.setText (fileurlCry)
}
})
getContentPane().add (tfdCry)
tfdCry.setBounds (950 , 80 , 150 , 30 )
jLabel8.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
jLabel8.setText ("\u66ff\u6362\u7ed3\u679c\uff1a" )
getContentPane().add (jLabel8)
jLabel8.setBounds (550 , 240 , 80 , 30 )
jtRepRes.setColumns (20 )
jtRepRes.setEditable (false)
jtRepRes.setFont (new java.awt .Font ("Monospaced" , 1 , 18 ))
jtRepRes.setRows (5 )
jScrollPane2.setViewportView (jtRepRes)
getContentPane().add (jScrollPane2)
jScrollPane2.setBounds (630 , 200 , 650 , 90 )
jLabel9.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
jLabel9.setText ("\u660e\u6587\uff1a" )
getContentPane().add (jLabel9)
jLabel9.setBounds (560 , 300 , 50 , 30 )
jLabel10.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
jLabel10.setText ("\u5bc6\u6587\uff1a" )
getContentPane().add (jLabel10)
jLabel10.setBounds (930 , 310 , 50 , 20 )
jtMess.setColumns (20 )
jtMess.setEditable (false)
jtMess.setFont (new java.awt .Font ("Monospaced" , 1 , 18 ))
jtMess.setRows (5 )
jScrollPane3.setViewportView (jtMess)
getContentPane().add (jScrollPane3)
jScrollPane3.setBounds (630 , 310 , 290 , 250 )
jtCry.setColumns (20 )
jtCry.setEditable (false)
jtCry.setFont (new java.awt .Font ("Monospaced" , 1 , 18 ))
jtCry.setRows (5 )
jScrollPane4.setViewportView (jtCry)
getContentPane().add (jScrollPane4)
jScrollPane4.setBounds (980 , 310 , 300 , 250 )
btnEncrypt.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
btnEncrypt.setText ("\u52a0\u5bc6" )
btnEncrypt.addActionListener (new java.awt .event .ActionListener () {
public void actionPerformed(java.awt .event .ActionEvent evt) {
btnEncryptActionPerformed(evt)
}
private void btnEncryptActionPerformed(ActionEvent evt) {
System.out .println ("按钮:加密" )
//FileUtils.encryptFile (filemess)
Encrypt en=new Encrypt()
en.setKey (filemess.getKey ())
BufferedReader br=null
BufferedWriter bw=null
try {
br=new BufferedReader(new FileReader(new File(filemess.getFileurlMessSrc ())))
bw=new BufferedWriter(new FileWriter(new File(filemess.getFileurlCry ())))
String str=null
while((str=br.readLine ())!=null){
jtMess.append (str+"\n" )
en.setMessage (str.replaceAll (" " , "" ).toLowerCase ())
String s=new String(en.sortLetter ())
bw.write (s)
jtCry.append (s+"\n" )
String s1=en.midRes ().get (0 ).toString ().replaceAll ("," , " " )
String s2=en.midRes ().get (1 ).toString ().replaceAll ("," , "" )
jtRepRes.append (s1+"\n" )
jtRepRes.append (s2+"\n" )
bw.write ("\r\n" )
bw.flush ()
}
} catch (Exception e) {
e.printStackTrace ()
}finally{
try {
br.close ()
bw.close ()
} catch (IOException e) {
e.printStackTrace ()
}
JOptionPane.showMessageDialog (null, "文件加密成功!" )
}
}
})
getContentPane().add (btnEncrypt)
btnEncrypt.setBounds (1120 , 80 , 80 , 30 )
btnDecrypt.setFont (new java.awt .Font ("宋体" , 1 , 14 ))
btnDecrypt.setText ("\u89e3\u5bc6" )
btnDecrypt.addActionListener (new java.awt .event .ActionListener () {
public void actionPerformed(java.awt .event .ActionEvent evt) {
btnDecryptActionPerformed(evt)
}
private void btnDecryptActionPerformed(ActionEvent evt) {
System.out .println ("按钮:解密" )
Decrypted de=new Decrypted()
de.setKey (filemess.getKey ())
BufferedReader br=null
BufferedWriter bw=null
try {
br=new BufferedReader(new FileReader(new File(filemess.getFileurlCry 2())))
bw=new BufferedWriter(new FileWriter(new File(filemess.getFileurlMessSrc 2())))
String str=null
while((str=br.readLine ())!=null){
System.out .println ("密文:" +str)
jtCry.append (str+"\n" )
de.setCiphertext ((str.replaceAll (" " , "" ).toUpperCase ()))
String s=new String(de.shift ())
System.out .println ("明文:" +s)
bw.write (s)
jtMess.append (s+"\n" )
bw.write ("\r\n" )
bw.flush ()
}
} catch (Exception e) {
e.printStackTrace ()
}finally{
try {
br.close ()
bw.close ()
} catch (IOException e) {
e.printStackTrace ()
}
}
JOptionPane.showMessageDialog (null, "文件解密成功!" )
}
})
getContentPane().add (btnDecrypt)
btnDecrypt.setBounds (1120 , 140 , 80 , 30 )
fileMenu.setText ("File" )
openMenuItem.setText ("Open" )
fileMenu.add (openMenuItem)
saveMenuItem.setText ("Save" )
fileMenu.add (saveMenuItem)
saveAsMenuItem.setText ("Save As ..." )
fileMenu.add (saveAsMenuItem)
exitMenuItem.setText ("Exit" )
exitMenuItem.addActionListener (new java.awt .event .ActionListener () {
public void actionPerformed(java.awt .event .ActionEvent evt) {
exitMenuItemActionPerformed(evt)
}
})
fileMenu.add (exitMenuItem)
menuBar.add (fileMenu)
editMenu.setText ("Edit" )
cutMenuItem.setText ("Cut" )
editMenu.add (cutMenuItem)
copyMenuItem.setText ("Copy" )
editMenu.add (copyMenuItem)
pasteMenuItem.setText ("Paste" )
editMenu.add (pasteMenuItem)
deleteMenuItem.setText ("Delete" )
editMenu.add (deleteMenuItem)
menuBar.add (editMenu)
helpMenu.setText ("Help" )
contentsMenuItem.setText ("Contents" )
helpMenu.add (contentsMenuItem)
aboutMenuItem.setText ("About" )
helpMenu.add (aboutMenuItem)
menuBar.add (helpMenu)
setJMenuBar(menuBar)
pack()
}//
//GEN-END:initComponents
private void exitMenuItemActionPerformed(java.awt .event .ActionEvent evt) {// GEN-FIRST:event_exitMenuItemActionPerformed
System.exit (0 )
}// GEN-LAST:event_exitMenuItemActionPerformed
public static void main(String args[]) {
java.awt .EventQueue .invokeLater (new Runnable() {
public void run() {
new mainFrame().setVisible (true)
}
})
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing .JMenuItem aboutMenuItem
private javax.swing .JButton btnDecrypt
private javax.swing .JButton btnEncrypt
private javax.swing .JButton btnSave
private javax.swing .JMenuItem contentsMenuItem
private javax.swing .JMenuItem copyMenuItem
private javax.swing .JMenuItem cutMenuItem
private javax.swing .JMenuItem deleteMenuItem
private javax.swing .JMenu editMenu
private javax.swing .JMenuItem exitMenuItem
private javax.swing .JMenu fileMenu
private javax.swing .JMenu helpMenu
private javax.swing .JLabel jLabel1
private javax.swing .JLabel jLabel10
private javax.swing .JLabel jLabel2
private javax.swing .JLabel jLabel3
private javax.swing .JLabel jLabel4
private javax.swing .JLabel jLabel5
private javax.swing .JLabel jLabel6
private javax.swing .JLabel jLabel7
private javax.swing .JLabel jLabel8
private javax.swing .JLabel jLabel9
private javax.swing .JScrollPane jScrollPane1
private javax.swing .JScrollPane jScrollPane2
private javax.swing .JScrollPane jScrollPane3
private javax.swing .JScrollPane jScrollPane4
private javax.swing .JSplitPane jSplitPane1
private javax.swing .JTextArea jtCry
private javax.swing .JTextArea jtMess
private javax.swing .JTextArea jtRepRes
private javax.swing .JTextArea jtrepl
private javax.swing .JMenuBar menuBar
private javax.swing .JMenuItem openMenuItem
private javax.swing .JMenuItem pasteMenuItem
private javax.swing .JMenuItem saveAsMenuItem
private javax.swing .JMenuItem saveMenuItem
private javax.swing .JTextField tfdCry
private javax.swing .JTextField tfdCry2
private javax.swing .JTextField tfdkey
private javax.swing .JTextField tfdmessSrc
private javax.swing .JTextField tfdmessSrc2
// End of variables declaration//GEN-END:variables
}
(六)、明文文件、密文文件、解密后文件:
(七)、运行结果:
1、完整运行结果:
2、加密结果:
得到明文路径、密文路径,点击加密:
得到加密后的结果:
3、解密结果: