commit
d67d346100
12 changed files with 784 additions and 0 deletions
-
21CMakeLists.txt
-
217Ne-code.cpp
-
110keya.h
-
47keya_types.h
-
8lib/.idea/.gitignore
-
8lib/.idea/lib.iml
-
8lib/.idea/modules.xml
-
4lib/.idea/vcs.xml
-
BINlib/libkeya.a
-
BINlib/libkeyac.a
-
148main.cpp
-
213setKey.cpp
@ -0,0 +1,21 @@ |
|||||
|
|
||||
|
cmake_minimum_required(VERSION 2.6) |
||||
|
|
||||
|
project(KeyASample) |
||||
|
|
||||
|
find_library(KEYAC keyac) |
||||
|
find_library(KEYA keya) |
||||
|
|
||||
|
set(cppdongle_sources Ne-code.cpp) |
||||
|
|
||||
|
set(cppdongle_headers ) |
||||
|
|
||||
|
set(KEYA_LIB_DIR ${CMAKE_SOURCE_DIR}/lib/) |
||||
|
|
||||
|
|
||||
|
link_directories(${KEYA_LIB_DIR}) |
||||
|
|
||||
|
add_executable(run-keya2 ${cppdongle_sources} ${cppdongle_headers}) |
||||
|
target_link_libraries(run-keya2 keya keyac pthread) |
||||
|
|
||||
|
|
@ -0,0 +1,217 @@ |
|||||
|
//
|
||||
|
// Created by mht on 20.04.25.
|
||||
|
//
|
||||
|
#include <fstream>
|
||||
|
#include <vector>
|
||||
|
#include <iostream>
|
||||
|
#include <cstring>
|
||||
|
#include <string>
|
||||
|
#include <sstream>
|
||||
|
#include "keya.h"
|
||||
|
|
||||
|
using namespace std; |
||||
|
|
||||
|
// Helper to pad data to 16 bytes
|
||||
|
void pad_data(vector<unsigned char>& data) |
||||
|
{ |
||||
|
size_t pad_len = 16 - (data.size() % 16); |
||||
|
if (pad_len < 16) { |
||||
|
data.insert(data.end(), pad_len, 0); // simple zero padding
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Read file into vector
|
||||
|
vector<unsigned char> read_file(const string& filename) |
||||
|
{ |
||||
|
ifstream file(filename, ios::binary); |
||||
|
return vector<unsigned char>((istreambuf_iterator<char>(file)), istreambuf_iterator<char>()); |
||||
|
} |
||||
|
|
||||
|
// Write vector into file
|
||||
|
void write_file(const string& filename, const vector<unsigned char>& data) |
||||
|
{ |
||||
|
ofstream file(filename, ios::binary); |
||||
|
file.write(reinterpret_cast<const char*>(data.data()), data.size()); |
||||
|
} |
||||
|
|
||||
|
// Encrypt buffer using KeyA
|
||||
|
int encrypt_buffer(vector<unsigned char>& data, unsigned char* kp, unsigned char* kr, unsigned char* kw) |
||||
|
{ |
||||
|
KeyA keya; |
||||
|
int result = keya.OpenDevice(0); |
||||
|
if (result != 0) return result; |
||||
|
|
||||
|
result = keya.Init(kp, kr, kw, NULL); |
||||
|
if (result != 0) { |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
unsigned char temp[16]; |
||||
|
for (size_t i = 0; i < data.size(); i += 16) { |
||||
|
memcpy(temp, &data[i], 16); |
||||
|
result = keya.EncByKV(0, temp, temp); |
||||
|
if (result != 0) { |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
memcpy(&data[i], temp, 16); |
||||
|
} |
||||
|
|
||||
|
keya.CloseDevice(); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
// Decrypt buffer using KeyA
|
||||
|
int decrypt_buffer(vector<unsigned char>& data, unsigned char* kp, unsigned char* kr, unsigned char* kw) |
||||
|
{ |
||||
|
KeyA keya; |
||||
|
int result = keya.OpenDevice(0); |
||||
|
if (result != 0) return result; |
||||
|
|
||||
|
result = keya.Init(kp, kr, kw, NULL); |
||||
|
if (result != 0) { |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
unsigned char temp[16]; |
||||
|
for (size_t i = 0; i < data.size(); i += 16) { |
||||
|
memcpy(temp, &data[i], 16); |
||||
|
result = keya.DecByKV(0, temp, temp); |
||||
|
if (result != 0) { |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
memcpy(&data[i], temp, 16); |
||||
|
} |
||||
|
|
||||
|
keya.CloseDevice(); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
// Save size as first 8 bytes (uint64_t) of encrypted file
|
||||
|
void prepend_size(vector<unsigned char>& data, uint64_t size) |
||||
|
{ |
||||
|
vector<unsigned char> result(8); |
||||
|
for (int i = 0; i < 8; ++i) { |
||||
|
result[i] = (size >> (i * 8)) & 0xFF; |
||||
|
} |
||||
|
result.insert(result.end(), data.begin(), data.end()); |
||||
|
data = result; |
||||
|
} |
||||
|
|
||||
|
// Extract original size from first 8 bytes
|
||||
|
uint64_t extract_size(vector<unsigned char>& data) |
||||
|
{ |
||||
|
uint64_t size = 0; |
||||
|
for (int i = 7; i >= 0; --i) { |
||||
|
size = (size << 8) | data[i]; |
||||
|
} |
||||
|
data.erase(data.begin(), data.begin() + 8); |
||||
|
return size; |
||||
|
} |
||||
|
|
||||
|
// Save list of hashes into a text file
|
||||
|
void save_hashes_to_file(const vector<string>& hashes, const string& filename) |
||||
|
{ |
||||
|
ofstream file(filename); |
||||
|
for (const auto& hash : hashes) { |
||||
|
file << hash << "\n"; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Read hashes from decrypted data
|
||||
|
vector<string> read_hashes_from_memory(const vector<unsigned char>& data) |
||||
|
{ |
||||
|
vector<string> hashes; |
||||
|
istringstream iss(string(data.begin(), data.end())); |
||||
|
string line; |
||||
|
while (getline(iss, line)) { |
||||
|
if (!line.empty()) |
||||
|
hashes.push_back(line); |
||||
|
} |
||||
|
return hashes; |
||||
|
} |
||||
|
|
||||
|
// Check if a hash exists in list
|
||||
|
bool hash_exists(const string& input_hash, const vector<string>& hashes) |
||||
|
{ |
||||
|
for (const auto& hash : hashes) { |
||||
|
if (hash == input_hash) { |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
int main() |
||||
|
{ |
||||
|
// Your keys
|
||||
|
unsigned char g_Kp[16] = {0x74,0xD8,0xCD,0x0F,0xC6,0x6A,0x09,0x69,0xB1,0x4A,0xC5,0xD1,0xE9,0x7A,0x9D,0x07}; |
||||
|
unsigned char g_Kr[16] = {0xDA,0x58,0x86,0x3C,0xD6,0x65,0xDF,0xE2,0x84,0x04,0xD3,0x4E,0x74,0xEB,0xBD,0x16}; |
||||
|
unsigned char g_Kw[16] = {0xC8,0xA8,0x16,0x3C,0x59,0xEF,0x9C,0xC4,0xC0,0xEC,0x48,0x41,0x4B,0xE5,0x11,0x37}; |
||||
|
|
||||
|
unsigned char* kp = g_Kp; |
||||
|
unsigned char* kr = g_Kr; |
||||
|
unsigned char* kw = g_Kw; |
||||
|
|
||||
|
// Step 1: Save some hashes to file
|
||||
|
vector<string> hashes = { |
||||
|
"8d523c784e2f81de923e2fad5960983b735aca25b2490a1cd50d91dc4b166795b27e59f8dfb76ef5120460224cc9ab5d489dc9ae135a732588dd43f71048771c", |
||||
|
"dd590a463303424547f76562fbaeca485c1b95581b7340ecdd02e308166ddcfcac757ed23b11931d23ca46e1b6498fe3c95bf2fd7f760337b41d9e7f4e396678", |
||||
|
"75863d940c8f298c92ccdcfeca6d809d2e5bf67d23c588d2bd62f4b8f86be8a882e30b9b5e524b79e8cf618290b18eec8b621d9c9d73f5300246bd3e15c99561", |
||||
|
"b6d7ec0a2520e2a2ba8f7bb4807b4f3641b80f1062075028600201b1b01e4ac102e078061cd4618f2e847348b118a11be1341a9d3a41fcfc443f92c69c40ffa1", |
||||
|
"4347b58249eea37f74c8a036ca29d6b2a362b9a8743bbd4f99de75c92b46fd876bf98a65041c28c9f10eb548df7645079fe616b00e42711d52135034fe5dbc55", |
||||
|
"a254cc5e08d0d11da6074fccbbc5fbff7a2ce99e6a36f6294f66e271455d3f0663f70864a74ea7d307c0ad7d76fa30694b52dcd9385a8de21b9a708a791d6a8c", |
||||
|
"f9f0ce3eed54de9e057fd66ae17e4b2dccb5b4b6c17e18a278e6628a8a5ac4e357f06c510370e06da2a62241a3200f3d5334102e045c36b5a74954f2ff070a79", |
||||
|
"a1475fdb4b02de84a57f85babc010e5f12281eedea1c75efeac54af7e40c0dfa1659eaa0b106c6aa3a605c7ec442f477d425db24b75c8a818750d9fe06e3727a", |
||||
|
"183d402bb712787f5aa8bf3f0cbe91d3383fdd460edac3db01ea756ae5afe9ca60d8917d749358a012b2b45eaae04ae5fc10fe17aa42409841dc5b475c2794a1", |
||||
|
"a29fc7db04adb89736832cd94dd1fd3fe405e18db670ef3f055e3310dd2f0fc4dd3c4535adcd1ec3fcd16f45a8b814558586bfc67b1d01d664cd51ee1db05c28", |
||||
|
"0092d1a3a78bab2b85abc55d5a851c3c351d0dacd402d77ad57b6d3f08836262bc7dff8b958368cddedd3aa8246ec4919f77c325210cabeadd2dfde5ce8d0412", |
||||
|
"0485a2fd8aceaddfeb45f4072f64637bec8ab5962e088c4f8f0a46196d6b80c1a2924e1d43fa530b2183e5240507115a3db8d49aae9d100195a6ba1eb8f1adcf", |
||||
|
"ec23e51688c082584821a20038510a818c58a0201dbb7ee133d0b11dfc6456a2ad1de4dd944e8083e378af4e58362b5c3ecf3f242493e0cfc1c1cde58e7fc8f1" |
||||
|
|
||||
|
}; |
||||
|
save_hashes_to_file(hashes, "hashes.txt"); |
||||
|
cout << "Hashes saved to 'hashes.txt'." << endl; |
||||
|
|
||||
|
// Step 2: Encrypt the hashes file
|
||||
|
vector<unsigned char> hash_data = read_file("hashes.txt"); |
||||
|
|
||||
|
uint64_t original_size = hash_data.size(); // Save original size
|
||||
|
pad_data(hash_data); // pad to multiple of 16
|
||||
|
|
||||
|
if (encrypt_buffer(hash_data, kp, kr, kw) == 0) { |
||||
|
prepend_size(hash_data, original_size); // Put size in front
|
||||
|
write_file("lic.enc", hash_data); |
||||
|
cout << "Encryption successful. Encrypted file saved as 'lic.enc'." << endl; |
||||
|
} else { |
||||
|
cout << "Encryption failed." << endl; |
||||
|
return 1; |
||||
|
} |
||||
|
|
||||
|
// Step 3: Later... Decrypt and check for input hash
|
||||
|
vector<unsigned char> enc_data = read_file("hashes.enc"); |
||||
|
uint64_t saved_size = extract_size(enc_data); |
||||
|
|
||||
|
if (decrypt_buffer(enc_data, kp, kr, kw) == 0) { |
||||
|
enc_data.resize(saved_size); // trim padding
|
||||
|
vector<string> decrypted_hashes = read_hashes_from_memory(enc_data); |
||||
|
|
||||
|
string input_hash; |
||||
|
cout << "Enter a hash to search for: "; |
||||
|
cin >> input_hash; |
||||
|
|
||||
|
if (hash_exists(input_hash, decrypted_hashes)) { |
||||
|
cout << "Hash FOUND in list." << endl; |
||||
|
} else { |
||||
|
cout << "Hash NOT FOUND." << endl; |
||||
|
} |
||||
|
} else { |
||||
|
cout << "Decryption failed." << endl; |
||||
|
return 1; |
||||
|
} |
||||
|
|
||||
|
return 0; |
||||
|
} |
@ -0,0 +1,110 @@ |
|||||
|
/* |
||||
|
* KeyA.h |
||||
|
* |
||||
|
* Created on: Aug 19, 2008 |
||||
|
* Author: Hedayat Vatankhah <hedayat@grad.com>, 2008. |
||||
|
*/ |
||||
|
|
||||
|
#ifndef KEYA_H_ |
||||
|
#define KEYA_H_ |
||||
|
|
||||
|
#include "keya_types.h" |
||||
|
|
||||
|
/** This can be sent to OpenDevice function to open the next keya device */ |
||||
|
#define KEYA_NEXT_ID(keya) keya.GetKeyAID() + 1 |
||||
|
|
||||
|
class KeyA |
||||
|
{ |
||||
|
public: |
||||
|
KeyA(uint moduleType = _HID_CLASS); |
||||
|
virtual ~KeyA(); |
||||
|
|
||||
|
static uint GetModuleCount(); |
||||
|
static uint EncBlock(const byte *pKey, const byte *pData, byte *pRes); |
||||
|
static uint DecBlock(const byte *pKey, const byte *pData, byte *pRes); |
||||
|
static char *GetErrorText(int lastError, char *perrText, uint buflen); |
||||
|
|
||||
|
uint OpenDevice(int id); |
||||
|
uint CloseDevice(); |
||||
|
int GetKeyAID(); |
||||
|
|
||||
|
uint SetModuleType(uint moduleType); |
||||
|
uint SetKeyAFunctionality(uint keyaFun); |
||||
|
|
||||
|
uint Init(const byte *pKp, const byte *pKr, const byte *pKw, |
||||
|
const byte *pKw2); |
||||
|
uint NetInit(const byte *pKp, const byte *pKr, const byte *pKw, |
||||
|
const byte *pKw2); |
||||
|
uint SetCallBackProc(CallbackProc pAttachCallBack, void *param); |
||||
|
uint SetAttachCallBackProc(PureAttachCallbackProc pAttachCallBack); |
||||
|
uint SetCallBackWithIDProc(CallbackProcWithID callback, |
||||
|
void *param); |
||||
|
void SetCallbackErrorHandlerProc(CallbackErrorHandler handler); |
||||
|
int GetCallbackThreadErrorNumber(); |
||||
|
|
||||
|
uint SetKAB(const byte *pKA, const byte *pKAB); |
||||
|
uint SetKeyAParameters(const byte *pKA, byte *pData); |
||||
|
|
||||
|
uint ChangeKAB(const byte *pKAB, const byte *pNewKAB); |
||||
|
uint WriteAccessKey(const byte *pKBB); |
||||
|
|
||||
|
uint ReadPayamPardazMemory(int address, int len, byte *pBuf); |
||||
|
uint WritePayamPardazMemory(int address, int len, |
||||
|
const byte *pKA, const byte *pData); |
||||
|
|
||||
|
uint ReadFreeMemory(int address, int len, byte *pBuf); |
||||
|
uint WriteFreeMemory(int address, int len, const byte *pBuf); |
||||
|
|
||||
|
uint ReadUserMemory(int address, int len, const byte *pUserKey, |
||||
|
byte *pBuf); |
||||
|
uint WriteUserMemory(int address, int len, const byte *pUserKey, |
||||
|
const byte *pBuf); |
||||
|
uint ReadUserMemory(int address, int len, byte *pBuf); |
||||
|
uint WriteUserMemory(int address, int len, const byte *pBuf); |
||||
|
|
||||
|
uint ReadDeveloperMemory(int address, int len, byte *pBuf); |
||||
|
uint WriteDeveloperMemory(int address, int len, const byte *pBuf); |
||||
|
|
||||
|
uint SetMasterPin(const char *pPin, byte pinLen); |
||||
|
uint SetUserPin(const char *pPin, byte pinLen); |
||||
|
uint UserLogin(const char *pPin, byte pinLen); |
||||
|
uint MasterLogin(const char *pPin, byte pinLen); |
||||
|
//////////////////// New Functions ////////////////// |
||||
|
uint GetAutoLoginStatus(); |
||||
|
uint ResetAutoLogin(); |
||||
|
uint SetAutoLogin(const char *pUserPin, int PinLen, const byte *pAutoLoginKey ); |
||||
|
uint AutoLogin(const byte *pAutoLoginKey ); |
||||
|
uint SetModuleName(const char *strModuleName ); |
||||
|
uint GetModuleName(char *strModuleName, int nModuleNameSize ); |
||||
|
//////////////////////////////////////////////////////////// |
||||
|
uint SetKV(byte iKVIndex, const byte *pKV); |
||||
|
uint EncByKV(byte iKvIndex, const byte *pData, byte *pRes); |
||||
|
uint DecByKV(byte iKvIndex, const byte *pData, byte *pRes); |
||||
|
|
||||
|
uint SetQuery(byte iQueryIndex, const byte *pQueryKey); |
||||
|
uint GetQuery(byte iQueryIndex, const byte *pQueryData, |
||||
|
byte *pQueryResult); |
||||
|
|
||||
|
uint EncByCustomKey(const byte *pCusKey, const byte *pData, |
||||
|
byte *pRes); |
||||
|
uint DecByCustomKey(const byte *pCusKey, const byte *pData, |
||||
|
byte *pRes); |
||||
|
|
||||
|
uint CheckModuleExistence(); |
||||
|
uint GetSerialNumber(byte *pSerialNum); |
||||
|
uint GetMemoryInfo(int *pDevMemLen, int *pSecMemLen, |
||||
|
int *pFMemLen); |
||||
|
uint GetVersionInfo(byte *pVer, byte *pDevMemSup, |
||||
|
byte *pFmemSup, byte *pLoginSup, byte *pCusKeySup); |
||||
|
uint GetKeyCount(int *pQueryKeyNum, int *pKVKeyNum); |
||||
|
|
||||
|
uint SetMaxClient(byte clientNum); |
||||
|
uint GetMaxClient(byte *pClientNum); |
||||
|
|
||||
|
private: |
||||
|
KeyAHID *keya; |
||||
|
|
||||
|
KeyA(KeyA &); |
||||
|
}; |
||||
|
|
||||
|
#endif /* KEYA_H_ */ |
@ -0,0 +1,47 @@ |
|||||
|
/* |
||||
|
* types.h |
||||
|
* |
||||
|
* Created on: Aug 14, 2008 |
||||
|
* Author: Hedayat Vatankhah <hedayat@grad.com>, 2008. |
||||
|
*/ |
||||
|
|
||||
|
#ifndef KEYA_TYPES_H_ |
||||
|
#define KEYA_TYPES_H_ |
||||
|
|
||||
|
#define FALSE 0 |
||||
|
#define TRUE 1 |
||||
|
|
||||
|
#ifndef NULL |
||||
|
#define NULL 0 |
||||
|
#endif |
||||
|
|
||||
|
/* |
||||
|
* Defining some basic types. (NOTICE: data type collision is possible. fix this! |
||||
|
* Maybe using unsigned instead of uint is much better?! |
||||
|
*/ |
||||
|
typedef unsigned char byte; |
||||
|
#ifndef __KERNEL__ |
||||
|
typedef unsigned int uint; |
||||
|
#ifndef __cplusplus |
||||
|
typedef byte bool; |
||||
|
#endif |
||||
|
#endif |
||||
|
|
||||
|
/** This can be sent to OpenDevice function to open the first available KeyA */ |
||||
|
#define KEYA_FIRST_ID 0 |
||||
|
|
||||
|
typedef int (*PureAttachCallbackProc)(void); |
||||
|
typedef int (*CallbackProc)(bool attached, void *param); |
||||
|
typedef int (*CallbackProcWithID)(bool attached, void *param, int keyaid); |
||||
|
typedef void (*CallbackErrorHandler)(int errnum); |
||||
|
|
||||
|
typedef struct KeyA2HIDProtocol KeyAHID; |
||||
|
|
||||
|
/* Only HID class is acceptable by this driver! */ |
||||
|
enum KeyAModuleClass |
||||
|
{ |
||||
|
_CUSTOME_CLASS, |
||||
|
_HID_CLASS |
||||
|
}; |
||||
|
|
||||
|
#endif /* KEYA_TYPES_H_ */ |
@ -0,0 +1,8 @@ |
|||||
|
# Default ignored files |
||||
|
/shelf/ |
||||
|
/workspace.xml |
||||
|
# Editor-based HTTP Client requests |
||||
|
/httpRequests/ |
||||
|
# Datasource local storage ignored files |
||||
|
/dataSources/ |
||||
|
/dataSources.local.xml |
@ -0,0 +1,8 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<module type="CPP_MODULE" version="4"> |
||||
|
<component name="NewModuleRootManager"> |
||||
|
<content url="file://$MODULE_DIR$" /> |
||||
|
<orderEntry type="inheritedJdk" /> |
||||
|
<orderEntry type="sourceFolder" forTests="false" /> |
||||
|
</component> |
||||
|
</module> |
@ -0,0 +1,8 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="ProjectModuleManager"> |
||||
|
<modules> |
||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/lib.iml" filepath="$PROJECT_DIR$/.idea/lib.iml" /> |
||||
|
</modules> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,4 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="VcsDirectoryMappings" defaultProject="true" /> |
||||
|
</project> |
@ -0,0 +1,148 @@ |
|||||
|
#include <fstream>
|
||||
|
#include <vector>
|
||||
|
#include <iostream>
|
||||
|
#include <cstring>
|
||||
|
#include "keya.h"
|
||||
|
|
||||
|
using namespace std; |
||||
|
|
||||
|
// Helper to pad data to 16 bytes
|
||||
|
void pad_data(vector<unsigned char>& data) |
||||
|
{ |
||||
|
size_t pad_len = 16 - (data.size() % 16); |
||||
|
if (pad_len < 16) { |
||||
|
data.insert(data.end(), pad_len, 0); // simple zero padding
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Read file into vector
|
||||
|
vector<unsigned char> read_file(const string& filename) |
||||
|
{ |
||||
|
ifstream file(filename, ios::binary); |
||||
|
return vector<unsigned char>((istreambuf_iterator<char>(file)), istreambuf_iterator<char>()); |
||||
|
} |
||||
|
|
||||
|
// Write vector into file
|
||||
|
void write_file(const string& filename, const vector<unsigned char>& data) |
||||
|
{ |
||||
|
ofstream file(filename, ios::binary); |
||||
|
file.write(reinterpret_cast<const char*>(data.data()), data.size()); |
||||
|
} |
||||
|
|
||||
|
// Encrypt buffer using KeyA
|
||||
|
int encrypt_buffer(vector<unsigned char>& data, unsigned char* kp, unsigned char* kr, unsigned char* kw) |
||||
|
{ |
||||
|
KeyA keya; |
||||
|
int result = keya.OpenDevice(0); |
||||
|
if (result != 0) return result; |
||||
|
|
||||
|
result = keya.Init(kp, kr, kw, NULL); |
||||
|
if (result != 0) { |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
unsigned char temp[16]; |
||||
|
for (size_t i = 0; i < data.size(); i += 16) { |
||||
|
memcpy(temp, &data[i], 16); |
||||
|
result = keya.EncByKV(0, temp, temp); |
||||
|
if (result != 0) { |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
memcpy(&data[i], temp, 16); |
||||
|
} |
||||
|
|
||||
|
keya.CloseDevice(); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
// Decrypt buffer using KeyA
|
||||
|
int decrypt_buffer(vector<unsigned char>& data, unsigned char* kp, unsigned char* kr, unsigned char* kw) |
||||
|
{ |
||||
|
KeyA keya; |
||||
|
int result = keya.OpenDevice(0); |
||||
|
if (result != 0) return result; |
||||
|
|
||||
|
result = keya.Init(kp, kr, kw, NULL); |
||||
|
if (result != 0) { |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
unsigned char temp[16]; |
||||
|
for (size_t i = 0; i < data.size(); i += 16) { |
||||
|
memcpy(temp, &data[i], 16); |
||||
|
result = keya.DecByKV(0, temp, temp); |
||||
|
if (result != 0) { |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
memcpy(&data[i], temp, 16); |
||||
|
} |
||||
|
|
||||
|
keya.CloseDevice(); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
// Save size as first 8 bytes (uint64_t) of encrypted file
|
||||
|
void prepend_size(vector<unsigned char>& data, uint64_t size) |
||||
|
{ |
||||
|
vector<unsigned char> result(8); |
||||
|
for (int i = 0; i < 8; ++i) { |
||||
|
result[i] = (size >> (i * 8)) & 0xFF; |
||||
|
} |
||||
|
result.insert(result.end(), data.begin(), data.end()); |
||||
|
data = result; |
||||
|
} |
||||
|
|
||||
|
// Extract original size from first 8 bytes
|
||||
|
uint64_t extract_size(vector<unsigned char>& data) |
||||
|
{ |
||||
|
uint64_t size = 0; |
||||
|
for (int i = 7; i >= 0; --i) { |
||||
|
size = (size << 8) | data[i]; |
||||
|
} |
||||
|
data.erase(data.begin(), data.begin() + 8); |
||||
|
return size; |
||||
|
} |
||||
|
|
||||
|
int main() |
||||
|
{ |
||||
|
// Your keys
|
||||
|
unsigned char g_Kp[16] = {0x74,0xD8,0xCD,0x0F,0xC6,0x6A,0x09,0x69,0xB1,0x4A,0xC5,0xD1,0xE9,0x7A,0x9D,0x07}; |
||||
|
unsigned char g_Kr[16] = {0xDA,0x58,0x86,0x3C,0xD6,0x65,0xDF,0xE2,0x84,0x04,0xD3,0x4E,0x74,0xEB,0xBD,0x16}; |
||||
|
unsigned char g_Kw[16] = {0xC8,0xA8,0x16,0x3C,0x59,0xEF,0x9C,0xC4,0xC0,0xEC,0x48,0x41,0x4B,0xE5,0x11,0x37}; |
||||
|
|
||||
|
unsigned char* kp = g_Kp; |
||||
|
unsigned char* kr = g_Kr; |
||||
|
unsigned char* kw = g_Kw; |
||||
|
|
||||
|
// Step 1: Encrypt JSON file
|
||||
|
vector<unsigned char> json_data = read_file("orig.txt"); |
||||
|
|
||||
|
uint64_t original_size = json_data.size(); // Save original size
|
||||
|
pad_data(json_data); // pad to multiple of 16
|
||||
|
|
||||
|
if (encrypt_buffer(json_data, kp, kr, kw) == 0) { |
||||
|
prepend_size(json_data, original_size); // Put size in front
|
||||
|
write_file("data.enc", json_data); |
||||
|
cout << "Encryption successful. Encrypted file saved as 'data.enc'." << endl; |
||||
|
} else { |
||||
|
cout << "Encryption failed." << endl; |
||||
|
} |
||||
|
|
||||
|
// Step 2: Decrypt encrypted file
|
||||
|
vector<unsigned char> enc_data = read_file("hashes.enc"); |
||||
|
uint64_t saved_size = extract_size(enc_data); // remove first 8 bytes, get size
|
||||
|
|
||||
|
if (decrypt_buffer(enc_data, kp, kr, kw) == 0) { |
||||
|
enc_data.resize(saved_size); // trim padding
|
||||
|
write_file("hash.txt", enc_data); |
||||
|
cout << "Decryption successful. Decrypted file saved as 'data_decrypted.txt'." << endl; |
||||
|
} else { |
||||
|
cout << "Decryption failed." << endl; |
||||
|
} |
||||
|
|
||||
|
return 0; |
||||
|
} |
@ -0,0 +1,213 @@ |
|||||
|
#include <iostream>
|
||||
|
#include <cstring>
|
||||
|
#include <cstdint>
|
||||
|
#include "keya.h"
|
||||
|
|
||||
|
using namespace std; |
||||
|
|
||||
|
const int kv_idx = 0; // The kv index used for encryption
|
||||
|
const int data_address = 0; // The start address of memory location to place encypted data
|
||||
|
|
||||
|
int produce_data(unsigned char **data, size_t *len) |
||||
|
{ |
||||
|
*len = 20; |
||||
|
*data = new unsigned char[*len]; // Some foo size
|
||||
|
|
||||
|
strcpy((char *) *data, "000000"); |
||||
|
// Place the keya serial number in front of the data.
|
||||
|
// The length of the serial number is 6 bytes length,
|
||||
|
// so the length of the data buffer must be at least 6 bytes.
|
||||
|
KeyA keya; |
||||
|
int result; |
||||
|
|
||||
|
result = keya.OpenDevice(0); |
||||
|
if (result != 0) |
||||
|
return result; |
||||
|
|
||||
|
// The serial number will be placed in first six byte of data
|
||||
|
result = keya.GetSerialNumber(*data); |
||||
|
if (result != 0) |
||||
|
{ |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
keya.CloseDevice(); |
||||
|
// End of placing the keya serial number
|
||||
|
|
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
int write_data(unsigned char *data, size_t len, unsigned char *kp, unsigned char *kr, unsigned char *kw) |
||||
|
{ |
||||
|
unsigned char buffer[16]; |
||||
|
unsigned char *pointer = data; |
||||
|
|
||||
|
KeyA keya; |
||||
|
int result; |
||||
|
int count = 0; |
||||
|
|
||||
|
result = keya.OpenDevice(0); |
||||
|
if (result != 0) |
||||
|
return result; |
||||
|
|
||||
|
// Kw2 is not needed here. Kw2 is only needed for writing to developer memory.
|
||||
|
result = keya.Init(kp, kr, kw, NULL); |
||||
|
if (result != 0) |
||||
|
{ |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
// Data encryption with EncByKV() function is done in 16 bytes blocks,
|
||||
|
// so data must be padded for length that not dividable by 16.
|
||||
|
while (len > 0) |
||||
|
{ |
||||
|
memset(buffer, 0, 16); |
||||
|
if (len < 16) |
||||
|
{ |
||||
|
memcpy(buffer, pointer, len); |
||||
|
len = 0; |
||||
|
pointer += len; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
memcpy(buffer, pointer, 16); |
||||
|
len -= 16; |
||||
|
pointer += 16; |
||||
|
} |
||||
|
result = keya.EncByKV(kv_idx, buffer, buffer); |
||||
|
if (result != 0) |
||||
|
{ |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
result = keya.WriteUserMemory(data_address + count, 16, NULL, buffer); |
||||
|
if (result != 0) |
||||
|
{ |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
count += 16; |
||||
|
} |
||||
|
|
||||
|
keya.CloseDevice(); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
int read_data(unsigned char *data, int len, unsigned char *kp, unsigned char *kr, unsigned char *kw) |
||||
|
{ |
||||
|
unsigned char buffer[16]; |
||||
|
unsigned char *pointer = data; |
||||
|
|
||||
|
KeyA keya; |
||||
|
int result; |
||||
|
int count = 0; |
||||
|
|
||||
|
result = keya.OpenDevice(0); |
||||
|
if (result != 0) |
||||
|
return result; |
||||
|
|
||||
|
// Kw2 is not needed here. Kw2 is only needed for writing to developer memory.
|
||||
|
result = keya.Init(kp, kr, kw, NULL); |
||||
|
if (result != 0) |
||||
|
{ |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
while (len > 0) |
||||
|
{ |
||||
|
result = keya.ReadUserMemory(data_address + count, 16, NULL, buffer); |
||||
|
if (result != 0) |
||||
|
{ |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
result = keya.DecByKV(kv_idx, buffer, buffer); |
||||
|
if (result != 0) |
||||
|
{ |
||||
|
keya.CloseDevice(); |
||||
|
return result; |
||||
|
} |
||||
|
if (len < 16) |
||||
|
{ |
||||
|
memcpy(pointer, buffer, len); |
||||
|
len = 0; |
||||
|
pointer += len; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
memcpy(pointer, buffer, 16); |
||||
|
len -= 16; |
||||
|
pointer += 16; |
||||
|
} |
||||
|
count += 16; |
||||
|
} |
||||
|
|
||||
|
keya.CloseDevice(); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
int main(int argc, char** argv) |
||||
|
{ |
||||
|
unsigned char buffer[16]; |
||||
|
unsigned char *data = new unsigned char[6]; |
||||
|
|
||||
|
KeyA keya; |
||||
|
int result; |
||||
|
int exist = keya.CheckModuleExistence(); |
||||
|
cout<< "moudle exis " << exist << endl ; |
||||
|
char* a = new char[300]; // Allocates memory for 60 characters
|
||||
|
// keya.GetErrorText(keya.CheckModuleExistence(), a ,300);
|
||||
|
// cout<<'\n'<< a <<'\n';
|
||||
|
cout<< "\n Module count: "<< keya.GetModuleCount() <<"\n"; |
||||
|
result = keya.OpenDevice(0); |
||||
|
if (result != 0) |
||||
|
return result; |
||||
|
|
||||
|
cout << "\nopen successfully.\n" << endl; |
||||
|
// The serial number will be placed in first six byte of data
|
||||
|
result = keya.GetSerialNumber(data); |
||||
|
cout << "\nserial:"<< data<<".\n" << endl; |
||||
|
|
||||
|
char* b = new char[300]; |
||||
|
|
||||
|
unsigned char g_Kp[16] = {0x74,0xD8,0xCD,0x0F,0xC6,0x6A,0x09,0x69,0xB1,0x4A,0xC5,0xD1,0xE9,0x7A,0x9D,0x07}; |
||||
|
unsigned char g_Kr[16] = {0xDA,0x58,0x86,0x3C,0xD6,0x65,0xDF,0xE2,0x84,0x04,0xD3,0x4E,0x74,0xEB,0xBD,0x16}; |
||||
|
unsigned char g_Kw[16] = {0xC8,0xA8,0x16,0x3C,0x59,0xEF,0x9C,0xC4,0xC0,0xEC,0x48,0x41,0x4B,0xE5,0x11,0x37}; |
||||
|
unsigned char g_Kw2[16] = {0x53,0x1C,0x74,0x5B,0xEC,0x9B,0x8E,0x76,0xD8,0x8E,0x28,0x79,0xD9,0x19,0x6D,0x0D}; |
||||
|
|
||||
|
unsigned char* kp = g_Kp; |
||||
|
unsigned char* kr = g_Kr; |
||||
|
unsigned char* kw = g_Kw; |
||||
|
unsigned char* kw2 = g_Kw2; |
||||
|
|
||||
|
int err = keya.Init(kp, kr, kw, kw2); |
||||
|
|
||||
|
std::cout << "err : " << err << std::endl; |
||||
|
keya.GetErrorText(err,b,300); |
||||
|
|
||||
|
std::cout << "err : " << b << std::endl; |
||||
|
|
||||
|
unsigned char idx = '0'; |
||||
|
|
||||
|
|
||||
|
unsigned char *stuff = new unsigned char[16]; |
||||
|
stuff = (unsigned char *) "aaaaaaaaaaaaaaaa"; |
||||
|
unsigned char *enc = new unsigned char[16]; |
||||
|
keya.EncByKV(idx,stuff,enc); |
||||
|
cout<<"encoded is:"<< enc << endl; |
||||
|
|
||||
|
unsigned char *dec = new unsigned char[16]; |
||||
|
keya.DecByKV(idx,enc,dec); |
||||
|
|
||||
|
cout<<"decoded is:"<< dec << endl; |
||||
|
|
||||
|
keya.CloseDevice(); |
||||
|
cout << "Finished successfully." << endl; |
||||
|
|
||||
|
delete[] data; |
||||
|
delete[] a; |
||||
|
return 0; |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue