Project: NeuroCrypto Library Implementation with TPM in C/C++
VerifiedAdded on 2023/06/16
|11
|1850
|78
Project
AI Summary
This C and C++ project details the implementation of a NeuroCrypto library for Tree Parity Machines (TPM). The library includes classes for TPMInputVector and TreeParityMachine, managing input vectors, weights, and TPM computations. The NeuroCrypto class integrates these components for synchronization, utilizing a public key generated after successful synchronization with the Rijndael cipher for encryption and decryption. The code incorporates functions for random vector creation, weight updates, and TPM result computation. The project uses Visual Studio 2010 and the .NET Framework v4, employing AES-128 in CBC mode with PKCS7 padding for encryption and decryption.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.

Running head: Programming in C and C++
Programming in C and C++
Name of the Student
Name of the University
Authors Note:
Programming in C and C++
Name of the Student
Name of the University
Authors Note:
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.

1Programming in C and C++
Discussion
NeuroCrypto Library [neurocrypto.c and neurocrypto.h]
In the coming pages of the report a proper coding has been provided for the NeuroCrypto
library which is used for tree parity machines. This is a special type of multilayer feed forward
network which consist of one output neuron, K hidden neurons and K*N input neurons. TPM
synchronization algorithm generally comprises of input vector X, and after that value of hidden
and output neuron is provided. After that a comparison is done regarding both the values of
TPMs and if the output is found to be same then suitable learning rule is applied to it. After the
completion of synchronization, the public key is used by Rijndael cipher which is generally used
to encrypt and decrypt a given plain text. A proper explanation regarding the various snippet to
codes are used in NeuroCrypto library is given below:
InputVector class structure
class TPMInputVector {
public:
DynamicArray <int> X;
void CreateRandomVector(int K,int N);
void xLength (int K, int N);
};
This class in general updates and updates input vector X which can be easily accessed by
NeuroCrypto class which is generally used for updating the weights which is used during
synchronization.
Discussion
NeuroCrypto Library [neurocrypto.c and neurocrypto.h]
In the coming pages of the report a proper coding has been provided for the NeuroCrypto
library which is used for tree parity machines. This is a special type of multilayer feed forward
network which consist of one output neuron, K hidden neurons and K*N input neurons. TPM
synchronization algorithm generally comprises of input vector X, and after that value of hidden
and output neuron is provided. After that a comparison is done regarding both the values of
TPMs and if the output is found to be same then suitable learning rule is applied to it. After the
completion of synchronization, the public key is used by Rijndael cipher which is generally used
to encrypt and decrypt a given plain text. A proper explanation regarding the various snippet to
codes are used in NeuroCrypto library is given below:
InputVector class structure
class TPMInputVector {
public:
DynamicArray <int> X;
void CreateRandomVector(int K,int N);
void xLength (int K, int N);
};
This class in general updates and updates input vector X which can be easily accessed by
NeuroCrypto class which is generally used for updating the weights which is used during
synchronization.

2Programming in C and C++
The value of X which is used throughout the synchronization is generally used for
randomly assigning the value to a neural network. Two important function of TPMinput vector
are CreateRandomVector and xLength. CreateRandomVector mainly assigns random single bits
which ranges from (-1 to 1) to all the KN number which consist of neurons on the contrary
xlenght is generally used for allocating KN long value of dynamic array X which helps in storing
the input vector.
TreeParityMachine class structure
class TreeParityMachine {
public:
DynamicArray <int> W, H;
int K, N, L;
int TPMOutput;
void Initialize ();
void ComputeTPMResult (const DynamicArray <int> &X);
void UpdateWeight (const DynamicArray <int> &X);
void RandomWeight ();
};
This is considered to be TPM class which generally comprises of weight and hidden
intermediate dynamicArray objects, K, N and L parameter which is for characteristic TPM and a
TPM output variable. Different function which is used to initialization, updating of weight and
output of computation are very defined.
The value of X which is used throughout the synchronization is generally used for
randomly assigning the value to a neural network. Two important function of TPMinput vector
are CreateRandomVector and xLength. CreateRandomVector mainly assigns random single bits
which ranges from (-1 to 1) to all the KN number which consist of neurons on the contrary
xlenght is generally used for allocating KN long value of dynamic array X which helps in storing
the input vector.
TreeParityMachine class structure
class TreeParityMachine {
public:
DynamicArray <int> W, H;
int K, N, L;
int TPMOutput;
void Initialize ();
void ComputeTPMResult (const DynamicArray <int> &X);
void UpdateWeight (const DynamicArray <int> &X);
void RandomWeight ();
};
This is considered to be TPM class which generally comprises of weight and hidden
intermediate dynamicArray objects, K, N and L parameter which is for characteristic TPM and a
TPM output variable. Different function which is used to initialization, updating of weight and
output of computation are very defined.

