You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

213 lines
5.4 KiB

#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;
}