removed ability to set string seeds, byte array only

This commit is contained in:
Isaac Parenteau
2019-10-05 17:00:24 -05:00
parent fbde1970eb
commit 82f197d26f

View File

@ -7,11 +7,16 @@ import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import net.locusworks.crypto.utils.RandomString;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@ -43,9 +48,11 @@ public class AES {
private IvParameterSpec ivParamSpec;
private String seed;
private AES(byte[] seed) {
initSecureKey(seed);
}
private void initSecureKey(String seed) {
private void initSecureKey(byte[] seed) {
try {
SecureRandom sr = getSecureRandom(seed);
KeyGenerator generator = KeyGenerator.getInstance(ENCRYPTION_TYPE);
@ -65,9 +72,9 @@ public class AES {
* @throws NoSuchAlgorithmException thrown when algorithm can't be used
* @throws NoSuchProviderException thrown when the provider cant be found
*/
private static SecureRandom getSecureRandom(String seed) throws NoSuchAlgorithmException {
private static SecureRandom getSecureRandom(byte[] seed) throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance(ALGORITHM);
sr.setSeed(seed.getBytes(StandardCharsets.UTF_8));
sr.setSeed(seed);
return sr;
}
@ -123,42 +130,30 @@ public class AES {
}
}
public AES setSeed(String seed) {
if (this.seed == null || !this.seed.equals(seed)) {
initSecureKey(seed);
}
this.seed = seed;
return this;
}
public final String getSeed() {
return this.seed;
}
public static AES createInstance() {
return createInstance(RandomString.getString(16));
}
public static AES createInstance(byte[] byteSeed) {
String seed = new String(byteSeed, StandardCharsets.UTF_8);
return createInstance(seed);
public static AES createInstance(byte[] seed) {
return new AES(seed);
}
public static AES createInstance(String seed) {
AES aes = new AES();
aes.setSeed(seed);
return aes;
return createInstance(seed.getBytes(StandardCharsets.UTF_8));
}
public static void main(String[] args) throws NoSuchAlgorithmException {
if (args == null || !(args.length > 0)) {
throw new IllegalArgumentException("No args provided. Need password as argument");
}
if (args.length % 2 == 0) {
System.out.println(AES.createInstance(String.valueOf(args[1])).decrypt(String.valueOf(args[0])));
} else {
System.out.println(AES.createInstance().encrypt(String.valueOf(args[0])));
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
Path keyFile = Paths.get("../pseudo-bot/key.bin");
if (!Files.exists(keyFile)) {
throw new IOException("Unable to find key.bin in local directory");
}
byte[] key = IOUtils.toByteArray(Files.newInputStream(keyFile));
AES aes = AES.createInstance(key);
String encrypted = aes.encrypt("pseudobotAdmin2017!");
System.out.println(encrypted);
}
}