Skip to content

Instantly share code, notes, and snippets.

@potados99
Last active January 15, 2024 06:57
Show Gist options
  • Save potados99/4f81675bb804ad36a1c60b7cb5f237a4 to your computer and use it in GitHub Desktop.
Save potados99/4f81675bb804ad36a1c60b7cb5f237a4 to your computer and use it in GitHub Desktop.
붕어빵틀
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.stream.Stream;
class XorPerformer {
public byte getKey(String password, Charset charset) {
int acc = 0;
byte[] bytes = password.getBytes(charset);
for (byte aByte : bytes) {
acc += aByte & 255;
}
byte key = (byte) (acc & 255);
if (key == 0) {
key = 1;
}
return key;
}
public byte[] encrypt(byte[] var1, byte var2) {
return xor(var1, var2);
}
public byte[] decrypt(byte[] var1, byte var2) {
return xor(var1, var2);
}
public byte[] xor(byte[] in, byte key) {
int len;
byte[] out = new byte[len = in.length];
for (int i = 0; i < len; ++i) {
out[i] = (byte) (in[i] ^ key);
}
return out;
}
}
class TripUtil {
public static void encrypt(String srcPath, String destPath, String tripFilename) {
doIt(srcPath, destPath, tripFilename);
}
public static void decrypt(String srcPath, String destPath, String tripFilename) {
doIt(srcPath, destPath, tripFilename);
}
private static void doIt(String srcPath, String destPath, String tripFilename) {
byte[] source = readSource(srcPath);
XorPerformer x = new XorPerformer();
byte key = x.getKey(tripFilename, Charset.forName("EUC-KR"));
byte[] decrypted = x.xor(source, key);
write(destPath, decrypted);
}
/**
* 주어진 경로가 단일 텍스트 파일이든 청크든,
* 일단 읽어냅니다.
*
* @param srcPath
* @return
*/
private static byte[] readSource(String srcPath) {
if (isChunk(srcPath)) {
File chunkDir = new File(srcPath);
File[] chunks = chunkDir.listFiles();
assert chunks != null;
ByteArrayOutputStream s = new ByteArrayOutputStream();
Stream
.of(chunks)
.sorted(Comparator.comparing(File::getName))
.map(TripUtil::read)
.forEach(bytes -> write(bytes, s));
return s.toByteArray();
} else {
return read(Path.of(srcPath));
}
}
private static byte[] read(File file) {
return read(file.toPath());
}
private static byte[] read(Path path) {
try {
return Files.readAllBytes(path);
} catch (IOException e) {
return new byte[]{};
}
}
private static void write(byte[] bytes, ByteArrayOutputStream stream) {
try {
stream.write(bytes);
} catch (IOException e) {
return;
}
}
private static void write(String destPath, byte[] bytes) {
try {
new File(destPath).getParentFile().mkdirs();
Files.write(Path.of(destPath), bytes);
} catch (IOException e) {
return;
}
}
private static boolean isChunk(String path) {
return !path.endsWith(".TXT");
}
}
class Main {
public static void main(String[] args) {
TripUtil.encrypt(
"C:\\Users\\Administrator\\AppData\\Roaming\\Google\\AndroidStudio2023.1\\scratches\\20240101-123사4567-X-10-1234678.TXT", // 요 경로의 파일을 암호화해서
"C:\\Users\\Administrator\\AppData\\Roaming\\Google\\AndroidStudio2023.1\\scratches\\20240101-123사4567-X-10-1234678.enc.TXT", // 이 파일에 집어넣는데
"20240101-123사4567-X-10-1234678.TXT" // 암호화 키는 이걸로 구합니다.
);
TripUtil.decrypt(
"C:\\Users\\Administrator\\AppData\\Roaming\\Google\\AndroidStudio2023.1\\scratches\\20240101-123사4567-X-10-1234678.enc.TXT", // 요 경로의 파일을 복호화해서
"C:\\Users\\Administrator\\AppData\\Roaming\\Google\\AndroidStudio2023.1\\scratches\\20240101-123사4567-X-10-1234678.dec.TXT", // 이 파일에 집어넣는데
"20240101-123사4567-X-10-1234678.TXT" // 복호화 키는 이걸로 구합니다.
);
TripUtil.decrypt(
"C:\\Users\\Administrator\\source\\repos\\driving-data-storage\\20240103-플래77시7777-X-08-1234567", // 요 경로의 청크를 복호화해서
"C:\\Users\\Administrator\\AppData\\Roaming\\Google\\AndroidStudio2023.1\\scratches\\hmm.txt", // 이 파일에 집어넣는데
"20240103-플래77시7777-X-08-1234567.TXT" // 복호화 키는 이걸로 구합니다.
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment