WindowsXPKg/src/key.cpp

88 lines
2.5 KiB
C++
Raw Normal View History

/**
2023-06-17 00:58:22 +03:00
* This file is a part of the UMSKT Project
*
2023-06-17 00:58:22 +03:00
* Copyleft (C) 2019-2023 UMSKT Contributors (et.al.)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @FileCreated by Neo on 5/26/2023
* @Maintainer Andrew
*/
2023-06-01 16:09:22 +03:00
#include "header.h"
2023-06-07 22:23:59 +03:00
char pCharset[] = "BCDFGHJKMPQRTVWXY2346789";
2023-06-01 16:09:22 +03:00
/* Converts from CD-key to a byte sequence. */
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++) {
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.
BN_bn2bin(y, byteSeq);
2023-06-01 16:09:22 +03:00
BN_free(y);
// Reverse the byte sequence.
endian(byteSeq, n);
2023-06-01 16:09:22 +03:00
}
/* Converts from byte sequence to the CD-key. */
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--)
cdKey[i] = pCharset[BN_div_word(z, 24)];
2023-06-01 16:09:22 +03:00
BN_free(z);
}