gpt4 book ai didi

java - 如何使用java生成许可证 key

转载 作者:行者123 更新时间:2023-12-02 07:59:02 26 4
gpt4 key购买 nike

如何使用java程序生成25位许可证 key 。如何使用编码和解码格式将给定字符串转换为25位许可证 key 。

我使用以下代码。但它会给出许可证 key 中的前七位数字为 AAAAAAA,然后许可证 key 将在前七位数字之后发生变化。我如何获得各种许可证 key ?

        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 < tohash.length; i++) num += tohash[i];

String serial = Encode(data) + charArray[num & 31];

String ret = "";

for (int i = 0; i < 5; i++)

{

ret += serial.substring((i*5),(i*5)+5);

if (i < 4) ret += "-";

}

return ret;


}

private String Encode(byte[] data){

String ret="" ;

for (int i = 0; i < data.length; i += 5){

ret += charArray[data[i] >> 3 & 0x1f];

ret += charArray[(data[i] << 2 | data[i + 1] >> 6) & 0x1f];

ret += charArray[(data[i + 1] >> 1) & 0x1f];

ret += charArray[(data[i + 1] << 4 | data[i + 2] >> 4) & 0x1f];

ret += charArray[(data[i + 2] << 1 | data[i + 3] >> 7) & 0x1f];

ret += charArray[data[i + 3] >> 2 & 0x1f];

ret += charArray[(data[i + 3] << 3 | data[i + 4] >> 5) & 0x1f];

ret += charArray[data[i + 4] & 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 < charArray.length; i++)

{

table[charArray[i]] = (byte)i;

}

byte[] ret = new byte[x.length * 5 / 8];

int pos = 0;

for (int i = 0; i <= 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 << 3 | b2 >> 2);

ret[pos++] = (byte)(b2 << 6 | b3 << 1 | b4 >> 4);

ret[pos++] = (byte)(b4 << 4 | b5 >> 1);

ret[pos++] = (byte)(b5 << 7 | b6 << 2 | b7 >> 3);

ret[pos++] = (byte)(b7 << 5 | b8);


}

return ret;

}



public int byteArrayToInt(byte[] b, int offset) {

int value = 0;
for (int i = 0; i < 4; i++) {

int shift = (4 - 1 - i) * 8;

value += (b[i + offset] & 0x000000FF) << 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);

}

最佳答案

试试这个:

try {
byte[] hash = getHash(tohash);
System.arraycopy(hash, 0, data, 0, 15);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

当你复制时:

System.arraycopy(hash, 0, data, 5, 10);

前 5 个字节为 == 0;

关于java - 如何使用java生成许可证 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9193376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com