3Programming in C and C++
NeuroCrypto class structure
class NeuroCrypto {
public:
unsigned int itrMax;
TreeParityMachine A, B;
TPMInputVector objInput;
char publickey[100];
//Default Constructor
NeuroCrypto (int k, int n, int l);
};
This is considered as the superset class of NeuroCrypto program module which generally
comprises of two TreeParityMachine objects A and B which is used for Alice and Bob's TPM, a
TPMInput vector object for objInput, a character array which is used for storing final public key.
The default constructor which is present at the TPM parameter K, N and L is generally inputted
by user at the time of runtime.
Declaration of variable and its initialization
int i, ii, ss, itrMax, j, K=0,sum, key_size, key_length, initN, initL;
TreeParityMachine A, B;
TPMInputVector objInput;
DynamicArray <char> publickey;
NeuroCrypto class structure
class NeuroCrypto {
public:
unsigned int itrMax;
TreeParityMachine A, B;
TPMInputVector objInput;
char publickey[100];
//Default Constructor
NeuroCrypto (int k, int n, int l);
};
This is considered as the superset class of NeuroCrypto program module which generally
comprises of two TreeParityMachine objects A and B which is used for Alice and Bob's TPM, a
TPMInput vector object for objInput, a character array which is used for storing final public key.
The default constructor which is present at the TPM parameter K, N and L is generally inputted
by user at the time of runtime.
Declaration of variable and its initialization
int i, ii, ss, itrMax, j, K=0,sum, key_size, key_length, initN, initL;
TreeParityMachine A, B;
TPMInputVector objInput;
DynamicArray <char> publickey;
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.

4Programming in C and C++
const char Dictionary [38] = "01234567890_abcdefghijklmnopqrstuvwxyz";
srand (time(NULL)); //random generator
cout << "Parameter settings (K, N, L)";
cin >> initK >> initN >> initL;
A.K=initK, A.N=initN, A.L=initL;//InitA
A.Initialize (); A.RandomWeight ();
B.K=initK, B.N=initN, B.L=initL;//InitB
B.Initialize (); B.RandomWeight ();
itrMax=(A.L*A.L*A.L*A.L)*A.N*A.K; /////
cout << "Maximum Iterations: "<<itrMax;
objInput.xLength (B.K, B.N);
cout<<"Synchronizing TPM Networks...";
A and B are generally considered to be instance of TreeParityMachine class. Publickey
object of Dynamic Array class generally stores the final public key after its successful
synchronization. Dictionary array in general stores about 38 symbols which consist of 10
numbers, 26 alphabets with an underscore which is used as a seed for generation of random
const char Dictionary [38] = "01234567890_abcdefghijklmnopqrstuvwxyz";
srand (time(NULL)); //random generator
cout << "Parameter settings (K, N, L)";
cin >> initK >> initN >> initL;
A.K=initK, A.N=initN, A.L=initL;//InitA
A.Initialize (); A.RandomWeight ();
B.K=initK, B.N=initN, B.L=initL;//InitB
B.Initialize (); B.RandomWeight ();
itrMax=(A.L*A.L*A.L*A.L)*A.N*A.K; /////
cout << "Maximum Iterations: "<<itrMax;
objInput.xLength (B.K, B.N);
cout<<"Synchronizing TPM Networks...";
A and B are generally considered to be instance of TreeParityMachine class. Publickey
object of Dynamic Array class generally stores the final public key after its successful
synchronization. Dictionary array in general stores about 38 symbols which consist of 10
numbers, 26 alphabets with an underscore which is used as a seed for generation of random

5Programming in C and C++
numbers. This program takes the parameter K, N and L from various user and initializes the
TPMs which has some of the common parameters.
Main Iteration
for (i=1; i!=itrMax; i++) {
objInput.CreateRandomVector(B.K, B.N);
A.ComputeTPMResult(objInput.X);
B.ComputeTPMResult(objInput.X);
if(A.TPMOutput == B.TPMOutput) {
A.UpdateWeight (objInput.X);
B.UpdateWeight (objInput.X);
sum = 0;
for(ss=0;ss<A.K*A.N;ss++) //Find sum
sum += abs(A.W.Z[ss]- B.W.Z[ss]);
if ( sum == 0 ) break;
}
}
if (sum == 0)
cout << "Status: SUCCESS!";
else cout << "Status: FAILED!";
numbers. This program takes the parameter K, N and L from various user and initializes the
TPMs which has some of the common parameters.
Main Iteration
for (i=1; i!=itrMax; i++) {
objInput.CreateRandomVector(B.K, B.N);
A.ComputeTPMResult(objInput.X);
B.ComputeTPMResult(objInput.X);
if(A.TPMOutput == B.TPMOutput) {
A.UpdateWeight (objInput.X);
B.UpdateWeight (objInput.X);
sum = 0;
for(ss=0;ss<A.K*A.N;ss++) //Find sum
sum += abs(A.W.Z[ss]- B.W.Z[ss]);
if ( sum == 0 ) break;
}
}
if (sum == 0)
cout << "Status: SUCCESS!";
else cout << "Status: FAILED!";

6Programming in C and C++
For each of the iteration of Alice’s TPM, a random of input vector which will produce by
making use of CreateRandomVector function of TPMInputVector class, the output which is
achieved will be computed by using ComputeTPMResult function and the value of output which
is available to Bob's TPM B. Bob’s TPM should make use of same iterative procedure. At the
time of synchronization both the parties that is A and B will look or check for both the values of
their TPM output values.
Key Generation and Synchronization Outputs
cout << "Iterations:" << i << "DataExchanged:" << (i*(A.K*A.N+4)/1024) << " KiB";
key_size = 37 / (A.L * 2 + 1);
key_length = A.K * A.N / key_size;
cout << "Key length: " << key_length;
publickey.length(key_length + 1);
for(i = 0; i < key_length; i++)
publickey.Z[i] = 0;
for (i=1; i < key_length+1; i++) {
K = 1;
for(j=(i-1)*key_size; j<i*key_size;j++)
For each of the iteration of Alice’s TPM, a random of input vector which will produce by
making use of CreateRandomVector function of TPMInputVector class, the output which is
achieved will be computed by using ComputeTPMResult function and the value of output which
is available to Bob's TPM B. Bob’s TPM should make use of same iterative procedure. At the
time of synchronization both the parties that is A and B will look or check for both the values of
their TPM output values.
Key Generation and Synchronization Outputs
cout << "Iterations:" << i << "DataExchanged:" << (i*(A.K*A.N+4)/1024) << " KiB";
key_size = 37 / (A.L * 2 + 1);
key_length = A.K * A.N / key_size;
cout << "Key length: " << key_length;
publickey.length(key_length + 1);
for(i = 0; i < key_length; i++)
publickey.Z[i] = 0;
for (i=1; i < key_length+1; i++) {
K = 1;
for(j=(i-1)*key_size; j<i*key_size;j++)
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

7Programming in C and C++
K = K + A.W.Z[j] + A.L;
//
publickey.Z[i-1]=Dictionary[K];
}
publickey.Z[key_length]='\0'; //Null char
cout << "Public Key: " << publickey.Z;
The iterations are mainly required for accomplishing TPMs for matching the weights
which are generally stored in i. For each iteration in the loop the data is generally transferred is
equal to K*N+4 bytes. So the total synchronization would be i * (K * N + 4) / 1024 kilobytes. The
value of public key is calculated as the total number of symbols which is generally assigned in
the dictionary and the depth is generally depends in the range from –L to L. This will ultimately
explain the formula L * 2 + 1 (which is generally inclusive of origin). In case of public key
generation which is of key length generation the ith character of public key will be assigned as
the kth symbol in the dictionary. This thing will be achieved in such a way that the location k
depends on the jth neutral weight and this again depends on the ith position of the publickey
array. The value of nested loop is again done by nested loop which is will produce a random
public key which totally depends on the weights, dictionary size and parameter of TPM (it totally
depends on the size of key).
Rijndael Cipher [frmMain.h]
Rijndael Cipher was adopted or implemented using System::Security::Cryptography
library in VS2010. AES-128 was used in CBC mode and PKCS7 padding for encryption and
decryption of the plain text.
K = K + A.W.Z[j] + A.L;
//
publickey.Z[i-1]=Dictionary[K];
}
publickey.Z[key_length]='\0'; //Null char
cout << "Public Key: " << publickey.Z;
The iterations are mainly required for accomplishing TPMs for matching the weights
which are generally stored in i. For each iteration in the loop the data is generally transferred is
equal to K*N+4 bytes. So the total synchronization would be i * (K * N + 4) / 1024 kilobytes. The
value of public key is calculated as the total number of symbols which is generally assigned in
the dictionary and the depth is generally depends in the range from –L to L. This will ultimately
explain the formula L * 2 + 1 (which is generally inclusive of origin). In case of public key
generation which is of key length generation the ith character of public key will be assigned as
the kth symbol in the dictionary. This thing will be achieved in such a way that the location k
depends on the jth neutral weight and this again depends on the ith position of the publickey
array. The value of nested loop is again done by nested loop which is will produce a random
public key which totally depends on the weights, dictionary size and parameter of TPM (it totally
depends on the size of key).
Rijndael Cipher [frmMain.h]
Rijndael Cipher was adopted or implemented using System::Security::Cryptography
library in VS2010. AES-128 was used in CBC mode and PKCS7 padding for encryption and
decryption of the plain text.

