title: How to generate a license key using java
body: <p>How to generate 25 digit license key using java program.how to convert the given string into 25 digit license key using encoding and decoding formats.</p>
  
  <p>I use the following code .but it will give first seven digits in license key as AAAAAAA then the license key will change after that first seven digits.how can I get various license keys?</p>
  
  <pre><code>        private String charset = "ABCDEFGHJKLMNPQRSTUVWXYZ123456789";
  
                 private char[] charArray;
  
                // Random generator = new Random();
  
                 private byte[] passwd;
  
                  public String Generate(String password)
                  {
  
                          passwd = toByteArray(password);
  
  
                        charArray = strToChar(charset);
  
                          byte[] data = new byte[15];
  
                           //generator.nextBytes(data);
  
                          byte[] tohash = new byte[5+ passwd.length];
  
                        //System.arraycopy(data, 0, tohash, 0, 5);
  
                        System.arraycopy(passwd, 0, tohash, 5, passwd.length);
  
                          try {
  
                                byte[] hash = getHash(tohash);
  
                                System.arraycopy(hash, 0, data, 5, 10);
  
                          } catch (NoSuchAlgorithmException e) {
  
                                  e.printStackTrace();
  
                          } catch (UnsupportedEncodingException e) {
  
                                  e.printStackTrace();
  
                          }
  
                          int num=0;//17
  
                          for (int i = 0; i &lt; tohash.length; i++) num += tohash[i];
  
                          String serial = Encode(data) + charArray[num &amp; 31];
  
                          String ret = "";
  
                          for (int i = 0; i &lt; 5; i++)
  
                          {
  
                                  ret += serial.substring((i*5),(i*5)+5);
  
                                  if (i &lt; 4) ret += "-";
  
                          }
  
                          return ret;
  
  
              }
  
                  private String Encode(byte[] data){
  
                          String ret="" ;
  
                          for (int i = 0; i &lt; data.length; i += 5){
  
                                  ret += charArray[data[i] &gt;&gt; 3 &amp; 0x1f];
  
                                  ret += charArray[(data[i] &lt;&lt; 2 | data[i + 1] &gt;&gt; 6) &amp; 0x1f];
  
                                  ret += charArray[(data[i + 1] &gt;&gt; 1) &amp; 0x1f];
  
                                  ret += charArray[(data[i + 1] &lt;&lt; 4 | data[i + 2] &gt;&gt; 4) &amp; 0x1f];
  
                                  ret += charArray[(data[i + 2] &lt;&lt; 1 | data[i + 3] &gt;&gt; 7) &amp; 0x1f];
  
                                  ret += charArray[data[i + 3] &gt;&gt; 2 &amp; 0x1f];
  
                                  ret += charArray[(data[i + 3] &lt;&lt; 3 | data[i + 4] &gt;&gt; 5) &amp; 0x1f];
  
                                  ret += charArray[data[i + 4] &amp; 0x1f];
                                  //
                                  System.out.println(ret);
  
                          }
  
                          return ret;
  
                  }
  
                 private byte[] Decode(String serial){
  
                          char[] x = strToChar(serial);
  
                          byte[] table = new byte[256];
  
                          for (int i = 0; i &lt; charArray.length; i++)
  
                          {
  
                                  table[charArray[i]] = (byte)i;
  
                          }
  
                          byte[] ret = new byte[x.length * 5 / 8];
  
                          int pos = 0;
  
                          for (int i = 0; i &lt;= x.length - 8; )
  
                          {
  
                                  byte b1 = table[x[i++]];
  
                                  byte b2 = table[x[i++]];
  
                                  byte b3 = table[x[i++]];
  
                                  byte b4 = table[x[i++]];
  
                                  byte b5 = table[x[i++]];
  
                                  byte b6 = table[x[i++]];
  
                                  byte b7 = table[x[i++]];
  
                                  byte b8 = table[x[i++]];
  
  
  
                                  ret[pos++] = (byte)(b1 &lt;&lt; 3 | b2 &gt;&gt; 2);
  
                                  ret[pos++] = (byte)(b2 &lt;&lt; 6 | b3 &lt;&lt; 1 | b4 &gt;&gt; 4);
  
                                  ret[pos++] = (byte)(b4 &lt;&lt; 4 | b5 &gt;&gt; 1);
  
                                  ret[pos++] = (byte)(b5 &lt;&lt; 7 | b6 &lt;&lt; 2 | b7 &gt;&gt; 3);
  
                                  ret[pos++] = (byte)(b7 &lt;&lt; 5 | b8);
  
  
                          }
  
                          return ret;
  
                  }
  
  
  
          public int byteArrayToInt(byte[] b, int offset) {
  
          int value = 0;
          for (int i = 0; i &lt; 4; i++) {
  
              int shift = (4 - 1 - i) * 8;
  
              value += (b[i + offset] &amp; 0x000000FF) &lt;&lt; shift;
  
  
          }
  
          return value;
  
      }
  
          private char[] strToChar(String str){
  
                  char[] c = str.toCharArray();
  
                  return c;
  
          }
  
          public byte[] toByteArray(String p) {
  
                  String stringToConvert = p;
  
                  byte[] theByteArray = stringToConvert.getBytes();
  
                  return theByteArray;
  
          }
  
         public byte[] getHash(byte[] toHash) throws NoSuchAlgorithmException, UnsupportedEncodingException {
  
                  MessageDigest digest = MessageDigest.getInstance("SHA-256");
  
                  digest.reset();
  
                  return digest.digest(toHash);
  
          }
  </code></pre>