I have to write a program to "encrypt and decrypted" some text.
For example if I input: FACHHOCHSCHULE
with a password of: TIGER
it should encrypt to: YIILYHKNWTACRI
which I got working but when I try to reverse the process I get numbers, letters and symbols. Please help.
#include <iostream>
#include <string>
using namespace std;
string encrypt(string,string);
string decrypt(string,string);
void main()
{
cout<<"Code: ";
string code;
cin>>code;
cout<<"Password: ";
string pass;
cin>>pass;
int length=pass.length();
char test=pass.at(0);
string temp=encrypt(code,pass);
cout<<"Encrypted Code: "<<temp<<endl;
string temptwo=decrypt(code,pass);
cout<<"Decrypted Code: "<<temptwo<<endl;
}
string encrypt(string s,string pass)
{
string answer;
int length=pass.length();
int place=0;
for(int i=0;i<s.length();i++)
{
int temp=(s.at(i)-char('A'))+(pass.at(place)-char('A'));
if(temp>= 26)
temp=temp%26;
place++;
temp=temp+char('A');
if(place>=length)
place=0;
answer+=temp;
}
return answer;
}
string decrypt(string s,string pass)
{
string answer;
int length=pass.length();
int place=0;
for(int i=0;i<s.length();i++)
{
int temptwo=(s.at(i)-char('A'))-(pass.at(place)-char('A'));
if(temptwo>= 26)
temptwo=temptwo%26;
place++;
temptwo=temptwo+char('A');
if(place>=length)
place=0;
answer+=temptwo;
}
return answer;
}