8Programming in C and C++
Encryption
cipherData = text_Plain->Text;
plainbytes = Encoding::ASCII->GetBytes(cipherData);
// generating symmetric key
plainKey = Encoding::ASCII->GetBytes(this->text_Key->Text);
desObj->Key = plainKey;
// choose other appropriate modes (CBC, CFB, CTS, ECB, OFB)
desObj->Mode = CipherMode::CBC;
// setting the padding mode
desObj->Padding = PaddingMode::PKCS7;
// --------------- ECRYPTION ---------------
// memory stream used as a target to write enrypted data
MemoryStream^ ms = gcnew MemoryStream();
Encryption
cipherData = text_Plain->Text;
plainbytes = Encoding::ASCII->GetBytes(cipherData);
// generating symmetric key
plainKey = Encoding::ASCII->GetBytes(this->text_Key->Text);
desObj->Key = plainKey;
// choose other appropriate modes (CBC, CFB, CTS, ECB, OFB)
desObj->Mode = CipherMode::CBC;
// setting the padding mode
desObj->Padding = PaddingMode::PKCS7;
// --------------- ECRYPTION ---------------
// memory stream used as a target to write enrypted data
MemoryStream^ ms = gcnew MemoryStream();

9Programming in C and C++
// transforms and encrypts plaintext data to memorystream object
CryptoStream^ cs = gcnew CryptoStream(ms, desObj->CreateEncryptor(),
CryptoStreamMode::Write);
cs->Write(plainbytes, 0, plainbytes->Length);
cs->Close();
// getting encrypted data from memorystream to bytes
cipherbytes = ms->ToArray();
ms->Close();
text_Encrypt->Text = Encoding::ASCII->GetString(cipherbytes);
Decryption
// --------------- DECRYPTION ---------------
MemoryStream^ ms1 = gcnew MemoryStream(cipherbytes);
CryptoStream^ cs1 = gcnew CryptoStream(ms1, desObj->CreateDecryptor(),
CryptoStreamMode::Read);
// decrypt the ciphertext from previous section
cs1->Read(cipherbytes, 0, cipherbytes->Length);
plainbytes2 = ms1->ToArray();
cs1->Close();
// transforms and encrypts plaintext data to memorystream object
CryptoStream^ cs = gcnew CryptoStream(ms, desObj->CreateEncryptor(),
CryptoStreamMode::Write);
cs->Write(plainbytes, 0, plainbytes->Length);
cs->Close();
// getting encrypted data from memorystream to bytes
cipherbytes = ms->ToArray();
ms->Close();
text_Encrypt->Text = Encoding::ASCII->GetString(cipherbytes);
Decryption
// --------------- DECRYPTION ---------------
MemoryStream^ ms1 = gcnew MemoryStream(cipherbytes);
CryptoStream^ cs1 = gcnew CryptoStream(ms1, desObj->CreateDecryptor(),
CryptoStreamMode::Read);
// decrypt the ciphertext from previous section
cs1->Read(cipherbytes, 0, cipherbytes->Length);
plainbytes2 = ms1->ToArray();
cs1->Close();
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.

10Programming in C and C++
ms1->Close();
text_Decrypt->Text = Encoding::ASCII->GetString(plainbytes2);
Software Used
Microsoft Visual Studio 2010 Ultimate (Visual C++)
Rijndael Cipher was implemented using System::Security::Cryptography library in
VS2010.
.NET Framework v4 is required to execute the binary file that is available in the
Downloads section.
ms1->Close();
text_Decrypt->Text = Encoding::ASCII->GetString(plainbytes2);
Software Used
Microsoft Visual Studio 2010 Ultimate (Visual C++)
Rijndael Cipher was implemented using System::Security::Cryptography library in
VS2010.
.NET Framework v4 is required to execute the binary file that is available in the
Downloads section.
1 out of 11

Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
© 2024 | Zucol Services PVT LTD | All rights reserved.