· 5 years ago · Dec 01, 2019, 06:54 PM
1package cherry.cherry;
2
3import java.io.BufferedWriter;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.OutputStream;
10import java.io.OutputStreamWriter;
11import java.io.RandomAccessFile;
12import java.security.Key;
13import java.security.KeyFactory;
14import java.security.spec.PKCS8EncodedKeySpec;
15import java.security.spec.X509EncodedKeySpec;
16import javax.crypto.Cipher;
17import javax.crypto.CipherInputStream;
18import javax.crypto.CipherOutputStream;
19import javax.crypto.KeyGenerator;
20import javax.crypto.SecretKey;
21import javax.crypto.spec.SecretKeySpec;
22
23public class Twig {
24 private static int a = 4096;
25 private static int b = 128;
26 public static byte[] c;
27 public static byte[] d;
28
29 private static Key a(String str) {
30 byte[] bArr;
31 try {
32 if (d == null) {
33 RandomAccessFile randomAccessFile = new RandomAccessFile(str, "r");
34 bArr = new byte[((int) randomAccessFile.length())];
35 randomAccessFile.read(bArr);
36 randomAccessFile.close();
37 } else {
38 bArr = d;
39 }
40 return KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(bArr));
41 } catch (Exception e) {
42 throw new RuntimeException(e);
43 }
44 }
45
46 private static void a(InputStream inputStream, OutputStream outputStream) {
47 byte[] bArr = new byte[1048576];
48 while (true) {
49 try {
50 int read = inputStream.read(bArr);
51 if (read != -1) {
52 if (read == 0) {
53 int read2 = inputStream.read();
54 if (read2 >= 0) {
55 outputStream.write(read2);
56 } else {
57 return;
58 }
59 } else {
60 outputStream.write(bArr, 0, read);
61 }
62 outputStream.flush();
63 } else {
64 return;
65 }
66 } catch (IOException e) {
67 e.printStackTrace();
68 return;
69 }
70 }
71 }
72
73 public static void a(String str, String str2, String str3) {
74 String str4 = "AES";
75 try {
76 Key a2 = a(str3);
77 Cipher instance = Cipher.getInstance("RSA/ECB/PKCS1Padding");
78 instance.init(2, a2);
79 FileInputStream fileInputStream = new FileInputStream(str);
80 byte[] bArr = new byte[(a / 8)];
81 fileInputStream.read(bArr);
82 SecretKeySpec secretKeySpec = new SecretKeySpec(instance.doFinal(bArr), str4);
83 Cipher instance2 = Cipher.getInstance(str4);
84 instance2.init(2, secretKeySpec);
85 CipherInputStream cipherInputStream = new CipherInputStream(fileInputStream, instance2);
86 FileOutputStream fileOutputStream = new FileOutputStream(str2);
87 a((InputStream) cipherInputStream, (OutputStream) fileOutputStream);
88 fileOutputStream.close();
89 cipherInputStream.close();
90 } catch (Exception e) {
91 throw new RuntimeException(e);
92 }
93 }
94
95 private static void a(String str, Key key) {
96 try {
97 FileOutputStream fileOutputStream = new FileOutputStream(str);
98 try {
99 fileOutputStream.write(key.getEncoded());
100 fileOutputStream.close();
101 } catch (IOException e) {
102 e.printStackTrace();
103 }
104 } catch (FileNotFoundException e2) {
105 throw new RuntimeException(e2);
106 }
107 }
108
109 private static Key b(String str) {
110 byte[] bArr;
111 try {
112 if (c == null) {
113 RandomAccessFile randomAccessFile = new RandomAccessFile(str, "r");
114 bArr = new byte[((int) randomAccessFile.length())];
115 randomAccessFile.read(bArr);
116 randomAccessFile.close();
117 } else {
118 bArr = c;
119 }
120 return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bArr));
121 } catch (Exception e) {
122 throw new RuntimeException(e);
123 }
124 }
125
126 public static void b(String str, String str2, String str3) {
127 String str4 = "AES";
128 try {
129 Key b2 = b(str3);
130 KeyGenerator instance = KeyGenerator.getInstance(str4);
131 instance.init(b);
132 SecretKey generateKey = instance.generateKey();
133 Cipher instance2 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
134 instance2.init(1, b2);
135 FileOutputStream fileOutputStream = new FileOutputStream(str2);
136 fileOutputStream.write(instance2.doFinal(generateKey.getEncoded()));
137 Cipher instance3 = Cipher.getInstance(str4);
138 instance3.init(1, generateKey);
139 CipherOutputStream cipherOutputStream = new CipherOutputStream(fileOutputStream, instance3);
140 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(cipherOutputStream));
141 FileInputStream fileInputStream = new FileInputStream(str);
142 a((InputStream) fileInputStream, (OutputStream) cipherOutputStream);
143 fileInputStream.close();
144 bufferedWriter.close();
145 } catch (Exception e) {
146 throw new RuntimeException(e);
147 }
148 }
149}