Hey guys, as the title says, this is my first encryption program. Being extremely simple, I was just looking for advice on ways I could improve it. I just recently got back into programming(I tried to learn before but unfortunately lost interest while I was still learning the basics) and would appreciate any advice and/or criticism you could offer. Thanks.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// encrypts clear text string
int encrypt(string x){
int size = x.length();
int ascii[size - 1];
int change = 1;
string hash;
// converts the chars of string x into its ascii value
for (int i = 0; i <= size - 1; i++)
ascii[i] = x[i];
// changing each ascii value by +change(dependent on number of times it loops)
for (int i = 0; i <= size - 1; i++) {
ascii[i]= ascii[i] + change;
change++;
}
// converts the new ascii values into a hashed string
for (int i = 0; i <= size - 1; i++)
hash[i] = ascii[i];
ofstream crypt;
crypt.open("hash.txt",ofstream::app);
cout << "Your hash is:";
// displays hash on screen and saves it to hash.txt
for (int i = 0; i <= size - 1; i++) {
cout << hash[i];
crypt << hash[i];
}
crypt << endl;
crypt.close();
cout << endl;
return 0;
}
// decrypts hashed string
int decrypt(string y){
int ysize = y.length();
int yascii[ysize - 1];
int change = 1;
string clear;
for (int i = 0; i <= ysize - 1; i++)
yascii[i] = y[i];
for (int i = 0; i <= ysize - 1; i++) {
yascii[i] = yascii[i] - change;
change++;
}
for (int i = 0; i <= ysize - 1; i++)
clear[i] = yascii[i];
cout << "Your message is:";
for (int i = 0; i <= ysize - 1; i++)
cout << clear[i];
cout << endl;
return 0;
}
int main(){
string tEncrypt, task, tDecrypt;
do{
cout << "Encrypt:Decrypt" << endl << "::>";
getline(cin, task);
if ((task == "Encrypt") || (task == "encrypt") || (task == "ENCRYPT"))
{
cout << "What do you want to encrypt?" << endl << "::>";
getline(cin, tEncrypt);
encrypt(tEncrypt);
}
else if ((task == "Decrypt") || (task == "decrypt") || (task == "DECRYPT"))
{
cout << "What is your hash?" << endl << "::>";
getline(cin, tDecrypt);
decrypt(tDecrypt);
}
}while(task != "exit");
return 0;
}