mirror of
https://github.com/Steffo99/unimore-oop-2020-cleaver.git
synced 2024-11-21 23:54:20 +00:00
facepalm
This commit is contained in:
parent
44cd6b5134
commit
823736aecc
16 changed files with 190 additions and 39 deletions
|
@ -6,6 +6,7 @@
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
|
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/docs" />
|
<excludeFolder url="file://$MODULE_DIR$/docs" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/examples" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?><Cleaver><Original>loremipsum.txt</Original><Split part-size="1024" parts="31" total-size="31"/><Compress/></Cleaver>
|
|
|
@ -109,7 +109,7 @@ public class CleaverFrame extends JFrame {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
for(Job job : jobs) {
|
for(Job job : jobs) {
|
||||||
if(job.getProgress() instanceof NotStartedProgress)
|
if(job.getState() == Thread.State.NEW)
|
||||||
{
|
{
|
||||||
job.start();
|
job.start();
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,7 +98,7 @@ public class ChopJob extends Job {
|
||||||
outputStream = new SplitFileOutputStream(file.getAbsolutePath(), splitConfig.getPartSize());
|
outputStream = new SplitFileOutputStream(file.getAbsolutePath(), splitConfig.getPartSize());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
outputStream = new FileOutputStream(String.format("%s.c00", file.getAbsolutePath()));
|
outputStream = new FileOutputStream(String.format("%s.c0", file.getAbsolutePath()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(compressConfig != null) {
|
if(compressConfig != null) {
|
||||||
|
@ -160,6 +160,7 @@ public class ChopJob extends Job {
|
||||||
|
|
||||||
this.setProgress(new FinishedProgress());
|
this.setProgress(new FinishedProgress());
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
this.setProgress(new ErrorProgress(e));
|
this.setProgress(new ErrorProgress(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -193,7 +193,7 @@ public class StitchJob extends Job {
|
||||||
inputStream = new SplitFileInputStream(resultFile.getPath(), splitConfig.getPartSize());
|
inputStream = new SplitFileInputStream(resultFile.getPath(), splitConfig.getPartSize());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
inputStream = new FileInputStream(String.format("%s.c00", resultFile.getAbsolutePath()));
|
inputStream = new FileInputStream(String.format("%s.c0", resultFile.getAbsolutePath()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (compressConfig != null) {
|
if (compressConfig != null) {
|
||||||
|
@ -201,7 +201,7 @@ public class StitchJob extends Job {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cryptConfig != null) {
|
if (cryptConfig != null) {
|
||||||
inputStream = new CryptInputStream(inputStream);
|
inputStream = new CryptInputStream(inputStream, cryptConfig.getKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
//Pipe everything to the output
|
//Pipe everything to the output
|
||||||
|
@ -220,6 +220,7 @@ public class StitchJob extends Job {
|
||||||
|
|
||||||
this.setProgress(new FinishedProgress());
|
this.setProgress(new FinishedProgress());
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
this.setProgress(new ErrorProgress(e));
|
this.setProgress(new ErrorProgress(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,112 @@
|
||||||
package eu.steffo.cleaver.logic.crypt;
|
package eu.steffo.cleaver.logic.crypt;
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import javax.crypto.SecretKeyFactory;
|
||||||
|
import javax.crypto.spec.PBEKeySpec;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
import java.io.FilterInputStream;
|
import java.io.FilterInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
|
import java.security.spec.KeySpec;
|
||||||
|
|
||||||
public class CryptInputStream extends FilterInputStream {
|
public class CryptInputStream extends FilterInputStream {
|
||||||
|
private Cipher cipher;
|
||||||
|
|
||||||
public CryptInputStream(InputStream in) {
|
/**
|
||||||
super(in);
|
* @return The algorithm used for the encryption.
|
||||||
|
*/
|
||||||
|
public String getAlgorithm() {
|
||||||
|
return "AES";
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: This doesn't do anything... yet.
|
/**
|
||||||
|
* @return The mode of operation used for the encryption.
|
||||||
|
*/
|
||||||
|
public String getModeOfOperation() {
|
||||||
|
return "CFB8";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The padding used for the encryption.
|
||||||
|
*/
|
||||||
|
public String getPadding() {
|
||||||
|
return "PKCS5Padding";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The secret key algorithm used in the generation of the final key.
|
||||||
|
*/
|
||||||
|
public String getKeyAlgorithm() {
|
||||||
|
return "PBKDF2WithHmacSHA1";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The full transformation string as required by {@link Cipher#getInstance(String)}.
|
||||||
|
*/
|
||||||
|
public String getTransformationString() {
|
||||||
|
return String.format("%s/%s/%s", getAlgorithm(), getModeOfOperation(), getPadding());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public CryptInputStream(InputStream in, String key) throws InvalidKeyException {
|
||||||
|
super(in);
|
||||||
|
|
||||||
|
//Setup the cipher object
|
||||||
|
try {
|
||||||
|
cipher = Cipher.getInstance(getTransformationString());
|
||||||
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
|
||||||
|
//Should never happen, as it's predefined
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Create the salt
|
||||||
|
byte[] salt = new byte[8];
|
||||||
|
SecureRandom secureRandom = new SecureRandom();
|
||||||
|
secureRandom.nextBytes(salt);
|
||||||
|
|
||||||
|
//Create the KeySpec
|
||||||
|
//Using the recommended 65536 as iteration count
|
||||||
|
KeySpec spec = new PBEKeySpec(key.toCharArray(), salt, 65536, 256);
|
||||||
|
|
||||||
|
SecretKeyFactory factory;
|
||||||
|
try {
|
||||||
|
factory = SecretKeyFactory.getInstance(getKeyAlgorithm());
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
//Should never happen, as it's predefined
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Create the pbkdf secret key
|
||||||
|
SecretKey pbkdf;
|
||||||
|
try {
|
||||||
|
pbkdf = factory.generateSecret(spec);
|
||||||
|
} catch (InvalidKeySpecException e) {
|
||||||
|
//Should never happen, as it's predefined
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Convert" the secret key to a AES secret key
|
||||||
|
SecretKey aes = new SecretKeySpec(pbkdf.getEncoded(), getAlgorithm());
|
||||||
|
|
||||||
|
//Init the cipher instance
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, aes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int read() throws IOException {
|
||||||
|
int encryptedInt = super.read();
|
||||||
|
byte[] encryptedByte = new byte[1];
|
||||||
|
encryptedByte[0] = (byte)encryptedInt;
|
||||||
|
byte[] decryptedByte = cipher.update(encryptedByte);
|
||||||
|
return decryptedByte[0];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,68 +4,119 @@ import eu.steffo.cleaver.errors.ProgrammingError;
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
import javax.crypto.NoSuchPaddingException;
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
import javax.crypto.SecretKey;
|
||||||
|
import javax.crypto.SecretKeyFactory;
|
||||||
|
import javax.crypto.spec.PBEKeySpec;
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
import java.io.FilterOutputStream;
|
import java.io.FilterOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
|
import java.security.spec.KeySpec;
|
||||||
|
|
||||||
public class CryptOutputStream extends FilterOutputStream {
|
public class CryptOutputStream extends FilterOutputStream {
|
||||||
private Cipher cipher;
|
private Cipher cipher;
|
||||||
|
|
||||||
private final String algorithm;
|
/**
|
||||||
private final String modeOfOperation;
|
* @return The algorithm used for the encryption.
|
||||||
private final String padding;
|
*/
|
||||||
|
|
||||||
public String getAlgorithm() {
|
public String getAlgorithm() {
|
||||||
return algorithm;
|
return "AES";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The mode of operation used for the encryption.
|
||||||
|
*/
|
||||||
public String getModeOfOperation() {
|
public String getModeOfOperation() {
|
||||||
return modeOfOperation;
|
return "CFB8";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The padding used for the encryption.
|
||||||
|
*/
|
||||||
public String getPadding() {
|
public String getPadding() {
|
||||||
return padding;
|
return "PKCS5Padding";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The secret key algorithm used in the generation of the final key.
|
||||||
|
*/
|
||||||
|
public String getKeyAlgorithm() {
|
||||||
|
return "PBKDF2WithHmacSHA1";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The full transformation string as required by {@link Cipher#getInstance(String)}.
|
||||||
|
*/
|
||||||
public String getTransformationString() {
|
public String getTransformationString() {
|
||||||
return String.format("%s/%s/%s", algorithm, modeOfOperation, padding);
|
return String.format("%s/%s/%s", getAlgorithm(), getModeOfOperation(), getPadding());
|
||||||
}
|
}
|
||||||
|
|
||||||
public CryptOutputStream(OutputStream out, String key, String algorithm, String modeOfOperation, String padding) throws ProgrammingError {
|
/**
|
||||||
|
* Create a new CryptOutputStream with default {@link Cipher} parameters.
|
||||||
|
* (AES algorithm in operation mode CFB8 with PKCS5 padding)
|
||||||
|
*
|
||||||
|
* Does not use this as {@literal try} and {@literal catch} are not supported.
|
||||||
|
*
|
||||||
|
* @param out The {@link OutputStream} to connect this {@link FilterOutputStream} to.
|
||||||
|
* @param key The desired encryption key.
|
||||||
|
*/
|
||||||
|
public CryptOutputStream(OutputStream out, String key) throws InvalidKeyException {
|
||||||
super(out);
|
super(out);
|
||||||
|
|
||||||
this.algorithm = algorithm;
|
|
||||||
this.modeOfOperation = modeOfOperation;
|
|
||||||
this.padding = padding;
|
|
||||||
|
|
||||||
//Setup the cipher object
|
//Setup the cipher object
|
||||||
try {
|
try {
|
||||||
cipher = Cipher.getInstance(getTransformationString());
|
cipher = Cipher.getInstance(getTransformationString());
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
|
||||||
// This should never happen.
|
//Should never happen, as it's predefined
|
||||||
throw new ProgrammingError("Invalid algor specified in the CryptOutputStream.");
|
e.printStackTrace();
|
||||||
} catch (NoSuchPaddingException e) {
|
return;
|
||||||
// This should never happen.
|
|
||||||
throw new ProgrammingError("Invalid padding specified in the CryptOutputStream.");
|
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), this.algorithm));
|
|
||||||
} catch (InvalidKeyException e) {
|
|
||||||
// This should never happen.
|
|
||||||
throw new ProgrammingError("Invalid key specified in the CryptOutputStream.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public CryptOutputStream(OutputStream out, String key) throws ProgrammingError {
|
//Create the salt
|
||||||
this(out, key, "AES", "CBC", "PKCS5Padding");
|
byte[] salt = new byte[8];
|
||||||
|
SecureRandom secureRandom = new SecureRandom();
|
||||||
|
secureRandom.nextBytes(salt);
|
||||||
|
|
||||||
|
//Create the KeySpec
|
||||||
|
//Using the recommended 65536 as iteration count
|
||||||
|
KeySpec spec = new PBEKeySpec(key.toCharArray(), salt, 65536, 256);
|
||||||
|
|
||||||
|
SecretKeyFactory factory;
|
||||||
|
try {
|
||||||
|
factory = SecretKeyFactory.getInstance(getKeyAlgorithm());
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
//Should never happen, as it's predefined
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Create the pbkdf secret key
|
||||||
|
SecretKey pbkdf;
|
||||||
|
try {
|
||||||
|
pbkdf = factory.generateSecret(spec);
|
||||||
|
} catch (InvalidKeySpecException e) {
|
||||||
|
//Should never happen, as it's predefined
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//"Convert" the secret key to a AES secret key
|
||||||
|
SecretKey aes = new SecretKeySpec(pbkdf.getEncoded(), getAlgorithm());
|
||||||
|
|
||||||
|
//Init the cipher instance
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, aes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int b) throws IOException {
|
public void write(int decryptedInt) throws IOException {
|
||||||
//TODO
|
byte[] decryptedByte = new byte[1];
|
||||||
super.write(b);
|
decryptedByte[0] = (byte)decryptedInt;
|
||||||
|
byte[] encryptedByte = cipher.update(decryptedByte);
|
||||||
|
int encryptedInt = encryptedByte[0];
|
||||||
|
super.write(encryptedInt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue