美文网首页
Java 自加密解密字符串示例

Java 自加密解密字符串示例

作者: 优雅的时间 | 来源:发表于2020-07-12 11:28 被阅读0次
  1. D盘下创建jianshu文件夹
D:\jianshu
  1. D:\jianshu文件夹下创建code.txt文件
D:\jianshu
  - code.txt
  1. 打开D:\jianshu\code.txt文件
D:\jianshu
  - code.txt > write code to code.txt
  1. 拷贝加密内容到code.txt,提供示例加密内容如下
d0,cb,83,84,8a,80,80,80,80,80,9d,dc,8d,d1,a1,22,7f,36,99,80,80,80,99,80,80,80,88,80,80,80,
e3,ef,e4,e5,ae,f4,f8,f4,66,08,11,66,18,2f,65,0a,20,65,2f,06,66,16,07,64,3b,36,65,15,26,a0,
de,df,de,d0,cb,81,82,bf,80,8a,80,80,80,80,80,9d,dc,8d,d1,a1,22,7f,36,99,80,80,80,99,80,80,
80,88,80,a4,80,80,80,80,80,80,80,a0,80,80,80,80,80,80,80,e3,ef,e4,e5,ae,f4,f8,f4,8a,80,a0,
80,80,80,80,80,81,80,98,80,83,54,d7,ef,a2,f1,56,81,a1,ba,24,ef,a2,f1,56,81,ff,12,a7,ca,a2,
f1,56,81,d0,cb,85,86,80,80,80,80,81,80,81,80,da,80,80,80,bf,80,80,80,80,80,
  1. 用下面的解密程序解密该文件,提供解密代码如下
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

public class JianShuCode {

    public static void main(String[] s) {
        getFile("D:\\jianshu", "code.txt", true);
    }

    public static void getFile(String filepath, String filename, boolean ch) {
        try {

            File get = new File(filepath + File.separator + filename);
            if (get.isFile() && get.exists()) {
                FileInputStream fileInputStream = new FileInputStream(get);
                InputStreamReader read = new InputStreamReader(fileInputStream, "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(read);

                StringBuffer line = new StringBuffer();
                String lineTxt = "";

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    line.append(lineTxt);
                }
                read.close();

                if (Pattern.matches("^[0123456789abcdef, \\+\\-]{0,}$", line.toString())) {
                    if (ch) {
                        StringTokenizer st = new StringTokenizer(line.toString(), ",");
                        byte[] byteStr = new byte[st.countTokens()];
                        int i = 0;
                        while (st.hasMoreElements()) {
                            String stri = st.nextToken().trim();
                            byteStr[i] = (byte) (Integer.parseInt(stri, 16) - 128);
                            i++;
                        }
                        writeRealFile(byteStr, filepath, filename);
                    }

                    if (!ch) {
                        String str = line.toString();
                        int length = str.length();
                        byte[] byteStr = new byte[length / 2];
                        int i = 0;
                        while (i < length) {
                            String stri = str.substring(i, i + 2).trim();
                            byteStr[i / 2] = (byte) (Integer.parseInt(stri, 16) - 128);
                            i += 2;
                        }
                        writeRealFile(byteStr, filepath, filename);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Finished, Open File D:\\jianshu\\code.txt.zip");
    }

    private static void writeRealFile(byte[] byteStr, String filepath, String fileName) {
        isPathExist(filepath);

        FileOutputStream fos = null;
        BufferedOutputStream bos = null;

        String ff = filepath + File.separator + fileName + ".zip";
        try {
            fos = new FileOutputStream(ff);
            bos = new BufferedOutputStream(fos);
            bos.write(byteStr);

            bos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void isPathExist(String path) {
        File filePath = new File(path);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
    }
}
  1. 执行后会发现D:\jianshu下出现code.txt.zip文件,解密成功。
D:\jianshu
  - code.txt
  - code.txt.zip

相关文章

网友评论

      本文标题:Java 自加密解密字符串示例

      本文链接:https://www.haomeiwen.com/subject/ipsgcktx.html