first commit

This commit is contained in:
MaddoScientisto 2026-03-14 20:04:39 +01:00
commit 4d332ef662
27586 changed files with 3281783 additions and 0 deletions

View file

@ -0,0 +1,407 @@
package com.ablia.reg;
import com.ablia.util.ConsoleInput;
import com.ablia.util.UnicodeFormatter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class EcDc extends ConsoleInput {
public static final String MASTER_KEY = "Xg3Z5sFQ";
public static final String ALG_MASTER = "_MASTER";
public static final String ALG_SHA_1 = "SHA-1";
public static final String ALG_SHA_256 = "SHA-256";
public static final String ALG_SHA_384 = "SHA-384";
public static final String ALG_SHA_512 = "SHA-512";
public static final String ALG_AES = "AES";
public static final String ALG_DIZIONARIO = "ALG_DIZIONARIO";
public static final long ALG_CODE_AES = 2L;
public static final long ALG_CODE_DIZIONARIO = 1L;
public static final long ALG_CODE_64 = 0L;
private static final String UTF_8 = "UTF-8";
private static final String AES_ALGORITH = "AES/ECB/PKCS5Padding";
private static SecretKeySpec secretKey;
private static byte[] key;
private static String[] dizionario = new String[] {
"1", "2", "3", "4", "5", "6", "7", "8",
"9", "0",
".", "@", "a", "b", "c", "d", "e", "f", "g", "h",
"i",
"j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v",
"w", "x", "y", "z", "A", "B",
"C", "D", "E", "F", "G", "H", "I",
"J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z", ",", ";", ":", "_", "-", "+",
"*", "/", "?",
"'", "^", "à", "è", "è", "ì", "ò",
"ù", " " };
private String encodedFileName;
private String plainFileName;
private static String[] creaDizionario(String lettera) {
String[] appoggio = new String[dizionario.length];
System.arraycopy(dizionario, 0, appoggio, 0, dizionario.length);
for (int k = 0; k < dizionario.length; k++) {
if (dizionario[k].equals(lettera))
break;
}
for (int j = 0; j < dizionario.length; j++) {
appoggio[j] = dizionario[k];
if (k < dizionario.length - 1) {
k++;
} else {
k = 0;
}
}
return appoggio;
}
public static String encodeDizionario(String theString, String chiave) {
String[] appoggio = new String[dizionario.length];
StringBuffer ret = new StringBuffer();
while (theString.length() > chiave.length())
chiave = String.valueOf(chiave) + chiave;
for (int i = 0; i < theString.length(); i++) {
appoggio = creaDizionario(String.valueOf(chiave.charAt(i)));
for (int j = 0; j < dizionario.length; j++) {
if (dizionario[j].equals(String.valueOf(theString.charAt(i)))) {
ret.append(appoggio[j]);
break;
}
}
}
return ret.toString();
}
public static String decodeDizionario(String l_string, String chiave) {
String[] appoggio = new String[dizionario.length];
StringBuffer ret = new StringBuffer();
while (l_string.length() > chiave.length())
chiave = String.valueOf(chiave) + chiave;
System.out.println(" ");
for (int i = 0; i < l_string.length(); i++) {
appoggio = creaDizionario(String.valueOf(chiave.charAt(i)));
for (int j = 0; j < appoggio.length; j++) {
if (appoggio[j].equals(String.valueOf(l_string.charAt(i)))) {
ret.append(dizionario[j]);
break;
}
}
}
return ret.toString();
}
public static String decodeIntKey(String stringToDecode, int key) {
String strTesto2 = "";
for (int i = 0; i < stringToDecode.length(); i++) {
int intValore = UnicodeFormatter.Asc(stringToDecode.charAt(i));
int intResto = (key + intValore) % 256;
if (intValore + key < 256) {
strTesto2 = String.valueOf(strTesto2) + UnicodeFormatter.decToChar(intValore - key);
} else {
strTesto2 = String.valueOf(strTesto2) + UnicodeFormatter.decToChar(256 - key + intResto);
}
}
return strTesto2;
}
public static String encodeIntKey(String stringToDecode, int Key) {
int intKey = Key;
String strTesto2 = "";
for (int i = 0; i < stringToDecode.length(); i++) {
int intValore = UnicodeFormatter.Asc(stringToDecode.charAt(i)) + intKey;
intValore %= 256;
strTesto2 = String.valueOf(strTesto2) + UnicodeFormatter.decToChar(intValore);
}
return strTesto2;
}
public EcDc() {}
public EcDc(String l_plainFile, String l_encodedFile) {
setPlainFileName(l_plainFile);
setEncodedFileName(l_encodedFile);
}
public static String decode64String(String stringToDecode) {
try {
BASE64Decoder en = new BASE64Decoder();
return new String(en.decodeBuffer(stringToDecode));
} catch (Exception e) {
return null;
}
}
public void decodeFile() {
if (getEncodedFileName().isEmpty())
return;
BASE64Decoder en = new BASE64Decoder();
try {
FileInputStream fis = new FileInputStream(getEncodedFileName());
FileOutputStream fos = new FileOutputStream(getPlainFileName());
en.decodeBuffer(fis, fos);
fos.flush();
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String encode64String(String stringToEncode) {
BASE64Encoder en = new BASE64Encoder();
return en.encode(stringToEncode.getBytes());
}
public void encodeFile() {
if (getPlainFileName().isEmpty())
return;
BASE64Encoder en = new BASE64Encoder();
try {
FileInputStream fis = new FileInputStream(getPlainFileName());
FileOutputStream fos = new FileOutputStream(getEncodedFileName());
en.encode(fis, fos);
fos.flush();
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getEncodedFileName() {
return this.encodedFileName;
}
public String getPlainFileName() {
return this.plainFileName;
}
public void setEncodedFileName(String string) {
this.encodedFileName = string;
}
public void setPlainFileName(String string) {
this.plainFileName = string;
}
public static String decodeAES(String strToDecrypt, String secret) {
try {
setSecretKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(2, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
} catch (Exception e) {
System.out.println("Error while decrypting: " + e.toString());
return null;
}
}
public static String encodeAES(String strToEncrypt, String secret) {
try {
setSecretKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(1, secretKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
} catch (Exception e) {
System.out.println("Error while encrypting: " + e.toString());
return null;
}
}
public static final String getEncodeAlgoritmo(long l_flgEncodeAlgoritmo) {
if (l_flgEncodeAlgoritmo == 0L)
return "Base 64";
if (l_flgEncodeAlgoritmo == 2L)
return "AES";
if (l_flgEncodeAlgoritmo == 1L)
return "Dizionario";
return "";
}
private static void setKey(String myKey) {
setSecretKey(myKey);
}
private static void setSecretKey(String myKey) {
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String decode(long algCode, String stringToDecode, String secret) {
if (algCode == 0L)
return decode64String(stringToDecode);
if (algCode == 2L)
return decodeAES(stringToDecode, secret);
if (algCode == 1L)
return decodeDizionario(stringToDecode, secret);
return stringToDecode;
}
public static String encode(long algCode, String strToEncrypt, String secret) {
if (algCode == 0L)
return encode64String(strToEncrypt);
if (algCode == 2L)
return encodeAES(strToEncrypt, secret);
if (algCode == 1L)
return encodeDizionario(strToEncrypt, secret);
return strToEncrypt;
}
public static String cryptSHA1Plain(String stringToEncode) {
return cryptSHAPlain(stringToEncode, "SHA-1");
}
public static String cryptSHA(String stringToCrypt, String shaAlgorithm) {
try {
return encode64String(cryptSHAPlain(stringToCrypt, shaAlgorithm));
} catch (Exception e) {
e.printStackTrace();
return stringToCrypt;
}
}
public static String cryptMD5Plain(String stringToEncode) {
byte[] defaultBytes = stringToEncode.getBytes();
String res = "";
try {
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(defaultBytes);
byte[] messageDigest = algorithm.digest();
res = convertToHex(messageDigest);
} catch (NoSuchAlgorithmException nsae) {
nsae.printStackTrace();
res = "MD5 error encode";
}
return res;
}
public static String cryptMD5(String stringToCrypt) {
try {
return encode64String(cryptMD5Plain(stringToCrypt));
} catch (Exception e) {
e.printStackTrace();
return stringToCrypt;
}
}
private static String cryptSHAPlain(String stringToEncode, String shaAlgorithm) {
byte[] defaultBytes = stringToEncode.getBytes();
String res = "";
try {
MessageDigest algorithm = MessageDigest.getInstance(shaAlgorithm);
algorithm.reset();
algorithm.update(defaultBytes);
byte[] messageDigest = algorithm.digest();
res = convertToHex(messageDigest);
} catch (NoSuchAlgorithmException nsae) {
nsae.printStackTrace();
res = "SHA error encode: " + shaAlgorithm;
}
return res;
}
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; ) {
int halfbyte = data[i] >>> 4 & 0xF;
int two_halfs = 0;
while (true) {
if (halfbyte >= 0 && halfbyte <= 9) {
buf.append((char)(48 + halfbyte));
} else {
buf.append((char)(97 + halfbyte - 10));
}
halfbyte = data[i] & 0xF;
if (two_halfs++ >= 1)
i++;
}
}
return buf.toString();
}
public static void main(String[] args) {
EcDc ecdc = new EcDc();
String temp = "testo da convertire";
String chiave = "la chiave";
System.out.println(" : " + temp + " chiave: " + chiave);
System.out.println("\ndizionario: ");
String cr = encodeDizionario(temp, chiave);
String dec = decodeDizionario(cr, chiave);
System.out.println("critta: " + cr);
System.out.println("decrit: " + dec);
System.out.println("\nchiave numerica 55: ");
cr = encodeIntKey(temp, 55);
dec = decodeIntKey(cr, 55);
System.out.println("critta: " + cr);
System.out.println("decrit: " + dec);
System.out.println("\nAES: ");
cr = encodeAES(temp, chiave);
dec = decodeAES(cr, chiave);
System.out.println("critta: " + cr);
System.out.println("decrit: " + dec);
System.out.println("\nAES2: ");
cr = EcDcAES.encryptAES(temp, chiave);
dec = EcDcAES.decryptAES(cr, chiave);
System.out.println("critta: " + cr);
System.out.println("decrit: " + dec);
}
public static String encodeHMAC_256(String key, String data) {
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secret_key);
return new String(Hex.encodeHex(sha256_HMAC.doFinal(data.getBytes("UTF-8"))));
} catch (Exception e) {
e.printStackTrace();
return "ERR!";
}
}
}

View file

@ -0,0 +1,54 @@
package com.ablia.reg;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class EcDcAES {
private static SecretKeySpec secretKey;
private static byte[] key;
public static void setKey(String myKey) {
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String encryptAES(String strToEncrypt, String secret) {
try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(1, secretKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
} catch (Exception e) {
System.out.println("Error while encrypting: " + e.toString());
return null;
}
}
public static String decryptAES(String strToDecrypt, String secret) {
try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(2, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
} catch (Exception e) {
System.out.println("Error while decrypting: " + e.toString());
return null;
}
}
}

View file

@ -0,0 +1,13 @@
package com.ablia.reg;
public class RegException extends Exception {
private static final long serialVersionUID = 1L;
public RegException(String msg) {
super(msg);
}
public RegException(Exception e) {
super(e.getMessage());
}
}

View file

@ -0,0 +1,65 @@
package com.ablia.reg;
import java.io.Serializable;
import java.sql.Date;
import java.util.Hashtable;
public class RegKey implements Serializable {
private static final long serialVersionUID = 1L;
private boolean ready = false;
private Hashtable keys;
private boolean checking = false;
public boolean isReady() {
return this.ready;
}
public void setReady(boolean b) {
this.ready = b;
}
public boolean isChecking() {
return this.checking;
}
public void setChecking(boolean b) {
this.checking = b;
}
protected Hashtable getKeys() {
if (this.keys == null)
this.keys = new Hashtable();
return this.keys;
}
public int getKeyI(String l_key) {
if (getKeys().get(l_key) != null)
return (Integer)getKeys().get(l_key);
return 0;
}
public String getKeyS(String l_key) {
if (getKeys().get(l_key) != null)
return (String)getKeys().get(l_key);
return "";
}
public long getKeyL(String l_key) {
if (getKeys().get(l_key) != null)
return (Long)getKeys().get(l_key);
return 0L;
}
public Date getKeyD(String l_key) {
if (getKeys().get(l_key) != null)
return (Date)getKeys().get(l_key);
return null;
}
public void setKey(String l_key, Object l_value) {
getKeys().put(l_key, l_value);
}
}

View file

@ -0,0 +1,23 @@
package com.ablia.reg;
public interface RegKeyC {
public static final int CMD_ATTIVA = 2;
public static final int CMD_LOG = 1;
public static final int TL_DEVEL = 0;
public static final int TL_UNLIMITED = 1;
public static final int TL_TIME = 2;
public static final String K_CUSTOMERNUMBER_L = "_CN_";
public static final String K_DATASCADENZA_D = "_DS_";
public static final String K_TIPOLIC_I = "_TL_";
public static final String K_LICENZA_S = "_LIC";
public static final String K_MAXHITS_L = "_MH_";
}

View file

@ -0,0 +1,27 @@
package com.ablia.reg;
import java.io.Serializable;
public class RegRequest implements Serializable {
private static final long serialVersionUID = 1L;
private int cmd = 0;
private RegKey regkey;
public int getCmd() {
return this.cmd;
}
public RegKey getRegkey() {
return this.regkey;
}
public void setCmd(int i) {
this.cmd = i;
}
public void setRegkey(RegKey key) {
this.regkey = key;
}
}

View file

@ -0,0 +1,37 @@
package com.ablia.reg;
import java.io.Serializable;
public class RegResponse implements Serializable {
private static final long serialVersionUID = 1L;
private boolean exception;
private RegException regException;
private RegKey regkey;
public RegException getRegException() {
return this.regException;
}
public RegKey getRegkey() {
return this.regkey;
}
public boolean isException() {
return this.exception;
}
public void setException(boolean newException) {
this.exception = newException;
}
public void setRegException(RegException exception) {
this.regException = exception;
}
public void setRegkey(RegKey key) {
this.regkey = key;
}
}