Issue
I'm trying to make a license generator, follow the code below:
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class GerarLicenca{
static String pass = "abccoss!23167#ms";
public static void main(String[] args) throws Exception{
if(args.length > 0){
if(args[0].equals("encode") && args.length >= 3){
encode(args);
}else if(args[0].equals("decode") && args.length >= 2){
decode(args);
}
}
System.out.println("Por favor executar:");
System.out.println("java GerarLicenca encode 'grails-framework' '10/10/2020 12:00'");
System.out.println("java GerarLicenca decode 'zlhMh05Op/6xmYhOBcQj0pnVgbA8gqiXP1rIxYhvD4U='");
}
static public void encode(String[] args) throws Exception{
if(!args[1].isEmpty() && !args[2].isEmpty()){
System.out.println("Gerando licença");
String licText = args[0] + "|"+args[1];
String encryptedBase64Data = new String(encrypt(licText.getBytes("UTF8")));
System.out.println("Licença gerada, cpiar abaixo:");
System.out.println(encryptedBase64Data);
}else{
if(args[1].isEmpty()){
System.out.println("Por favor informar app name");
}
if(args[2].isEmpty()){
System.out.println("Por favor informar data");
}
}
}
static public void decode(String[] args) throws Exception{
if(!args[1].isEmpty()){
String licText = new String(decrypt(args[1].getBytes("UTF8")));
String [] info = licText.split("\\|");
Date maxDate = null;
String appName = null;
try {
if (info.length == 2) {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
maxDate = df.parse(info[1]);
appName = info[0];
System.out.println("Data: "+maxDate+ " app Name: "+appName);
} else {
System.out.println("Erro ao descriptografar");
}
} catch (ParseException ex) {
System.out.println(ex);
}
}
}
static public byte[] getpass() throws Exception{
return pass.getBytes("UTF8");
}
static public byte[] encrypt(byte[] data) throws Exception {
Cipher c = Cipher.getInstance("AES");
SecretKeySpec k = new SecretKeySpec(getpass(), "AES");
c.init(Cipher.ENCRYPT_MODE, k);
byte[] encryptedData = c.doFinal(data);
return Base64.encodeBase64(encryptedData);
}
static public byte[] decrypt(byte[] base64EncrypteData) throws Exception {
Cipher c = Cipher.getInstance("AES");
SecretKeySpec k = new SecretKeySpec(getpass(), "AES");
c.init(Cipher.DECRYPT_MODE, k);
byte[] debase64EncryptedData = Base64.decodeBase64(base64EncrypteData);
return c.doFinal(debase64EncryptedData);
}
}
and I'm compiling from the command line follow the print
As you can see from the print it gives an error because it is not being able to import the
import org.apache.commons.codec.binary.Base64;
however I downloaded the .jar and it's inside my folder, and I'm going through the command in javac
javac -cp commons-codec-1.7.jar:. GenerateLicenca.java
but nothing works, can someone give me a light on how to do this, other than by IDE, do it directly from the command line?
Solution
Specify the classpath when you run your program:
java -cp commons-codec-1.7.jar:. GenerateLicenca encode 'grails-framework' '10/10/2020 12:00'
Answered By - David Conrad