what-the-bank/sources/org/bouncycastle/crypto/examples/DESExample.java

217 lines
8.3 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.crypto.examples;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.security.SecureRandom;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.KeyGenerationParameters;
import org.bouncycastle.crypto.engines.DESedeEngine;
import org.bouncycastle.crypto.generators.DESedeKeyGenerator;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;
/* loaded from: classes6.dex */
public class DESExample {
private PaddedBufferedBlockCipher cipher;
private boolean encrypt;
private BufferedInputStream in;
private byte[] key;
private BufferedOutputStream out;
private void process() {
this.cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()));
if (this.encrypt) {
performEncrypt(this.key);
} else {
performDecrypt(this.key);
}
try {
this.in.close();
this.out.flush();
this.out.close();
} catch (IOException unused) {
}
}
private void performEncrypt(byte[] bArr) {
this.cipher.init(true, new KeyParameter(bArr));
byte[] bArr2 = new byte[47];
byte[] bArr3 = new byte[this.cipher.getOutputSize(47)];
while (true) {
try {
int read = this.in.read(bArr2, 0, 47);
if (read <= 0) {
break;
}
int processBytes = this.cipher.processBytes(bArr2, 0, read, bArr3, 0);
if (processBytes > 0) {
byte[] encode = Hex.encode(bArr3, 0, processBytes);
this.out.write(encode, 0, encode.length);
this.out.write(10);
}
} catch (IOException | CryptoException unused) {
return;
}
}
int doFinal = this.cipher.doFinal(bArr3, 0);
if (doFinal > 0) {
byte[] encode2 = Hex.encode(bArr3, 0, doFinal);
this.out.write(encode2, 0, encode2.length);
this.out.write(10);
}
}
private void performDecrypt(byte[] bArr) {
this.cipher.init(false, new KeyParameter(bArr));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(this.in));
byte[] bArr2 = null;
while (true) {
try {
String readLine = bufferedReader.readLine();
if (readLine == null) {
break;
}
byte[] decode = Hex.decode(readLine);
bArr2 = new byte[this.cipher.getOutputSize(decode.length)];
int processBytes = this.cipher.processBytes(decode, 0, decode.length, bArr2, 0);
if (processBytes > 0) {
this.out.write(bArr2, 0, processBytes);
}
} catch (IOException | CryptoException unused) {
return;
}
}
int doFinal = this.cipher.doFinal(bArr2, 0);
if (doFinal > 0) {
this.out.write(bArr2, 0, doFinal);
}
}
public static void main(String[] strArr) {
String str;
boolean z = true;
if (strArr.length < 2) {
DESExample dESExample = new DESExample();
PrintStream printStream = System.err;
StringBuilder sb = new StringBuilder("Usage: java ");
sb.append(dESExample.getClass().getName());
sb.append(" infile outfile [keyfile]");
printStream.println(sb.toString());
System.exit(1);
}
String str2 = strArr[0];
String str3 = strArr[1];
if (strArr.length > 2) {
str = strArr[2];
z = false;
} else {
str = "deskey.dat";
}
new DESExample(str2, str3, str, z).process();
}
public DESExample(String str, String str2, String str3, boolean z) {
PrintStream printStream;
StringBuilder sb;
SecureRandom secureRandom;
SecureRandom secureRandom2 = null;
this.cipher = null;
this.in = null;
this.out = null;
this.key = null;
this.encrypt = z;
try {
this.in = new BufferedInputStream(new FileInputStream(str));
} catch (FileNotFoundException unused) {
PrintStream printStream2 = System.err;
StringBuilder sb2 = new StringBuilder("Input file not found [");
sb2.append(str);
sb2.append("]");
printStream2.println(sb2.toString());
System.exit(1);
}
try {
this.out = new BufferedOutputStream(new FileOutputStream(str2));
} catch (IOException unused2) {
PrintStream printStream3 = System.err;
StringBuilder sb3 = new StringBuilder("Output file not created [");
sb3.append(str2);
sb3.append("]");
printStream3.println(sb3.toString());
System.exit(1);
}
if (z) {
try {
try {
secureRandom = new SecureRandom();
} catch (Exception unused3) {
}
try {
secureRandom.setSeed("www.bouncycastle.org".getBytes());
} catch (Exception unused4) {
secureRandom2 = secureRandom;
System.err.println("Hmmm, no SHA1PRNG, you need the Sun implementation");
System.exit(1);
secureRandom = secureRandom2;
KeyGenerationParameters keyGenerationParameters = new KeyGenerationParameters(secureRandom, 192);
DESedeKeyGenerator dESedeKeyGenerator = new DESedeKeyGenerator();
dESedeKeyGenerator.init(keyGenerationParameters);
this.key = dESedeKeyGenerator.generateKey();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(str3));
byte[] encode = Hex.encode(this.key);
bufferedOutputStream.write(encode, 0, encode.length);
bufferedOutputStream.flush();
bufferedOutputStream.close();
return;
}
KeyGenerationParameters keyGenerationParameters2 = new KeyGenerationParameters(secureRandom, 192);
DESedeKeyGenerator dESedeKeyGenerator2 = new DESedeKeyGenerator();
dESedeKeyGenerator2.init(keyGenerationParameters2);
this.key = dESedeKeyGenerator2.generateKey();
BufferedOutputStream bufferedOutputStream2 = new BufferedOutputStream(new FileOutputStream(str3));
byte[] encode2 = Hex.encode(this.key);
bufferedOutputStream2.write(encode2, 0, encode2.length);
bufferedOutputStream2.flush();
bufferedOutputStream2.close();
return;
} catch (IOException unused5) {
printStream = System.err;
sb = new StringBuilder("Could not decryption create key file [");
}
} else {
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(str3));
int available = bufferedInputStream.available();
byte[] bArr = new byte[available];
bufferedInputStream.read(bArr, 0, available);
this.key = Hex.decode(bArr);
return;
} catch (IOException unused6) {
printStream = System.err;
sb = new StringBuilder("Decryption key file not found, or not valid [");
}
}
sb.append(str3);
sb.append("]");
printStream.println(sb.toString());
System.exit(1);
}
public DESExample() {
this.encrypt = true;
this.cipher = null;
this.in = null;
this.out = null;
this.key = null;
}
}