RSA encryption/decryption method - encryption

i've seen this interesting method to encrypt/decrypt messages with RSA on YouTube and it works, I've tested it : https://www.youtube.com/watch?v=tXXnHXslVhw&t=98s minute 1:45 . I am not that good at math, is there a name for what this guy is doing?
void En() {
crypted= text;
unsigned long long temp=0;
unsigned long long enc = 0;
for (int i = 0; i < text.length() / 2; i++)
{
if (text[i]>='a' && text[i] <= 'z')
{
temp = (text[i] - 96) * 26 + text[i + 1] - 96;
enc = pow(temp, public_key);
enc= enc % N
cout << enc << endl;
enc_v2 = enc;
}
}
cout << "Enc: " << enc << endl;}
void De() {
unsigned long long temp2 = 0;
unsigned long long temp = 0;
char ch=' ', ch2=' ';
for (int i = 0; i < text.length()/2; i++)
{
cout << enc_v2 << private_key;
temp = pow(enc_v2, private_key);
cout << "Temp :" << temp;
temp = temp % N;
cout << "Temp modulo :" << temp;
temp2 = temp;
temp = temp / 26;
cout << " Temp char 1 :"<< temp;
ch = temp + 96;
temp2 = temp2 - temp * 26;
cout << " Temp char 1 :" << temp2;
ch2 = temp2 + 96;
}
cout << "Text: " << ch << ch2;}
P.S. I know about the pow and modular exponentiation , this in only to show what he is doing.
Thank you!

Related

Sending Floats from server to client through socket (UDP)

So I am trying to send floats back and forth between clients and a server. I am able to send floats to the server and the server reads them perfectly. But unfortunately I can't seem to get the server sending the floats back to the other clients. Our current code that doesn't work looks like this.
Send function on server side
int Server::Send(float dt) {
{
char message[BUFLEN];
std::string msg = std::to_string(x2) + "#" + std::to_string(z2);
strcpy(message, (char*)msg.c_str());
if (sendto(server_socket, message, sizeof(message), 0, ptr->ai_addr, ptr->ai_addrlen) == SOCKET_ERROR)
{
std::cout << "Sendto() failed..." << std::endl;
}
else
{
std::cout << "Sent: " << message << "\n" << std::endl;
}
Receiving on Client side
int Client::Recieve(float dt) {
char buf[BUFLEN];
struct sockaddr_in fromAddr;
int fromlen;
fromlen = sizeof(fromAddr);
memset(buf, 0, BUFLEN);
int bytes_recieved = -1;
int sError = -1;
bytes_recieved = recvfrom(client_socket, buf, BUFLEN, 0, (struct sockaddr*) &fromAddr, &fromlen);
sError = WSAGetLastError();
if (sError != WSAEWOULDBLOCK && bytes_recieved > 0)
{
std::cout << "Recieved: " << buf << std::endl;
std::string tmp = buf;
std::size_t pos = tmp.find("#");
tmp = tmp.substr(0, pos - 1);
x2 = std::stof(tmp, NULL);
tmp = buf;
tmp = tmp.substr(pos + 1);
z2 = std::stof(tmp, NULL);
std::cout << "tx: " << x2 << " ty: " << z2 << std::endl;
}
else
std::cout << "Not Receiving" << std::endl;
Any help would be awesome!

Need help figuriing out correct syntax to update objects in global scope

I have this code:
int do_transact(ifstream & inFile, list<shared_ptr<Bike>> bikelist, status s)
{
int id, i = 0, size = bikelist.size();
float days;
string str, str2;
char name[50];
list<shared_ptr<Bike>>::iterator it = bikelist.begin();
if (s == NO_STATUS) //performs rental
{
inFile >> id;
inFile >> days;
inFile >> str;
inFile >> str2;
strcpy_s(name, str.c_str());
while (i < size)
{
if (id == (*it)->id_num) // rents bike
{
cout << "vvvvvv PERFORMING RENTAL vvvvvv" << endl << endl;
cout << "The price of this rental will be: $" << (days)*((*it)->cost_per_day) << endl;
strcpy_s((*it)->to_whom, name);
(*it)->rented_code = RENTED;
cout << "Thank you for your business!" << endl << endl;
return 0;
}
i++;
it++;
}
}
}
I am trying to change to_whom and rented_code in the original list, but it is not updating.
What is the syntax I need in order to change these values the way I need?

Prim's algorithm won't compute

I made a prim's algorithm but whenever i try to use the code it give me the same matrix back. In general it isn't minimizing. Can anyone check the code and let me know why it isn't minimizing my matrix
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <list>
#include <cstdlib>
#include <iomanip>
#include <limits.h>
int minKey(int n,int key[], bool mst[])
{
// Initialize min value
int min = INT_MAX, min_index;
for (int i = 0; i < n; i++)
if (mst[i] == false && key[i] < min)
min = key[i], min_index = i;
return min_index;
}
void print(int n,int **matrix)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++) // print the matrix
{
cout << setw(2) << matrix[i][j] << " ";
}
cout << endl;
}
}
int **gen_random_graph(int n)
{
srand(time(0));
int **adj_matrix = new int*[n];
for(int i = 0; i < n; i++)
{
for (int j = i; j < n; j++) //generating a N x N matrix based on the # of vertex input
{
adj_matrix[i] = new int[n];
}
}
for(int u = 0; u < n; u++)
{
for (int v = u; v < n; v++) //decide whether it has an edge or not
{
bool edgeOrNot = rand() % 2;
adj_matrix[u][v] = adj_matrix[v][u] = edgeOrNot;
cout << u << " " << v << " " << adj_matrix[u][v] << endl;
if(adj_matrix[u][v] == true)
{
adj_matrix[v][u] = true;
if(u == v) //We can't have i = j in an undirected graph
{
adj_matrix[u][v] = -1;
}
cout << u << " " << v << " " << adj_matrix[u][v] << endl;
}
else
{
adj_matrix[v][u] = adj_matrix[u][v] = -1;
cout << u << " " << v << " " << adj_matrix[u][v] << "else" << endl;
}
}
}
for(int i = 0; i < n; i++)
{
for(int j = i; j < n; j++) //create the N x N with edges and sets the weight between the edge randomly
{
if(adj_matrix[i][j] == true)
{
int weight = rand() % 10 + 1;
adj_matrix[i][j] = adj_matrix[j][i] = weight;
cout << " ( " << i << "," << j << " ) " << "weight: " << adj_matrix[i][j] << endl;
}
}
}
print(n,adj_matrix);
return (adj_matrix);
}
void solve_mst_prim_matrix(int n, int **matrix)
{
int parent[n]; // Array to store constructed MST
int key[n]; // Key values used to pick minimum weight edge in cut
bool mstSet[n]; // To represent set of vertices not yet included in MST
// Initialize all keys as INFINITE
for (int i = 0; i < n; i++)
{
key[i] = INT_MAX, mstSet[i] = false;
}
// Always include first 1st vertex in MST.
key[0] = 0; // Make key 0 so that this vertex is picked as first vertex
parent[0] = -1; // First node is always root of MST
// The MST will have n vertices
for (int count = 0; count < n-1; count++)
{
// Pick the minimum key vertex from the set of vertices
// not yet included in MST
int u = minKey(n,key, mstSet);
// Add the picked vertex to the MST Set
mstSet[u] = true;
// Update key value and parent index of the adjacent vertices of
// the picked vertex. Consider only those vertices which are not yet
// included in MST
for (int v = 0; v < n; v++)
// matrix[u][v] is non zero only for adjacent vertices of m
// mstSet[v] is false for vertices not yet included in MST
// Update the key only if matrix[u][v] is smaller than key[v]
if (matrix[u][v] && mstSet[v] == false && matrix[u][v] < key[v])
parent[v] = u, key[v] = matrix[u][v];
}
cout << endl;
print(n,matrix);
}
int main()
{
int N;
cout << "Enter number of vertices" << endl;
cin >> N;
int **matrix = gen_random_graph(N);
solve_mst_prim_matrix(N, matrix);
return 0;
}
Correct me if I'm wrong, but after reading your code, you did not even change any value of **matrix in your solve_mst_prim_matrix function. So it basically prints the same thing..

Pointer Arithmetic in Opencv Using CvMat?

So I'm trying to access matrix elements via a pointer. Here is my code:
CvMat *Q = cvCreateMat(3,3, CV_32F);
for(int i = 0; i < Q->rows ; i++){
float *ptr = (float *)(Q->data.ptr + i * Q->step );
for(int j = 0; j < Q->cols ; j++){
*ptr = 0.0;
if((i ==0)&&(j==0)) *ptr = 1;
if((i ==0)&&(j==1)) *ptr = 2;
if((i ==0)&&(j==2)) *ptr = 3;
if((i ==1)&&(j==0)) *ptr = 4;
if((i ==1)&&(j==1)) *ptr = 5;
if((i ==1)&&(j==2)) *ptr = 6;
if((i ==2)&&(j==0)) *ptr = 7;
if((i ==2)&&(j==1)) *ptr = 8;
if((i ==2)&&(j==2)) *ptr = 9;
//cout << *ptr << endl;
//system("pause");
}
}
cout << CV_MAT_ELEM(*Q,float,0,0) << endl;
cout << CV_MAT_ELEM(*Q,float,0,1) << endl;
cout << CV_MAT_ELEM(*Q,float,0,2) << endl;
cout << CV_MAT_ELEM(*Q,float,1,0) << endl;
cout << CV_MAT_ELEM(*Q,float,1,1) << endl;
cout << CV_MAT_ELEM(*Q,float,1,2) << endl;
cout << CV_MAT_ELEM(*Q,float,2,0) << endl;
cout << CV_MAT_ELEM(*Q,float,2,1) << endl;
cout << CV_MAT_ELEM(*Q,float,2,2) << endl;
system("pause");
I am trying to make the matrix in the for loop as:
[1 2 3
4 5 6
7 8 9],
but when cout'ing them, I get:
3
-4.31602e+008
-4.31602e+008
6
-4.31602e+008
-4.31602e+008
9
-4.31602e+008
-4.31602e+008
Where is the -4.31602e+008 coming from? What am I not understanding here? I'm a little new to pointers.
Check out the API for CvMat (also you might want to consider using Mat if you can use c++).
I'm not totally sure what you are trying to accomplish here, but if you want to access the data using the pointer you are doing it slightly wrong at this point.
float *ptr = (float *)(Q->data.ptr + i * Q->step );
The step here is the number of bytes in a row (so here it would be 12, 4 bytes per element * 3 elements) the pointer will automatically step the correct number of bytes based on the datatype of the pointer when you do arithmetic with it (Good tutorial here). In order to access it like an array you should do it like this:
CvMat *Q = cvCreateMat(3,3, CV_32F);
for(int i = 0; i < Q->rows ; i++){
for(int j = 0; j < Q->cols ; j++){
float *ptr = (float *)(Q->data.ptr) + i*Q->rows + j; //Index is row major
if((i ==0)&&(j==0)) *ptr = 1;
if((i ==0)&&(j==1)) *ptr = 2;
if((i ==0)&&(j==2)) *ptr = 3;
if((i ==1)&&(j==0)) *ptr = 4;
if((i ==1)&&(j==1)) *ptr = 5;
if((i ==1)&&(j==2)) *ptr = 6;
if((i ==2)&&(j==0)) *ptr = 7;
if((i ==2)&&(j==1)) *ptr = 8;
if((i ==2)&&(j==2)) *ptr = 9;
}
}
A much simpler solution would be to use the CV_MAT_ELEM macro that exists.
CvMat *Q = cvCreateMat(3,3, CV_32F);
for(int i = 0; i < Q->rows ; i++){
for(int j = 0; j < Q->cols ; j++){
CV_MAT_ELEM(*Q, float, i, j) = i*Q->rows + j + 1;
}
}
You should increment ptr inside your inner j loop.

OpenCV populating CvMat with data and verifying it

I have a vector of TrainingSets(struct below) called data
class TrainingSet
{
public:
int time;
float input[2];
float output[3*NUM_TRACKING_POINTS];
TrainingSet(int t, float in[2], float out[3*NUM_TRACKING_POINTS])
{
time = t;
for (int i = 0; i < 2; i++)
input[i] = in[i];
for (int i = 0; i < 3*NUM_TRACKING_POINTS; i++)
output[i] = out[i];
}
TrainingSet()
{
}
};
And then I try to take the contents of this Vector, and put them into CvMats for the purpose of training a Neural Network.
int datasize = data.size();
float** in = new float*[datasize];
float** out = new float*[datasize];
for (int i = 0; i < datasize; i++) {
in[i] = new float[2*TIME_STEPS];
out[i] = new float[3*NUM_TRACKING_POINTS];
}
for ( int i = 0 ; i < datasize; i ++)
{
// get the first set in the sequence.
TrainingSet tset = data.front();
data.pop();
// get the inputs
in[i] = new float[2*TIME_STEPS];
in[i][0] = tset.input[0];
in[i][1] = tset.input[1];
// get the outputs
out[i] = new float[3*NUM_TRACKING_POINTS];
for (int j = 0; j < 3*NUM_TRACKING_POINTS; j++)
out[i][j] = tset.output[j];
for (int j = 2; j < 2*TIME_STEPS; j++)
{
if (i == 0)
in[i][j] = 0.0f;
else
in[i][j] = in[i - 1][j - 2];
}
}
// make matrices from data.
CvMat *trainInput = cvCreateMat(datasize, 2*TIME_STEPS, CV_32FC1);
cvInitMatHeader(trainInput, datasize, 2*TIME_STEPS, CV_32FC1, in);
CvMat *trainOutput = cvCreateMat(datasize, 3*NUM_TRACKING_POINTS, CV_32FC1);
cvInitMatHeader(trainOutput, datasize, 3*NUM_TRACKING_POINTS, CV_32FC1, out);
for (int x = 0; x < datasize; x++)
{
cout << "IN: ";
for (int y = 0; y < 2*TIME_STEPS; y++)
cout << cvmGet(trainInput, x, y) << " ";
cout << endl << "IN: ";
for (int y = 0; y < 2*TIME_STEPS; y++)
cout << in[x][y] << " ";
cout << endl << "OUT: ";
for (int y = 0; y < 3 * NUM_TRACKING_POINTS; y++)
cout << cvmGet(trainOutput, x, y) << " ";
cout << endl << "OUT: ";
for (int y = 0; y < 3 * NUM_TRACKING_POINTS; y++)
cout << out[x][y] << " ";
cout << endl << endl;
}
That last forloop is to check to see if the matrices contents are the data I just fed it, but they don't match. The Matrices seem to have completely different data.
Any thoughts on what is going wrong?
Seems to me that in and out are not a contiguous array, but an array of pointers.
I think the cvMat needs a contiguous memory array to be able to operate on it.
Also once you create the array, you don't need to create a CvMat from it, just
use the
CvSetData( header, data ).

Resources