2023-06-01 16:09:22 +03:00
|
|
|
//
|
|
|
|
// Created by neo on 5/26/2023.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "header.h"
|
|
|
|
|
|
|
|
/* Converts from CD-key to a byte sequence. */
|
2023-06-04 15:40:08 +03:00
|
|
|
void unbase24(BYTE *byteSeq, const char *cdKey) {
|
2023-06-04 13:31:24 +03:00
|
|
|
BYTE pDecodedKey[PK_LENGTH + NULL_TERMINATOR]{};
|
2023-06-01 16:09:22 +03:00
|
|
|
BIGNUM *y = BN_new();
|
|
|
|
|
|
|
|
BN_zero(y);
|
|
|
|
|
|
|
|
// Remove dashes from the CD-key and put it into a Base24 byte array.
|
|
|
|
for (int i = 0, k = 0; i < strlen(cdKey) && k < PK_LENGTH; i++) {
|
|
|
|
for (int j = 0; j < 24; j++) {
|
2023-06-04 15:40:08 +03:00
|
|
|
if (cdKey[i] != '-' && cdKey[i] == pCharset[j]) {
|
2023-06-01 16:09:22 +03:00
|
|
|
pDecodedKey[k++] = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty byte sequence.
|
|
|
|
memset(byteSeq, 0, 16);
|
|
|
|
|
|
|
|
// Calculate the weighed sum of byte array elements.
|
|
|
|
for (int i = 0; i < PK_LENGTH; i++) {
|
|
|
|
BN_mul_word(y, PK_LENGTH - 1);
|
|
|
|
BN_add_word(y, pDecodedKey[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Acquire length.
|
|
|
|
int n = BN_num_bytes(y);
|
|
|
|
|
|
|
|
// Place the generated code into the byte sequence.
|
2023-06-04 15:40:08 +03:00
|
|
|
BN_bn2bin(y, byteSeq);
|
2023-06-01 16:09:22 +03:00
|
|
|
BN_free(y);
|
|
|
|
|
|
|
|
// Reverse the byte sequence.
|
2023-06-04 15:40:08 +03:00
|
|
|
endian(byteSeq, n);
|
2023-06-01 16:09:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Converts from byte sequence to the CD-key. */
|
2023-06-04 15:40:08 +03:00
|
|
|
void base24(char *cdKey, BYTE *byteSeq) {
|
2023-06-04 13:31:24 +03:00
|
|
|
BYTE rbyteSeq[16];
|
2023-06-01 16:09:22 +03:00
|
|
|
BIGNUM *z;
|
|
|
|
|
|
|
|
// Copy byte sequence to the reversed byte sequence.
|
|
|
|
memcpy(rbyteSeq, byteSeq, sizeof(rbyteSeq));
|
|
|
|
|
|
|
|
// Skip trailing zeroes and reverse y.
|
|
|
|
int length;
|
|
|
|
|
|
|
|
for (length = 15; rbyteSeq[length] == 0; length--);
|
|
|
|
endian(rbyteSeq, ++length);
|
|
|
|
|
|
|
|
// Convert reversed byte sequence to BigNum z.
|
|
|
|
z = BN_bin2bn(rbyteSeq, length, nullptr);
|
|
|
|
|
|
|
|
// Divide z by 24 and convert the remainder to a CD-key char.
|
|
|
|
cdKey[25] = 0;
|
|
|
|
|
|
|
|
for (int i = 24; i >= 0; i--)
|
2023-06-04 15:40:08 +03:00
|
|
|
cdKey[i] = pCharset[BN_div_word(z, 24)];
|
2023-06-01 16:09:22 +03:00
|
|
|
|
|
|
|
BN_free(z);
|
|
|
|
}
|