加密解密小程序

以前米事写的小程序 - -
操作是写入密匙(限定是长度最多为10的数字串)/字符 实时转换
加密方法是对每个字符*(除了字母之外所有字符也都可行 - -)进行向后移位的操作。

【程式截图】
 

【源码】
Decode.java

  1. /**
  2. * 解密算法
  3. */
  4. public class Decode {
  5.        
  6.         /**
  7.          * 解密算法
  8.          * @param keyStr 密匙
  9.          * @param ostr 密文
  10.          * @return 明文
  11.          */
  12.        
  13.         public String decodeStr ( String keyStr, String ostr ) {
  14.                 char [ ] str = ostr. toCharArray ( );
  15.                 int [ ] key = new int [ 20 ];
  16.        
  17.                 for ( int i= 0;i<keyStr. length ( );i++ ) {
  18.                         key [i ]= ( ( int )keyStr. charAt (i ) ) -48;
  19.                 }
  20.                
  21.                 for ( int i= 0;i<ostr. length ( );i++ ) {
  22.                         str [i ] = ( char ) ( ( int )str [i ]-key [i%keyStr. length ( ) ] );
  23.                 }
  24.                
  25.                 return String. valueOf (str );
  26.         }
  27.  
  28. }

Encode.java

  1. /**
  2. * 加密算法
  3. */
  4. public class Encode {
  5.  
  6.         /**
  7.          * 加密算法
  8.          * @param keyStr 密匙
  9.          * @param str 明文
  10.          * @return 密文
  11.          */
  12.         public String encodeStr ( String keyStr, String str ) {
  13.                 char [ ] ostr = str. toCharArray ( );
  14.                 int [ ] key = new int [ 20 ];
  15.                
  16.                 for ( int i= 0;i<keyStr. length ( );i++ ) {
  17.                         key [i ]= ( ( int )keyStr. charAt (i ) ) -48;
  18.                 }
  19.                
  20.                 for ( int i = 0;i<str. length ( );i++ ) {
  21.                         ostr [i ] = ( char ) ( ( int )ostr [i ]+key [i%keyStr. length ( ) ] );
  22.                 }
  23.                
  24.                 return String. valueOf (ostr );
  25.         }
  26.        
  27. }

 

