removed ability to set string seeds, byte array only
This commit is contained in:
@ -7,11 +7,16 @@ import javax.crypto.KeyGenerator;
|
|||||||
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import net.locusworks.crypto.utils.RandomString;
|
import net.locusworks.crypto.utils.RandomString;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
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.InvalidAlgorithmParameterException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
@ -43,9 +48,11 @@ public class AES {
|
|||||||
|
|
||||||
private IvParameterSpec ivParamSpec;
|
private IvParameterSpec ivParamSpec;
|
||||||
|
|
||||||
private String seed;
|
private AES(byte[] seed) {
|
||||||
|
initSecureKey(seed);
|
||||||
|
}
|
||||||
|
|
||||||
private void initSecureKey(String seed) {
|
private void initSecureKey(byte[] seed) {
|
||||||
try {
|
try {
|
||||||
SecureRandom sr = getSecureRandom(seed);
|
SecureRandom sr = getSecureRandom(seed);
|
||||||
KeyGenerator generator = KeyGenerator.getInstance(ENCRYPTION_TYPE);
|
KeyGenerator generator = KeyGenerator.getInstance(ENCRYPTION_TYPE);
|
||||||
@ -65,9 +72,9 @@ public class AES {
|
|||||||
* @throws NoSuchAlgorithmException thrown when algorithm can't be used
|
* @throws NoSuchAlgorithmException thrown when algorithm can't be used
|
||||||
* @throws NoSuchProviderException thrown when the provider cant be found
|
* @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);
|
SecureRandom sr = SecureRandom.getInstance(ALGORITHM);
|
||||||
sr.setSeed(seed.getBytes(StandardCharsets.UTF_8));
|
sr.setSeed(seed);
|
||||||
return sr;
|
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() {
|
public static AES createInstance() {
|
||||||
return createInstance(RandomString.getString(16));
|
return createInstance(RandomString.getString(16));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AES createInstance(byte[] byteSeed) {
|
public static AES createInstance(byte[] seed) {
|
||||||
String seed = new String(byteSeed, StandardCharsets.UTF_8);
|
return new AES(seed);
|
||||||
return createInstance(seed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AES createInstance(String seed) {
|
public static AES createInstance(String seed) {
|
||||||
AES aes = new AES();
|
return createInstance(seed.getBytes(StandardCharsets.UTF_8));
|
||||||
aes.setSeed(seed);
|
|
||||||
return aes;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws NoSuchAlgorithmException {
|
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
|
||||||
if (args == null || !(args.length > 0)) {
|
Path keyFile = Paths.get("../pseudo-bot/key.bin");
|
||||||
throw new IllegalArgumentException("No args provided. Need password as argument");
|
if (!Files.exists(keyFile)) {
|
||||||
}
|
throw new IOException("Unable to find key.bin in local directory");
|
||||||
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])));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
byte[] key = IOUtils.toByteArray(Files.newInputStream(keyFile));
|
||||||
|
|
||||||
|
AES aes = AES.createInstance(key);
|
||||||
|
|
||||||
|
String encrypted = aes.encrypt("pseudobotAdmin2017!");
|
||||||
|
System.out.println(encrypted);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user