0

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;
}
   ...
   ...
   ...

   }
gpa
  • 2,411
  • 6
  • 38
  • 68

1 Answers1

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.

  • Why can't you create a serial number with fixed length? Sure if have completely unrelated data and try to group them together, you may have a bad time, but if the data are related, I don't seewhy one would **never** be able to come up with a fixed length serial. Try not to speak in extreme generalities ("can't create"), you'll almost always be wrong. – Steve P. Aug 02 '13 at 15:55
  • It's better to store a serial number in a database on any. – Сашка724ая Aug 02 '13 at 16:00
  • I like the idea of using UUID, UUID uuid = UUID.nameUUIDFromBytes(cleartext.getBytes("UTF-8")); But now, is there anything on donet that generate same UUID for given text? i have posted another [http://stackoverflow.com/questions/18021808/uuid-interop-with-c-sharp-code] – gpa Aug 02 '13 at 16:21
  • `UUID.nameUUIDFromBytes(byte[])` will not allow you to extract the information back out of the UUID (it's a one way function). Is this not a requirement? (Your question says it is). – Syon Aug 02 '13 at 16:37
  • @AndreyRedov Thanks for providing starting point. user Syon was big help from dotnet side. – gpa Aug 02 '13 at 19:54