欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Shell加密解密文件,,Shell加密解密文件A

来源: javaer 分享于  点击 21040 次 点评:248

Shell加密解密文件,,Shell加密解密文件A


Shell加密解密文件

A.java

import java.io.*;import javax.swing.*;public class A {    private static final String CharSet = "0123456789ABCDEF";    public static void main(String[] args) {        JFileChooser jfc = new JFileChooser();        JFileChooser jfc2 = new JFileChooser();        jfc.showDialog(null, "请选择要加密编码的文件");        jfc2.showDialog(null, "请选择要输出的文件名");        if (jfc.getSelectedFile() != null && jfc2.getSelectedFile() != null) {            File oldfile = jfc.getSelectedFile();            FileInputStream inStream = null;            FileWriter fw = null;            try {                if (oldfile.exists()) {                    int byteread = 0;                    inStream = new FileInputStream(oldfile);                    fw = new FileWriter(jfc2.getSelectedFile());                    byte[] sRead = new byte[10240];                    while ((byteread = inStream.read(sRead)) != -1) {                        StringBuilder smi = new StringBuilder(byteread * 2);                        int ka = 3, kb = 5, kc = 2, kd = 7, js = 0;                        if (byteread % 2 != 0)                            js = 1;                        for (int i = 0; i < byteread - 1; i += 2) {                            char c1 = (char) sRead[i];                            char c2 = (char) sRead[i + 1];                            int tmp = ka * c1 + kc * c2;                            while (tmp < 0)                                tmp += 1024;                            byte s1 = (byte) (tmp % 1024);                            int js1 = (int) s1 >> 4 & 0xf;                            smi.append(CharSet.substring(js1, js1 + 1));                            int ks1 = s1 & 0xf;                            smi.append(CharSet.substring(ks1, ks1 + 1));                            tmp = kb * c1 + kd * c2;                            while (tmp < 0)                                tmp += 1024;                            byte s2 = (byte) (tmp % 1024);                            int js2 = (int) s2 >> 4 & 0xf;                            smi.append(CharSet.substring(js2, js2 + 1));                            int ks2 = s2 & 0xf;                            smi.append(CharSet.substring(ks2, ks2 + 1));                        }                        if (js == 1) {                            byte s3 = (byte) ((sRead[byteread - 1] - 4) % 1024);                            int js3 = (int) s3 >> 4 & 0xf;                            smi.append(CharSet.substring(js3, js3 + 1));                            int ks3 = (int) s3 & 0xf;                            smi.append(CharSet.substring(ks3, ks3 + 1));                        }                        fw.write(smi.toString());                    }                    fw.flush();                }            } catch (IOException e) {                e.printStackTrace();            } finally {                try {                    fw.close();                    inStream.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

B.java

import java.io.*;import javax.swing.*;public class B {    private static final String CharSet = "0123456789ABCDEF";    private static int niyuan(int m, int n) {        int a, b, c, d, t, yu = 0, shang, mod;        a = m;        b = n;        mod = a;        c = 0;        d = 1;        while (b < 0)            b += a;        if (a % b == 0 || b % 2 == 0)            return 0;        while (b != 1) {            t = a % b;            shang = a / b;            a = b;            b = t;            yu = c - shang * d;            c = d;            d = yu;        }        if (yu < 0)            yu += mod;        return yu;    }    public static void main(String[] args) {        // TODO Auto-generated method stub        JFileChooser jfc = new JFileChooser();        JFileChooser jfc2 = new JFileChooser();        jfc.showDialog(null, "请选择要解码解密的文件");        jfc2.showDialog(null, "请选择要输出的文件名");        if (jfc.getSelectedFile() != null && jfc2.getSelectedFile() != null) {            FileWriter fw = null;            try {                FileInputStream fis = new FileInputStream(jfc.getSelectedFile());                fw = new FileWriter(jfc2.getSelectedFile());                int byteread = 0;                byte[] buffer = new byte[20480];                int ka = 3, kb = 5, kc = 2, kd = 7, js = 0, tmp;                int aany, ddny;                int r00 = ka * kc * kd;                int r01 = -ka * kb * kc;                int r10 = -kb * kc * kc;                int r11 = ka * kb * kc;                int x00 = ka * ka * kc * kd - ka * kb * kc * kc;                int x11 = ka * kb * kc * kd - kb * kb * kc * kc;                while (x00 % 2 == 0) {                    x00 /= 2;                    r00 /= 2;                    r01 /= 2;                }                while (x11 % 2 == 0) {                    x11 /= 2;                    r10 /= 2;                    r11 /= 2;                }                aany = x00;                ddny = x11;                if (niyuan(1024, aany) != 0 && niyuan(1024, ddny) != 0) {                    int kn00 = r00 * niyuan(1024, x00);                    int kn01 = r01 * niyuan(1024, x00);                    int kn10 = r10 * niyuan(1024, x11);                    int kn11 = r11 * niyuan(1024, x11);                    ka = kn00;                    kb = kn01;                    kc = kn10;                    kd = kn11;                } else {                    JOptionPane.showMessageDialog(null, "无逆矩阵!");                    System.exit(0);                }                while (ka < 0)                    ka += 1024;                while (kb < 0)                    kb += 1024;                while (kc < 0)                    kc += 1024;                while (kd < 0)                    kd += 1024;                ka %= 1024;                kb %= 1024;                kc %= 1024;                kd %= 1024;                try {                    while ((byteread = fis.read(buffer)) != -1) {                        int nLen = byteread / 2;                        StringBuilder smi = new StringBuilder(nLen);                        byte[] sming = new byte[nLen];                        String chs=new String(buffer,"US-ASCII");                        for (int i = 0; i < nLen; i++) {                            byte bTmp;                            if (byteread < 2)                                bTmp = -1;                            bTmp = (byte) (CharSet.indexOf(chs.substring(i * 2,i * 2+1)) * 16 + CharSet                                    .indexOf(chs.substring(i * 2 + 1,i * 2 + 2)));                            sming[i] = bTmp;                        }                        if (nLen % 2 != 0)                            js = 1;                        for (int i = 0; i < nLen - 1; i += 2) {                            char c1 = (char) sming[i];                            char c2 = (char) sming[i + 1];                            tmp = ka * c1 + kc * c2;                            while (tmp < 0)                                tmp += 1024;                            char s1 = (char) (tmp % 1024);                            smi.append(s1);                            tmp = kb * c1 + kd * c2;                            while (tmp < 0)                                tmp += 1024;                            char s2 = (char) (tmp % 1024);                            smi.append(s2);                        }                        if (js == 1) {                            char c3 = (char) ((sming[nLen - 1] - 4) % 1024);                            smi.append(c3);                        }                        fw.write(smi.toString(), 0, nLen);                    }                    fw.flush();                } catch (IOException e) {                    e.printStackTrace();                } finally {                    try {                        fis.close();                        fw.close();                    } catch (IOException e) {                        e.printStackTrace();                    }                }            } catch (FileNotFoundException e1) {                // TODO Auto-generated catch block                e1.printStackTrace();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        System.exit(0);    }}
相关栏目:

用户点评