| | |
Encryption
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
Having some problems creating an encoder for class. I have the decoder, but I can't seem to make my encoder work properly.
I have attached the decoder to the post as a file.
Encryption example: "Hello" would be "xxxxx!H#xxxxxxx#e¤xxx¤l&xxxxxxxxx&l$xxxxxxxxxxx$o", where x is randomly generated letters, and ! is the first key. After the first key comes the first character in the message, and then the next key.
What I am having problems with is simply what to use... I tried using only char[] and loops, but was unable to separate the independent characters in the message when introducing them to the encoded string. I got H, He, Hel, Hell, Hello in the encoded string instead of the single characters. Perhaps I have to use arrays?
For creating new keys, I used
However, I was unable to integrate them into the encryption due to type differance. I managed to go from char to char[], but I got the ASCII number instead of the letter.
Meh. Any suggestions?
I have attached the decoder to the post as a file.
Encryption example: "Hello" would be "xxxxx!H#xxxxxxx#e¤xxx¤l&xxxxxxxxx&l$xxxxxxxxxxx$o", where x is randomly generated letters, and ! is the first key. After the first key comes the first character in the message, and then the next key.
What I am having problems with is simply what to use... I tried using only char[] and loops, but was unable to separate the independent characters in the message when introducing them to the encoded string. I got H, He, Hel, Hell, Hello in the encoded string instead of the single characters. Perhaps I have to use arrays?
For creating new keys, I used
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> using namespace std; int main() { srand((unsigned) time(NULL)); char c = (char) (rand() % 26 + 'a'); cout << char(c) << endl; cout << int(c) << endl; }
However, I was unable to integrate them into the encryption due to type differance. I managed to go from char to char[], but I got the ASCII number instead of the letter.
Meh. Any suggestions?
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
1
#2 Oct 18th, 2009
I can't analyze the code you attached since it's in a language I don't know. But here's an example of a very simple encoding technique that simply pads to "encrypt" (not really encryption), and takes out the pads to "decrypt". It uses a character array and isolates characters using the [] operator.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> #include <ctime> #include <cstring> using namespace std; void encrypt (char* plaintext, char* encrypted) { // stick in five random characters for each real character. int length = strlen (plaintext); for (int i = 0; i < length; i++) { encrypted[i * 6] = plaintext[i]; for (int j = 1; j < 6; j++) { char randomLetter = (rand () % 26) + 'a'; encrypted[i * 6 + j] = randomLetter; } } encrypted[length * 6] = 0; // null terminator } void decrypt (char* plaintext, char* encrypted) { // remove random characters int length = strlen (encrypted); for (int i = 0; i < length; i+=6) { plaintext[i/6] = encrypted[i]; } plaintext[length / 6] = 0; // null terminator } int main () { srand (time (NULL)); char plaintext[11]; char encrypted[61]; cout << "Enter plaintext (length <= 10 characters) : "; cin >> plaintext; encrypt (plaintext, encrypted); cout << "Encrypted : " << encrypted << endl; decrypt (plaintext, encrypted); cout << "Decrypted : " << plaintext << endl; return 0; }
Last edited by VernonDozier; Oct 18th, 2009 at 5:31 pm.
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
Thanks for the reply. I did not think of using operators inside the brackets, so this helped alot. Now I just have to figure out some way to add a key in front of every character which is part of the message, using the method described in my first post. I would have to add something like this:
Message: SECRET
Output: goakaSogajkoErasjkarCiagkalaiRkagakEqagklqTakgal
Using caps for viewing convenience in this example. The letter in front of the first part of the message, 'S', is a. This is the first key. The letter just after 'S', is 'o'. This is the next key. The next 'o' in the string is followed by the next part of the message, 'E', which is in turn followed by the next key, 'r'. The next 'r' is followed by the next part of the message, 'C', and so on.
I guess I could just use the position of message characters +/- 1 to place these keys, but they would have to follow the pattern AB BC CD DE etc...
Now then, how would I go about implementing this pattern...
Message: SECRET
Output: goakaSogajkoErasjkarCiagkalaiRkagakEqagklqTakgal
Using caps for viewing convenience in this example. The letter in front of the first part of the message, 'S', is a. This is the first key. The letter just after 'S', is 'o'. This is the next key. The next 'o' in the string is followed by the next part of the message, 'E', which is in turn followed by the next key, 'r'. The next 'r' is followed by the next part of the message, 'C', and so on.
I guess I could just use the position of message characters +/- 1 to place these keys, but they would have to follow the pattern AB BC CD DE etc...
Now then, how would I go about implementing this pattern...
•
•
Join Date: Jul 2005
Posts: 1,677
Reputation:
Solved Threads: 262
0
#4 Oct 18th, 2009
If you know the first key then maybe something like this will work.
Encrypt:
while there are letters to encrypt
generate random letters until current key is generated
place a letter of message
generate random letter to act as next key
repeat
Decrypt---need to know first key
while no further characters in message
find current key
extract a letter of message
find next key
repeat
Encrypt:
while there are letters to encrypt
generate random letters until current key is generated
place a letter of message
generate random letter to act as next key
repeat
Decrypt---need to know first key
while no further characters in message
find current key
extract a letter of message
find next key
repeat
Klatu Barada Nikto
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
Think I have it now:
Confirmed working by my decoder app
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> #include <string> using namespace std; void main() { char spam = 'k'; char key = 'r'; string melding; bool end = false; unsigned int seed = time(0); srand(seed); int i = 0; getline(cin, melding); while(spam != key && end != true) { spam = 65 + rand() % 63; cout << spam; if(spam == key) { cout << melding[i]; key = 65 + rand() % 63; cout << key; i++; } if(melding[i] == '\0') { end = true; } } cout << endl << endl; }
Confirmed working by my decoder app
![]() |
Similar Threads
- String Encryption Challenge Help (C++)
- News Story: Thunder Tables Kill Microsoft 40-bit Encryption (Network Security)
- News Story: States Begin Requiring Encryption of Personal Data (Network Security)
- News Story: Blackmail virus returns with an 'uncrackable' 1024-bit encryption key (Network Security)
- How to create SHA1 Encryption program (VB.NET)
- News Story: Exclusive: new PGP encryption upgrades for enterprise users (Network Security)
- c++ windows program to demonstrate encryption (C++)
- No Encryption in IE 6.0/XP Home (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: (Matrix Multiplication with Multi-threading)
- Next Thread: Beginner wonders about C++ interface apps
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






