Jump to content
Sign in to follow this  
Lord Nikon

Simple C++ Encryption Program

Recommended Posts

Well basically what this program does to encrypt a string is add 2 to the ASCII value of each character in a string. It's pretty easy to comprehend if you have done any amateur C++ before :P

// Calc.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <cctype>

using namespace std;
void Encrypt(string&);
string Decrypt(string strTarget);

int main() {
//initialize and get the string from the user
string strTarget;
cout << "Enter a string to encrypt: ";
getline(cin,strTarget);
string temp(strTarget);
Encrypt(strTarget);

cout << "Encrypted: " << strTarget << endl;
cout << "Decrypted: " << Decrypt(strTarget) << endl;

return 0;
}

void Encrypt(string &strTarget)
{
int len = strTarget.length();
char a;
string strFinal(strTarget);

for (int i = 0; i <= (len-1); i++)
{
	a = strTarget.at(i); 
	int b = (int)a; //get the ASCII value of 'a'
	b += 2; //Mulitply the ASCII value by 2
	if (b > 254) { b = 254; }
	a = (char)b; //Set the new ASCII value back into the char
	strFinal.insert(i , 1, a); //Insert the new Character back into the string
}
string strEncrypted(strFinal, 0, len);
strTarget = strEncrypted;
}

string Decrypt(string strTarget)
{
int len = strTarget.length();
char a;
string strFinal(strTarget);

for (int i = 0; i <= (len-1); i++)
{
	a = strTarget.at(i);
	int b = (int)a;
	b -= 2;

	a = (char)b;
	strFinal.insert(i, 1, a);
}
string strDecrypted(strFinal, 0, len);
return strDecrypted;
}

 

Copy+Paste into a C++ source file and run it. (Y)

Share this post


Link to post
Share on other sites
Well basically what this program does to encrypt a string is add 2 to the ASCII value of each character in a string. It's pretty easy to comprehend if you have done any amateur C++ before (Y)

 

Now I am not much a C++ coder, but I know my encryption coding. Adding two values to eery ASCII value seems like a very breakable encryption meathod. Know if you were to create a random linear code value to scramble the ASCII data using an algarythm, that would be a step better. Encryption comes in two varieties, bad and good. Only the top 5% is any good, everything else can be broken in minutes.

Share this post


Link to post
Share on other sites
Now I am not much a C++ coder, but I know my encryption coding. Adding two values to eery ASCII value seems like a very breakable encryption meathod. Know if you were to create a random linear code value to scramble the ASCII data using an algarythm, that would be a step better. Encryption comes in two varieties, bad and good. Only the top 5% is any good, everything else can be broken in minutes.

Hence the thread title

Simple C++ Encryption Program (Y)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...