GUIFrame.java

  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.*;
  6. import javax.swing.border.Border;
  7. import javax.swing.event.DocumentEvent;
  8. import javax.swing.event.DocumentListener;
  9.  
  10. /**
  11. * GUI界面
  12. */
  13.  
  14. public class GUIFrame extends JFrame {
  15.        
  16.         /**     加密选项 */
  17.         JRadioButton encodeBtn = new JRadioButton ( "加密", true );
  18.         /**     解密选项 */
  19.         JRadioButton decodeBtn = new JRadioButton ( "解密", false );
  20.        
  21.         /** 容器 */
  22.         JPanel panel = new JPanel ( );
  23.         /** 边框 */
  24.         Border border = BorderFactory. createTitledBorder ( "加密" );
  25.        
  26.         /**     加密提示 */
  27.         JLabel encodeLabel = new JLabel ( "明文" );
  28.         /**     解密提示 */
  29.         JLabel decodeLabel = new JLabel ( "密文" );
  30.         /**     密匙提示 */
  31.         JLabel keyLabel = new JLabel ( "密匙(最多10个数字) " );
  32.        
  33.         /**     明文框 */
  34.         JTextField encodeStr = new JTextField ( 20 );
  35.         /**     密文框 */
  36.         JTextField decodeStr = new JTextField ( 20 );
  37.         /** 密匙框 */
  38.         JTextField keyStr = new JTextField ( 10 );
  39.        
  40.         /**     处理行为 */
  41.         EncodeStrAction strEncodeAction = new EncodeStrAction ( );
  42.         DecodeStrAction strDecodeAction = new DecodeStrAction ( );
  43.         KeyEncodeAction keyEncodeAction = new KeyEncodeAction ( );
  44.         KeyDecodeAction keyDecodeAction = new KeyDecodeAction ( );
  45.        
  46.         /**
  47.          * 构造方法
  48.          */
  49.         public GUIFrame ( ) {
  50.                 this. setSize ( 300, 200 );
  51.                 this. setTitle ( "加密解密小程序 - -" );
  52.                
  53.                 this. setLayout ( new BorderLayout ( ) );
  54.                
  55.                 JPanel chooseBtn = new JPanel ( );
  56.                
  57.                 encodeBtn. addActionListener ( new EncodeAction ( ) );
  58.                 decodeBtn. addActionListener ( new DecodeAction ( ) );
  59.                
  60.                 chooseBtn. add (encodeBtn );
  61.                 chooseBtn. add (decodeBtn );
  62.                
  63.                 this. add (chooseBtn, "North" );
  64.                
  65.                 panel. setSize ( 300, 300 );
  66.                 panel. setLayout ( new FlowLayout ( FlowLayout. CENTER ) );
  67.                 panel. setBorder (border );
  68.                
  69.                 this. add (panel, "Center" );
  70.                
  71.                 this. add ( new JLabel ( "Linyq@SPOTO. 2008  ", JLabel. RIGHT ), "South" );
  72.                
  73.                 showEncodeFrame ( );
  74.                
  75.                 this. setVisible ( true );
  76.         }
  77.        
  78.         /**     加密选项 */
  79.         private class EncodeAction implements ActionListener {
  80.  
  81.                 public void actionPerformed ( ActionEvent e ) {
  82.                         encodeBtn. setSelected ( true );
  83.                         decodeBtn. setSelected ( false );
  84.                         setBorderTitle ( "加密" );
  85.                         showEncodeFrame ( );
  86.                 }
  87.                
  88.         }
  89.        
  90.         /** 解密选项 */
  91.         private class DecodeAction implements ActionListener {
  92.  
  93.                 public void actionPerformed ( ActionEvent e ) {
  94.                         encodeBtn. setSelected ( false );
  95.                         decodeBtn. setSelected ( true );
  96.                         setBorderTitle ( "解密" );
  97.                         showDecodeFrame ( );
  98.                 }
  99.                
  100.         }
  101.        
  102.         /**     清理容器 */
  103.         private void clear ( ) {
  104.                 panel. remove (encodeLabel );
  105.                 panel. remove (decodeLabel );
  106.                 panel. remove (keyLabel );
  107.                 panel. remove (encodeStr );
  108.                 panel. remove (decodeStr );
  109.                 panel. remove (keyStr );
  110.         }
  111.        
  112.         /**     设置标题 */
  113.         private void setBorderTitle ( String title ) {
  114.                 panel. setBorder ( null );
  115.                 border = BorderFactory. createTitledBorder (title );
  116.                 panel. setBorder (border );
  117.         }
  118.        
  119.         /**     加密操作 */
  120.         private void showEncodeFrame ( ) {
  121.                
  122.                 clear ( );
  123.                
  124.                 encodeStr. setEditable ( true );
  125.                 panel. add (encodeLabel );panel. add (encodeStr );
  126.                 panel. add (keyLabel );panel. add (keyStr );
  127.                
  128.                 decodeStr. setEditable ( false );
  129.                 panel. add (decodeLabel );panel. add (decodeStr );
  130.                
  131.                 encodeStr. getDocument ( ). addDocumentListener (strEncodeAction );
  132.                 decodeStr. getDocument ( ). removeDocumentListener (strDecodeAction );
  133.                
  134.                 keyStr. getDocument ( ). removeDocumentListener (keyDecodeAction );
  135.                 keyStr. getDocument ( ). addDocumentListener (keyEncodeAction );
  136.                
  137.         }
  138.        
  139.         /**     解密操作 */
  140.         private void showDecodeFrame ( ) {
  141.                 clear ( );
  142.                
  143.                 decodeStr. setEditable ( true );
  144.                 panel. add (decodeLabel );panel. add (decodeStr );
  145.                 panel. add (keyLabel );panel. add (keyStr );
  146.                
  147.                 encodeStr. setEditable ( false );
  148.                 panel. add (encodeLabel );panel. add (encodeStr );
  149.                
  150.                 encodeStr. getDocument ( ). removeDocumentListener (strEncodeAction );
  151.                 decodeStr. getDocument ( ). addDocumentListener (strDecodeAction );
  152.                
  153.                 keyStr. getDocument ( ). removeDocumentListener (keyEncodeAction );
  154.                 keyStr. getDocument ( ). addDocumentListener (keyDecodeAction );
  155.                
  156.         }
  157.        
  158.         /**     加密处理 */
  159.         private class EncodeStrAction implements DocumentListener {
  160.  
  161.                 public void changedUpdate ( DocumentEvent e ) {
  162.                         updateDecodeStr ( );
  163.                 }
  164.  
  165.                 public void insertUpdate ( DocumentEvent e ) {
  166.                         updateDecodeStr ( );
  167.                 }
  168.  
  169.                 public void removeUpdate ( DocumentEvent e ) {
  170.                         updateDecodeStr ( );
  171.                 }
  172.                
  173.         }
  174.        
  175.         /**     解密处理 */
  176.         private class DecodeStrAction implements DocumentListener {
  177.  
  178.                 public void changedUpdate ( DocumentEvent e ) {
  179.                         updateEncodeStr ( );
  180.                 }
  181.  
  182.                 public void insertUpdate ( DocumentEvent e ) {
  183.                         updateEncodeStr ( );
  184.                 }
  185.  
  186.                 public void removeUpdate ( DocumentEvent e ) {
  187.                         updateEncodeStr ( );
  188.                 }
  189.                
  190.         }
  191.        
  192.         /** 加密更新 */
  193.         private void updateDecodeStr ( ) {
  194.                 //      判定密匙是否合法
  195.                 if (chk (keyStr. getText ( ) ) ) {
  196.                         decodeStr. setText ( new Encode ( ). encodeStr (keyStr. getText ( ), encodeStr. getText ( ) ) );
  197.                 } else {
  198.                         decodeStr. setText ( "密匙必须是最多十位的数字!" );
  199.                 }
  200.         }
  201.        
  202.         /** 解密更新 */
  203.         private void updateEncodeStr ( ) {
  204.                 //      判定密匙是否合法
  205.                 if (chk (keyStr. getText ( ) ) ) {
  206.                         encodeStr. setText ( new Decode ( ). decodeStr (keyStr. getText ( ), decodeStr. getText ( ) ) );
  207.                 } else {
  208.                         encodeStr. setText ( "密匙必须是最多十位的数字!" );
  209.                 }
  210.         }
  211.        
  212.         private boolean chk ( String key ) {
  213.                 if (key. length ( )> 10 ) {
  214.                         return false;
  215.                 }
  216.                 try {
  217.                         Integer. parseInt (key );
  218.                         return true;
  219.                 } catch ( NumberFormatException e ) {
  220.                         return false;
  221.                 }
  222.         }
  223.  
  224.         /** 加密时改变密匙 */
  225.         private class KeyEncodeAction implements DocumentListener {
  226.                 public void changedUpdate ( DocumentEvent e ) {
  227.                         updateDecodeStr ( );
  228.                 }
  229.  
  230.                 public void insertUpdate ( DocumentEvent e ) {
  231.                         updateDecodeStr ( );
  232.                 }
  233.  
  234.                 public void removeUpdate ( DocumentEvent e ) {
  235.                         updateDecodeStr ( );
  236.                 }
  237.                
  238.         }
  239.        
  240.         /**     解密时改变密匙 */
  241.          private class KeyDecodeAction implements DocumentListener {
  242.                        
  243.                 public void changedUpdate ( DocumentEvent e ) {
  244.                         updateEncodeStr ( );
  245.                 }
  246.  
  247.                 public void insertUpdate ( DocumentEvent e ) {
  248.                         updateEncodeStr ( );
  249.                 }
  250.  
  251.                 public void removeUpdate ( DocumentEvent e ) {
  252.                         updateEncodeStr ( );
  253.                 }
  254.         }
  255.         
  256. }

 

Main.java

  1. import javax.swing.JFrame;
  2.  
  3. /** 加密解密小程序
  4. * @author Linyq.
  5. * @author YOYO.1989x.Net
  6. * @version 1.0
  7. */
  8. public class Main {
  9.  
  10.         /**
  11.          * main入口
  12.          * @param args
  13.          */
  14.         public static void main ( String [ ] args ) {
  15.                 JFrame. setDefaultLookAndFeelDecorated ( true );
  16.                 new GUIFrame ( );
  17.         }
  18.  
  19. }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值