Generating serial or hex code for product licensin

2019-09-16 18:11发布

问题:

Is there way to generate serial/hex code something like: xxxx-xxxx-xxxx-xxxx-xxxx-xxxx using java api? when product decipher the code it should able to extract

  1. Name
  2. Validity
  3. Mac Address
  4. Additional information

Code will be generated based on above factors. I have tried to RSA/ECB/PKCS1Padding with private key/public key. and then converting to HEX, but it needs private key sixe of 4096 and hex code generated is too long!

public class ProductKeyGenerator {
private static final String transformation = "RSA/ECB/PKCS1Padding";

public static void main(String[] args) {
    String cleartext = "CN=CompanyName;mac=some mac;@host=somehost;email=admin@somedomain.com;issued=01/01/20013;expire=12/12/2013";
    ProductKeyGenerator pgen = new ProductKeyGenerator();
    String productKey = pgen.generate(cleartext);
    System.out.println(productKey);
}

   ...
   ...
   ...
String generate(String data) {
    //encrypted hex
    String hexEnc = null;
    try {
        //--sign
        String signature = sign(data);
        data += ";sign=" + signature;

        byte[] dataBytes = data.getBytes("utf-8");
        //encrypt
        byte[] encBytes = encrypt(dataBytes);
        hexEnc = Hex.encodeHexString(encBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return hexEnc;
}

String sign(String text) {
    String signed = null;
    try {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        byte[] data = text.getBytes("utf-8");
        byte[] digitalSignature = signData(data);
        signed = Hex.encodeHexString(digitalSignature);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return signed;
}
   ...
   ...
   ...

   }

回答1:

With custom data you can't create a serial number with fixed length. But you can associate a serial number with information e.g. in database. Simple serial number generator:

String serial = UUID.randomUUID().toString().toUpperCase();
System.out.println(serial);

PS: It's very unsafe to use mac address in serial number. It's very easy to change.