simple encryption decrption
i really hate c++ =p, but i am creating this program because of bordom, and to expand knowledge, i would like to know if anyone else can help with it, for some reason, its messed really bad,, or post some of your code for encryption too, anywho, this is really basic, just posting for fun, and it doesnt decrypt at all, but oh well
/*
Simple Encryption and Decryption
*/
#include <iostream.h>
int main()
{
char letter; // initial input
int temp; //random input for crypto
int i=0; // for counter and other counter
int p; // temp var for counting and storing letters in let[]
char let[100]; // storing words when encoded
cout << "Enter Random Number" << endl; //////////////////////
cin >> temp; // Simple input //
cout << "Enter Text To Be Encrypted" << endl; //////////////////////
for(p=0;letter != '5';p++) { // increases P everytime to keep up with letter does not equal 5
cin >> letter; // simple input
if('a' <= letter && letter <= 'z') { // if its not a and z it will quit
letter = (letter - temp); // changes each letter to create an different character per character
cout << letter;
letter = let[p]; // set p to hold all input into a string as the encoded format
}
}
cout << endl;
for(i=0;i!=5;i++) { // loop 5 times to show different combos
for(p=0;let[p] != '\n';p++) { //loop until end of line in array
cout << let[p];
}
}
return 0;
}
Re: simple encryption decrption
Just a thought but using strings would make this code much more efficient, instead of looping until you hit the '\n' character you simply loop until you hit the end of the string. And out of curiosity, why does your encoding loop break when letter is '5'? Your loop will end everytime letter - temp becomes the literal value of 5.. again this would be solved by just looping through characters in a string, all you would have to do is manage each word which can be done a few ways, cin.peek() for example to see if there is a character entered, if there is then just add a space and then the next word. As infosecguru2 already pointed out your let[] array is empty because letter = let[p] should be the other way around. And what is this deal with combos? You're just looping through the same array 5 times and printing it.. Also you should add when encrypting so that the data never goes to negative numbers unless you are using integers or doubles to handle your encrypted data and keep it all in binary form instead of converting it to text because when you put it in a char type you can't print -ive numbers properly..
I rewrote the code with what I thought you were trying to do but I may have missed something, was the basic algorithm to subtract your random number from any lowercase letter? Or was there more to it? Because there's a lot of garbage that didn't need to be there if that was the case.
If my code has an error in it I'm sorry, but my compiler decided to not work (gotta love VC++, something else to fix soon)
Quote:
#include <iostream>
#include <string>
using namespace std;
void main()
{
string letter
int temp; //random input for crypto
int i=0; // for counter and other counter
int p; // temp var for counting and storing letters in let[]
string let; // storing words when encoded
cout << "Enter Random Number" << endl; //////////////////////
cin >> temp; // Simple input //
cout << "Enter Text To Be Encrypted" << endl; //////////////////////
cin >> letter;
while(cin.peek()) {
letter += " ";
cin >> let;
letter += let;
}
let = "";
for(p = 0; p < letter.length(); p++)
{
if('a' <= letter[p] && letter[p] <= 'z') { // if its not a and z it will quit
let += (letter[p] - temp); // changes each letter to create an different character per character
cout << let[p];
}
else { //could be a space or something, you'll lose detail without this
let += letter[p];
cout << letter[p];
}
}
cout << endl;
cout << letter[p]; //you were just printing out your encrypted text without doing anything to it..
}