赞
踩
代码中有标注每一步,见最下面源代码
将所有内容加密 转存在一个文件中
将第一步的文件进行切分,分成多个小文件
将多个小文件转为 postman需要的json文件
操作postman进行保存,如下图:
待另外一个电脑登录的同一账号下的postman,将信息同步完毕后,在另外一台电脑继续执行下面操作
将导出的 文件解密 并自动生成 完成的内容
package com.wwy.mystuflink.encryption; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import lombok.Data; import org.apache.commons.collections.MapUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.IOFileFilter; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.security.Key; import java.security.SecureRandom; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; public class Test { private static byte[] salt = "qpwoeiru".getBytes(); private static final String password = "password"; private static final String file_prefix = "this_file_path_is:"; private static final String file_split = "------------------------------------------------------------------------------------------------------------"; private static final String encoded = "UTF-8"; //以下为 源服务器 资源信息 private static final String source_path = "/Users/xiaoshuai/ideaProject1/study/dubbo_study"; private static final String target_big_file_path = "/Users/xiaoshuai/ideaProject/1103/main.txt"; private static final String target_directory_path = "/Users/xiaoshuai/ideaProject/1103/smart/"; private static final String target_directory_smart_file_path = target_directory_path + "%s.txt"; private static final List<String> source_filter_path = Lists.newArrayList( "/.git", "/.idea", "/.gitignore", "/.DS_Store", "/target/", "/.mvn/" ); private static final String post_man_directory_path = "/Users/xiaoshuai/ideaProject/1103/postman/"; private static final String post_man_json_path = post_man_directory_path + "%s.txt"; //以下为 目标服务器 资源信息 private static final String my_post_man_directory_path = "/Users/xiaoshuai/ideaProject/1103/postman/"; private static final String my_target_big_file_path = "/Users/xiaoshuai/ideaProject/1103/main1.txt"; private static final String my_target_big_decrypt_file_path = "/Users/xiaoshuai/ideaProject/1103/main2.txt"; //公共过滤非必要条件集合 private static final Map<String, String> rootPathFix = new HashMap<String, String>(){{ this.put("/Users/xiaoshuai/ideaProject1/study","/Users/xiaoshuai/ideaProject2/"); }}; /** * 测试加密 * * @param args * @throws Exception */ public static void testEncryption(String[] args) throws Exception { RandomAccessFile file = new RandomAccessFile("/Users/xiaoshuai/ideaProject/amap-abs-generator/pom.xml"); FileWriter fileWriter = new FileWriter(new File("/Users/xiaoshuai/ideaProject/test/111.txt"), true); String context = ""; fileWriter.append(PBEUtil.byteToString(PBEUtil.encrypt(context.getBytes(), password, salt)) + "\n"); while (null != (context = file.readLine())) { fileWriter.append(PBEUtil.byteToString(PBEUtil.encrypt(context.getBytes(), password, salt)) + "\n"); } fileWriter.flush(); } /** * 测试解密 * * @param args * @throws Exception */ public static void testDecrypt(String[] args) throws Exception { RandomAccessFile file = new RandomAccessFile("/Users/xiaoshuai/ideaProject/1103/main.txt"); FileWriter fileWriter = new FileWriter(new File("/Users/xiaoshuai/ideaProject/1103/main2.txt")); String context; while (null != (context = file.readLine())) { try { fileWriter.append(new String(PBEUtil.decrypt(PBEUtil.stringToByte(context), password, salt)) + "\n"); } catch (Exception e) { e.printStackTrace(); System.out.println(context); } } fileWriter.flush(); } /** * 复制文本信息 * * @param readPath * @param writePath * @throws Exception */ public static void exportMessage(String readPath, String writePath) throws Exception { Boolean jiami = true; RandomAccessFile file = new RandomAccessFile(readPath); FileWriter fileWriter = new FileWriter(new File(writePath), true); String context = file_split; if (jiami) { fileWriter.append(PBEUtil.byteToString(PBEUtil.encrypt(context.getBytes(), password, salt)) + "\n"); } else { fileWriter.append(context + "\n"); } context = file_prefix + readPath; if (jiami) { fileWriter.append(PBEUtil.byteToString(PBEUtil.encrypt(context.getBytes(), password, salt)) + "\n"); } else { fileWriter.append(context + "\n"); } while (null != (context = file.readLine())) { if (jiami) { fileWriter.append(PBEUtil.byteToString(PBEUtil.encrypt(context.getBytes(), password, salt)) + "\n"); } else { fileWriter.append(context + "\n"); } } fileWriter.flush(); } //第一步 导出到大文件 public static void main1(String[] args) { File write = new File(target_big_file_path); File listFile = new File(source_path); IOFileFilter ioFileFilter = new IOFileFilter() { @Override public boolean accept(File file) { return !source_filter_path.stream().anyMatch(e -> file.getAbsolutePath().contains(e)); } @Override public boolean accept(File dir, String name) { return true; } }; Collection<File> files = FileUtils.listFiles(listFile, ioFileFilter, ioFileFilter); files.stream().forEach(e -> { try { exportMessage(e.getAbsolutePath(), write.getAbsolutePath()); } catch (Exception ex) { ex.printStackTrace(); } }); } //第二步 大文件切分 public static void main2(String[] args) throws IOException { int i = 0; RandomAccessFile file = new RandomAccessFile(target_big_file_path); String context; File write = null; FileWriter fileWriter = null; while (null != (context = file.readLine())) { if (null == fileWriter || i >= 1000 || (null != write && (write.length() / 1024) > 500)) { i = 0; FileUtils.touch(write = new File(String.format(target_directory_smart_file_path, System.currentTimeMillis()))); fileWriter = new FileWriter(write); } i++; fileWriter.append(context + "\n"); fileWriter.flush(); } } //第三步 public static void main3(String[] args) throws IOException { File listFile = new File(target_directory_path); IOFileFilter ioFileFilter = new IOFileFilter() { @Override public boolean accept(File file) { return !source_filter_path.stream().anyMatch(e -> file.getAbsolutePath().contains(e)); } @Override public boolean accept(File dir, String name) { return true; } }; AtomicInteger atomicInteger = new AtomicInteger(1); List<Item> list = Lists.newArrayList(); Collection<File> files = FileUtils.listFiles(listFile, ioFileFilter, ioFileFilter); files.stream().sorted(Comparator.comparing(File::getName)).forEach(e -> { try { String name = "test" + atomicInteger.getAndAdd(1); Item item = new Item(); item.name = name; item.request.body.raw = FileUtils.readFileToString(e, encoded); item.request.url.raw = String.format(item.request.url.raw, name); item.request.url.path[1] = name; list.add(item); } catch (Exception ex) { ex.printStackTrace(); } }); Integer childName = 111; File jsonfile = new File(String.format(post_man_json_path, childName)); if (!jsonfile.exists()) { FileUtils.touch(jsonfile); } FileWriter fileWriter = new FileWriter(jsonfile); List<Item> strArray = Lists.newArrayList(); Result result = new Result(); for (int i = 0; i < list.size(); i++) { Item item = list.get(i); strArray.add(item); if (strArray.size() > 10) { result.item = strArray; fileWriter.write(JSONObject.toJSONString(result)); fileWriter.flush(); strArray.clear(); File file = new File(String.format(post_man_json_path, ++childName)); FileUtils.touch(file); fileWriter = new FileWriter(file); } } result.item = strArray; fileWriter.write(JSONObject.toJSONString(result)); fileWriter.flush(); } //第四步 public static void main4(String[] args) throws IOException { File write = new File(my_target_big_file_path); if (write.exists()) { write.delete(); } FileUtils.touch(write); FileWriter fileWriter = new FileWriter(write); File listFile = new File(my_post_man_directory_path); IOFileFilter ioFileFilter = new IOFileFilter() { @Override public boolean accept(File file) { return !source_filter_path.stream().anyMatch(e -> file.getAbsolutePath().contains(e)); } @Override public boolean accept(File dir, String name) { return true; } }; Collection<File> files = FileUtils.listFiles(listFile, ioFileFilter, ioFileFilter); files.stream().forEach(e -> { try { String context = FileUtils.readFileToString(e, encoded); Result result = JSONObject.parseObject(context, Result.class); result.item.stream().forEach(item -> { try { fileWriter.append(item.request.body.raw); } catch (IOException ex) { ex.printStackTrace(); } }); } catch (Exception ex) { ex.printStackTrace(); } }); fileWriter.flush(); RandomAccessFile file = new RandomAccessFile(my_target_big_file_path); FileWriter decryptFileWriter = new FileWriter(new File(my_target_big_decrypt_file_path)); String context; while (null != (context = file.readLine())) { try { decryptFileWriter.append(new String(PBEUtil.decrypt(PBEUtil.stringToByte(context), password, salt)) + "\n"); } catch (Exception e) { e.printStackTrace(); System.out.println(context); } } decryptFileWriter.flush(); File tempFile = null; FileWriter tempFileWriter = null; RandomAccessFile decryptFile = new RandomAccessFile(my_target_big_decrypt_file_path); while (null != (context = decryptFile.readLine())) { try { if (context.equals(file_split) && null != (context = decryptFile.readLine()) && context.startsWith(file_prefix)) { AtomicReference<StringBuffer> filePath = new AtomicReference<>(new StringBuffer(context.replace(file_prefix, ""))); if (MapUtils.isNotEmpty(rootPathFix)) { rootPathFix.keySet().stream().forEach(key -> { filePath.set(new StringBuffer(filePath.toString().replace(key, rootPathFix.get(key)))); }); } if (null == filePath.get()) { continue; } if (null != tempFileWriter) { tempFileWriter.flush(); } tempFile = new File(filePath.get().toString()); FileUtils.touch(tempFile); tempFileWriter = new FileWriter(tempFile); context = decryptFile.readLine(); } tempFileWriter.append(context + "\n"); } catch (Exception e) { e.printStackTrace(); System.out.println(context); } } } @Data static class Result { Info info = new Info(); List<Item> item; } @Data static class Info { String _postman_id = "de75f883-659b-4c3b-bccf-6a2aa7d1eb1e"; String name = "myTest"; String schema = "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"; } @Data static class Item { String name = ""; Request request = new Request(); String[] response = new String[0]; } @Data static class Request { String method = "POST"; String[] header = new String[0]; Body body = new Body(); Url url = new Url(); } @Data static class Body { String mode = "raw"; String raw = ""; } @Data static class Url { String raw = "localhost:8080/domain/%s.txt"; String[] host = new String[]{"localhost"}; String port = "8080"; String[] path = new String[]{"domain", ""}; } static class PBEUtil { public static final String ALGORITHM = "PBEWITHMD5andDES"; public static final int ITERATION_COUNT = 100; public static byte[] initSalt() throws Exception { //实例化安全随机数 SecureRandom random = new SecureRandom(); return random.generateSeed(8); } /*** * 转换密钥 * @param password 密码 * @return 密钥 * @throws Exception */ private static Key toKey(String password) throws Exception { //密钥材料 PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray()); //实例化 SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM); //生成密钥 return factory.generateSecret(keySpec); } /*** * 加密 * @param data 待加密数据 * @param password 密钥 * @param salt * @return * @throws Exception */ public static byte[] encrypt(byte[] data, String password, byte[] salt) throws Exception { //转换密钥 Key key = toKey(password); //实例化PBE参数材料 PBEParameterSpec spec = new PBEParameterSpec(salt, ITERATION_COUNT); //实例化 Cipher cipher = Cipher.getInstance(ALGORITHM); //初始化 cipher.init(Cipher.ENCRYPT_MODE, key, spec); return cipher.doFinal(data); } /*** * 解密 * @param data 待解密数据 * @param password 密钥 * @param salt * @return * @throws Exception */ public static byte[] decrypt(byte[] data, String password, byte[] salt) throws Exception { //转换密钥 Key key = toKey(password); //实例化PBE参数材料 PBEParameterSpec spec = new PBEParameterSpec(salt, ITERATION_COUNT); //实例化 Cipher cipher = Cipher.getInstance(ALGORITHM); //初始化 cipher.init(Cipher.DECRYPT_MODE, key, spec); //执行操作 return cipher.doFinal(data); } public static String byteToString(byte[] data) { if (null == data) { return null; } StringBuilder sb = new StringBuilder(); for (byte b : data) { sb.append(b).append(","); } sb.deleteCharAt(sb.length() - 1); return sb.toString(); } public static byte[] stringToByte(String data) { if (null == data) { return null; } String[] array = data.split(","); byte[] bytes = new byte[array.length]; for (int i = 0; i < array.length; i++) { bytes[i] = Byte.parseByte(array[i]); } return bytes; } public static void main(String[] args) throws Exception { byte[] salt = "qpwoeiru".getBytes(); System.out.println("salt:" + byteToString(salt)); String password = "password"; System.out.println("口令:" + password); String data = " * @version v1.0"; System.out.println("加密前数据:String:" + data); System.out.println("加密前数据:byte[]:" + byteToString(data.getBytes())); byte[] encryptData = encrypt(data.getBytes(), password, salt); String byteToString = byteToString(encryptData); System.out.println("加密后数据:byte[]:" + byteToString); byte[] decryptData = decrypt(stringToByte(byteToString), password, salt); System.out.println("解密后数据: byte[]:" + byteToString(decryptData)); System.out.println("解密后数据: string:" + new String(decryptData)); } } private static class RandomAccessFile { private ArrayBlockingQueue<String> queue; public RandomAccessFile(String filePath) { try { String[] context = FileUtils.readFileToString(new File(filePath), encoded).split("\n"); queue = new ArrayBlockingQueue(context.length); queue.addAll(Arrays.asList(context)); } catch (IOException e) { e.printStackTrace(); } } public String readLine() { try { if (queue.size() > 0) { return queue.take(); } else { return null; } } catch (InterruptedException e) { e.printStackTrace(); } return ""; } } }
涉及的jar包依照pom文件引入
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wwy</groupId> <artifactId>mystuflink</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mystuflink</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency>--> <!--<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-java</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-streaming-java_2.11</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>21.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> <version>4.12</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。