You are on page 1of 11

The programming exam

- Problems with strings (see the functions)


- Problems with matrices
- Un subiect sa scrie o functie
- We can make the flowchart---- if we want
- Write the code in pencil
- Puneti comentarii in cod
-

Agenda cu teorie- o pagina fata—verso pentru fiecare curs; maxim 3 linii de cod pe pagina;
Matrix encryption and
decryption

Date: January 11th 2018


Student: Paul – Vasile Vezeteu
Class: 412 G
Natural language description

1. Encryption program:

Function main
{
print "Enter a sequence to be encrypted"
get sequence from keyboard: ‘seq’
print "Enter a generating code"
get sequence from keyboard: ‘cod’
n = length of ‘seq’
l = length of ‘cod’
initialize function interschimbare(cod,l)
print "Encrypted message: "
initialize function criptare(seq,n)
//new line//
print "Encryption code: "
print ‘cod’
}

2. Decryption program:

Function main
{
do{
//new line//
print "Please introduce the password to proceed decrypting the message: "
while (‘1’ // infinite loop)
ch = read char from keyboard without echo
if ch == ENTER
password[i] ='\0';
break;

else if ch == BKSPC
if i>0
decrement I by 1
printf("\b \b");
else if ch == TAB || ch == SPACE
continue;
else
password[i] = ch;
increment I by 1
printf("*");
if strcmp(password,set) == 0
print " Access granted"
break;
else
print " Access denied"

increment ct by 1

}while (‘ct not equal with three’)

print "Enter a sequence to be dencrypted"


get sequence from keyboard: ‘seq’
print "Enter a generating code"
get sequence from keyboard: ‘cod’
n = length of ‘seq’
l = length of ‘cod’
initialize function interschimbare(cod,l)
initialize function decriptare(seq,n)
}

3. Functions for encryption and decription:


i) Function interschimbare ( char b[10],int dimensiune)

{
declaring mat[6][5] = {
{65,66,67,68,69},
{70,71,72,73,74},
{75,76,77,78,79},
{80,81,82,83,84},
{85,86,87,88,89},
{90}
};

loop “z” from 0 to “dimensiune” (increment by 2)


{
cod1 = b[z]-48;
cod2 = b[z + 1]-48;
loop “i” from 0 to “4”
loop “j” from 0 to “4”
pahar = mat[i][cod1 - 1];
mat[i][cod1-1] = mat[i][cod2 - 1];
mat[i][cod2 - 1] = pahar;
}
}

ii) Function criptare (char text[100],int lungimesir)


{
declaring mat[6][5] = {
{65,66,67,68,69},
{70,71,72,73,74},
{75,76,77,78,79},
{80,81,82,83,84},
{85,86,87,88,89},
{90}
};

loop “m” from 0 to “lungimesir”


loop “i” from 0 to “4”
loop “j” from 0 to “4”
if text[m] == mat[i][j] || text[m] == mat[i][j] + 32
print i and j
}

iii) Function decriptare (char text[100],int lungimesir)


{
declaring mat[6][5] = {
{65,66,67,68,69},
{70,71,72,73,74},
{75,76,77,78,79},
{80,81,82,83,84},
{85,86,87,88,89},
{90}
};
loop “i” from 0 to “lungimesir”
m = text[i];
k = text[i + 1];
print mat[m-48][k-48]
}
The Code

1. The encryption code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "functii.cpp"

int main()
{
int meniu,n,i,cod1,cod2,j,m,conv,l,z;
char pahar,v[300],seq[100],c,cod[10];

puts("Enter a sequence to be encrypted");


gets(seq);

puts("Enter a generating code");


gets(cod);

n = strlen(seq);
l = strlen(cod);

interschimbare(cod,l);

printf("Encrypted message: ");

criptare(seq,n);

printf("\nEncryption code: ");


puts(cod);

2. The decryption code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include "functii.cpp"

#define ENTER 13
#define BKSPC 8
#define TAB 9
#define SPACE 32

int main()
{
char ch,password[10],set[10] = "ROOT",pahar,seq[100],cod[10];
int ct = 0,cod1,cod2,n,i,j,l,z;

do
{
int i=0;
printf("\nPlease introduce the password to proceed decrypting the message: ");

while(1)
{
ch = getch();
if(ch == ENTER)
{
password[i] ='\0';
break;
}
else if(ch == BKSPC)
{
if(i>0)
{
i--;
printf("\b \b");
}
}
else if(ch == TAB || ch == SPACE)
{
continue;
}
else
{
password[i] = ch;
i++;
printf("*");
}
}

if(strcmp(password,set) == 0)
{
puts(" Access granted");
break;
}
else
{
puts(" Access denied");
} //verificare la tastare parola
ct++;

} while(ct != 3);

if(ct == 3)
{
exit(1);
}

fflush(stdin);
puts("Enter a sequence to be decrypted: ");
gets(seq);

puts("Enter a generating code");


gets(cod);

n = strlen(seq);
l = strlen(cod);

interschimbare(cod,l);

decriptare(seq,n);

3. The functions:
A. The first function:

void interschimbare(char b[10],int dimensiune)


{
int z,i,j,cod1,cod2;
char pahar;
char mat[6][5] =
{
{65,66,67,68,69},
{70,71,72,73,74},
{75,76,77,78,79},
{80,81,82,83,84},
{85,86,87,88,89},
{90}
};
for(z = 0; z < dimensiune; z+=2)
{
cod1 = b[z]-48;
//citeste sub forma de string si transform in numar
cod2 = b[z + 1]-48;

for (i = 0; i <= 4; i++)


{
for(j = 0; j <= 4; j++)
{
pahar = mat[i][cod1 - 1];
mat[i][cod1-1] = mat[i][cod2 - 1];
mat[i][cod2 - 1] = pahar;
}
}

B. The encryption function:

int criptare(char text[100],int lungimesir)


{
int m,i,j;
char mat[6][5] =
{
{65,66,67,68,69},
{70,71,72,73,74},
{75,76,77,78,79},
{80,81,82,83,84},
{85,86,87,88,89},
{90}
};

for(m=0; m<=lungimesir; m++)


for(i = 0;i <=4; i++)
for(j = 0;j <= 4;j++)
{
if (text[m] == mat[i][j] || text[m] == mat[i][j] + 32)
{
printf("%d%d",i,j);
}
}
C. The decryption function:

int decriptare(char text[100],int lungimesir)


{
int i;
char m,k;
char mat[6][5] =
{
{65,66,67,68,69},
{70,71,72,73,74},
{75,76,77,78,79},
{80,81,82,83,84},
{85,86,87,88,89},
{90}
};

for(i = 0; i <= lungimesir; i+=2)


{
m = text[i];
k = text[i + 1];

printf("%c",mat[m-48][k-48]);
}

You might also like