Java 入門指南 - Encrypt.java




Encrypt.java 的程式原始碼如下

package encryptor; // 套件

public class Encrypt {
    // 屬性區
    private char[] cArray = new char[26];
    private int a;
    private int b;
    private int n;    
    private char c;  
    private int diff; 
    
    // 建構子區
    public Encrypt(int a, int b, int n, char c, int dif) {
        setA(a);
        setB(b);
        setN(n);
        setC(c);
        setDif(dif);
        setArray();
    }
    
    public Encrypt(int a, int b) {
        this(a, b, 26, 'a', 97);
    }
    
    public Encrypt() {
        int ta = 0;
        int tb = 0;
        
        while (ta % 2 == 0) {
            ta = (int) (Math.random() * 10);
            tb = (int) (Math.random() * 10);
        }
        
        setA(ta);
        setB(tb);
        setN(26);
        setC('a');
        setDif(97);
        setArray();
    }
    
    // setter 與 getter 區
    public void setA(int arg) {
        a = arg;
    }
    
    public int getA() {
        return a;
    }
    
    public void setB(int arg) {
        b = arg;
    }
    
    public int getB() {
        return b;
    }
    
    public void setN(int arg) {
        n = arg;
    }
    
    public int getN() {
        return n;
    }
    
    public void setC(char arg) {
        c = arg;
    }
    
    public char getC() {
        return c;
    }
    
    public void setDif(int arg) {
        diff = arg;
    }
    
    public int getDif() {
        return diff;
    }

    public void setArray() {
        int i, d, ci, m;
        char r;
        for (i = 0; i < n; i++) {
            ci = getC();
            d = ci * getA() + getB();
            m = d % getN();
            r = (char) (m + getDif());
            cArray[i] = r;
            c++;
        }        
    }
    
    public char[] getArray() {
        return cArray;
    }
    
    // 進行編碼的方法
    public String toEncode(String s) {
        char[] cs = s.toCharArray();
        int i, ci, d, m;
        char r;
        String rs = "";
        Character cc;
        
        for (i = 0; i < cs.length; i++) {
            if (cs[i] >= 97 && cs[i] <= 122) {
                ci = cs[i];
                m = ci - 97;
                cs[i] = getArray()[m];
            }
        }
        
        for (i = 0; i < cs.length; i++) {
            cc = new Character(cs[i]);
            rs = rs.concat(cc.toString());
        }
        
        return rs;
    }
    
    // 進行解碼的方法
    public String toDecode(String s) {
        char[] cs = s.toCharArray();
        int i, j;
        char r;
        String rs = "";
        Character cc;
        
        for (i = 0; i < cs.length; i++) {
            if (cs[i] >= 97 && cs[i] <= 122) {
                for (j = 0; j <= getArray().length; j++) {
                    if (cs[i] == getArray()[j]) {
                        cs[i] = (char) (j + getDif());
                        break;
                    }
                }
            }
        }

        for (i = 0; i < cs.length; i++) {
            cc = new Character(cs[i]);
            rs = rs.concat(cc.toString());
        }
        
        return rs;        
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:Encrypt.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2011 年 4 月 */


您可以繼續參考
範例程式碼


相關目錄
回 Java 入門指南
回 Java 教材目錄
回首頁


參考資料
The JavaTM Tutorials: Getting Started
The JavaTM Tutorials: Learning the Java Language
The JavaTM Tutorials: Essential Classes
The Java Language Specification, Third Edition


本文於 2013 年 1 月訂正

2 則留言:

Unknown 提到...

第88行的ci = getC();
應該是在for迴圈外面唷~

Kaiching Chang 提到...

有空的話,動動手指打打鍵盤編譯一下吧
^_^