mirror of
https://github.com/dashr9230/SA-MP.git
synced 2025-09-19 20:26:14 +08:00
[arctool2] Add archive tool
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
|
||||
#include "KeyPair.h"
|
||||
#include "CryptoFns.h"
|
||||
#include <stdio.h>
|
||||
|
||||
//------------------------------------
|
||||
|
||||
@ -24,6 +25,17 @@ CKeyPair::~CKeyPair(void)
|
||||
|
||||
//------------------------------------
|
||||
|
||||
#ifdef ARCTOOL
|
||||
void CKeyPair::GenerateKey()
|
||||
{
|
||||
// Generate a key pair
|
||||
HCRYPTPROV hCryptProv = m_pContext->GetProvider();
|
||||
CryptGenKey(hCryptProv, AT_SIGNATURE, (ms_dwRSAKeySize << 16) | CRYPT_EXPORTABLE, &m_hCryptKey);
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------------------------------
|
||||
|
||||
void CKeyPair::ReleaseKey()
|
||||
{
|
||||
// Destroy the key pair
|
||||
@ -33,6 +45,99 @@ void CKeyPair::ReleaseKey()
|
||||
|
||||
//------------------------------------
|
||||
|
||||
#ifdef ARCTOOL
|
||||
void CKeyPair::LoadFromFile(PCHAR szFileName)
|
||||
{
|
||||
DWORD dwKeySize;
|
||||
BYTE *pbKeyBlob;
|
||||
FILE *fiKey;
|
||||
|
||||
// Load the private key from a file
|
||||
fiKey = fopen((CHAR*)szFileName, "rb");
|
||||
fread(&dwKeySize, sizeof(dwKeySize), 1, fiKey);
|
||||
pbKeyBlob = new BYTE[dwKeySize];
|
||||
fread(pbKeyBlob, 1, dwKeySize, fiKey);
|
||||
fclose(fiKey);
|
||||
|
||||
// Load the key pair
|
||||
HCRYPTPROV hCryptProv = m_pContext->GetProvider();
|
||||
CryptImportKey(hCryptProv, pbKeyBlob, dwKeySize, NULL, CRYPT_EXPORTABLE, &m_hCryptKey);
|
||||
|
||||
// Clean up memory
|
||||
delete[] pbKeyBlob;
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------------------------------
|
||||
|
||||
#ifdef ARCTOOL
|
||||
void CKeyPair::WriteToFile(PCHAR szFileName)
|
||||
{
|
||||
DWORD dwKeySize;
|
||||
BYTE *pbKeyBlob;
|
||||
FILE *fiKey;
|
||||
|
||||
// Export the private key
|
||||
CryptExportKey(m_hCryptKey, NULL, PRIVATEKEYBLOB, 0, NULL, &dwKeySize);
|
||||
pbKeyBlob = new BYTE[dwKeySize];
|
||||
CryptExportKey(m_hCryptKey, NULL, PRIVATEKEYBLOB, 0, pbKeyBlob, &dwKeySize);
|
||||
|
||||
// Write the private key to a file
|
||||
fiKey = fopen((CHAR*)szFileName, "wb");
|
||||
fwrite(&dwKeySize, sizeof(dwKeySize), 1, fiKey);
|
||||
fwrite(pbKeyBlob, 1, dwKeySize, fiKey);
|
||||
fclose(fiKey);
|
||||
|
||||
// Clean up memory
|
||||
delete[] pbKeyBlob;
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------------------------------
|
||||
|
||||
#ifdef ARCTOOL
|
||||
void CKeyPair::WriteCHeaderFile(PSTR szFileName)
|
||||
{
|
||||
const BYTE bytXORKey = 0xAA;
|
||||
DWORD dwKeySize;
|
||||
BYTE *pbKeyBlob;
|
||||
FILE *fiHeader;
|
||||
|
||||
// Export the public key
|
||||
CryptExportKey(m_hCryptKey, NULL, PUBLICKEYBLOB, 0, NULL, &dwKeySize);
|
||||
pbKeyBlob = new BYTE[dwKeySize];
|
||||
CryptExportKey(m_hCryptKey, NULL, PUBLICKEYBLOB, 0, pbKeyBlob, &dwKeySize);
|
||||
|
||||
// Generate the header file
|
||||
fiHeader = fopen(szFileName, "wt");
|
||||
fprintf(fiHeader, "//-----------------------------------------\n" );
|
||||
fprintf(fiHeader, "// SAMP Archive 2 Tool - Public Keys\n" );
|
||||
fprintf(fiHeader, "//\n" );
|
||||
fprintf(fiHeader, "// This file was automatically generated.\n" );
|
||||
fprintf(fiHeader, "// Do not modify this file!\n" );
|
||||
fprintf(fiHeader, "//-----------------------------------------\n" );
|
||||
fprintf(fiHeader, "\n" );
|
||||
fprintf(fiHeader, "#pragma once\n" );
|
||||
fprintf(fiHeader, "\n" );
|
||||
fprintf(fiHeader, "#define RSA_XOR_KEY %d\n", bytXORKey );
|
||||
fprintf(fiHeader, "\n" );
|
||||
fprintf(fiHeader, "#define RSA_PUB_KEY_SIZE %d\n", dwKeySize );
|
||||
fprintf(fiHeader, "const BYTE RSA_PUB_KEY[] = \n\t{" );
|
||||
for(DWORD i=0; i<dwKeySize; i++) {
|
||||
fprintf(fiHeader, "0x%x%s", (pbKeyBlob[i] ^ bytXORKey), (i==(dwKeySize-1)?"};":",\t"));
|
||||
if (((i+1) % 8) == 0) {
|
||||
fprintf(fiHeader, "\n\t ");
|
||||
}
|
||||
}
|
||||
fclose(fiHeader);
|
||||
|
||||
// Clean up
|
||||
delete[] pbKeyBlob;
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------------------------------
|
||||
|
||||
void CKeyPair::LoadFromMemory(DWORD dwPubKeySize, BYTE* pbPubKeyBlob, BYTE bytXORKey)
|
||||
{
|
||||
BYTE *pbKeyBlob;
|
||||
|
Reference in New Issue
Block a user