C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,574 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,195 people online right now. Registration is fast and FREE... Join Now!




Simple encyption

 

Simple encyption

j_wolfe

9 Jan, 2009 - 10:34 AM
Post #1

New D.I.C Head
*

Joined: 9 Jan, 2009
Posts: 6


My Contributions
Hello, I am trying to make a simple c++ program that encrypts text given by the user, using a password also given by the user. I apologize for any blatant misuse of the language, as I have not had formal training. My code just outputs the password and a funny looking question mark, rather than encrypting.
CODE

#include <iostream>
#include <sstream>

using namespace std;
//to be encrypted
string textin = "";
//password
string pass = "";
int main()
{
//gather text to be encrypted and password
cout<<"Enter the text to be encrypted:\n";
cin>>textin;
cout<<"Enter the password used to encrypt the text:\n";
cin>>pass;
//try to encrypt
for(int i = 0; i < textin.size(); i++)
    {
    textin[i]=textin[i]+pass[i];
        //fail at adding strings
    }
cout<<textin;
return 0;
}

I know that adding strings is wrong, but I'm not sure what I should be doing instead. It works fine incrementing the text by a set value, but this code fails. Anything you can do, or material you can point me to is greatly appreciated. Thanks.

User is offlineProfile CardPM
+Quote Post


kapax

RE: Simple Encyption

9 Jan, 2009 - 11:04 AM
Post #2

D.I.C Head
**

Joined: 2 Jul, 2008
Posts: 54



Thanked: 3 times
My Contributions
Try this (i added one loop and changed a line inside your loop) and tell if you are satisfied:

CODE

#include <iostream>
#include <sstream>

using namespace std;
//to be encrypted
string textin = "";
//password
string pass = "";
int main()
{
//gather text to be encrypted and password
cout<<"Enter the text to be encrypted:\n";
cin>>textin;
cout<<"Enter the password used to encrypt the text:\n";
cin>>pass;
//try to encrypt

string modifiedPass = "";
modifiedPass.append(pass);
for (int j = 0; j < textin.size(); j++) {
    if (j % pass.length() == 0 || j+1 == textin.length())
        modifiedPass.append(pass);
}

for(int i = 0; i < textin.size(); i++)
    {
    textin[i]=textin[i]+modifiedPass[i];
        //fail at adding strings
    }
cout<<textin;
return 0;
}



Basically, what I did, is creating a new string and making it the length of the input text.
User is offlineProfile CardPM
+Quote Post

matthew180

RE: Simple Encyption

9 Jan, 2009 - 12:17 PM
Post #3

D.I.C Head
Group Icon

Joined: 7 Jan, 2009
Posts: 191



Thanked: 36 times
Dream Kudos: 25
My Contributions
I hope you are using this code only for academic purposes (your encryption is very weak). If this is going to be anything real-world, to protect anything of value (an account, personal information, etc.) then please consider using mcrypt.

Matthew

User is offlineProfile CardPM
+Quote Post

Hyper

RE: Simple Encyption

9 Jan, 2009 - 12:56 PM
Post #4

Banned
*****

Joined: 15 Oct, 2008
Posts: 2,129



Thanked: 97 times
Dream Kudos: 425
My Contributions
You could use Windows 98's method of password encryption: XOR the input! biggrin.gif

EDIT: Because I hate people who never provide at least tiny examples, here:
variable ^= number

This post has been edited by Hyper: 9 Jan, 2009 - 12:57 PM
User is offlineProfile CardPM
+Quote Post

j_wolfe

RE: Simple Encyption

9 Jan, 2009 - 01:36 PM
Post #5

New D.I.C Head
*

Joined: 9 Jan, 2009
Posts: 6


My Contributions
Thank you, kapax, that seems to work. The terminal doesn't seem to like the characters output, but my intent was to save it to a file anyway.

In response to Matthew, this is not for practical use. I just thought it might be kind of fun, that's all. I wasn't aware that such a library existed, but I think by doing it myself I learn more.

That is an interesting concept, Hyper, though I'm not entirely sure how that would work (programming, as you can probably tell, is not yet something I'm good at)

User is offlineProfile CardPM
+Quote Post

KYA

RE: Simple Encyption

9 Jan, 2009 - 01:58 PM
Post #6

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,193



Thanked: 490 times
Dream Kudos: 2825
Expert In: C, C++, Java

My Contributions
Mathematical idea, doing something then undoing it yields the same/original item.
User is offlineProfile CardPM
+Quote Post

Hyper

RE: Simple Encyption

9 Jan, 2009 - 11:08 PM
Post #7

Banned
*****

Joined: 15 Oct, 2008
Posts: 2,129



Thanked: 97 times
Dream Kudos: 425
My Contributions
QUOTE(j_wolfe @ 9 Jan, 2009 - 01:36 PM) *
That is an interesting concept, Hyper, though I'm not entirely sure how that would work (programming, as you can probably tell, is not yet something I'm good at)


It would work something like this (psuedo code):
User enters string of characters into a C-style string: char UserInput[50]
After the user is done entering their string, you do the following:
for (int x = 0; x < 50; x++) { UserInput[x] ^= 50; }

That would cause each letter to be XOR'd with the value of 50. Here's a Wikipedia link to what XOR is:
I lied, it isn't Wikipedia

Welcome! I hope you understand you can simply output the encrypted string to a file once complete.
If this post was helpful, remember to click the green button that says: This Post Was Helpful! to save a kid in China (somehow...)!

This post has been edited by Hyper: 9 Jan, 2009 - 11:08 PM
User is offlineProfile CardPM
+Quote Post

j_wolfe

RE: Simple Encyption

10 Jan, 2009 - 08:41 AM
Post #8

New D.I.C Head
*

Joined: 9 Jan, 2009
Posts: 6


My Contributions
I see now. I just didn't get what you would be XORing.
User is offlineProfile CardPM
+Quote Post

Hyper

RE: Simple Encyption

10 Jan, 2009 - 09:57 AM
Post #9

Banned
*****

Joined: 15 Oct, 2008
Posts: 2,129



Thanked: 97 times
Dream Kudos: 425
My Contributions
QUOTE(j_wolfe @ 9 Jan, 2009 - 10:34 AM) *

Hello, I am trying to make a simple c++ program that encrypts text given by the user, using a password also given by the user. I apologize for any blatant misuse of the language, as I have not had formal training. My code just outputs the password and a funny looking question mark, rather than encrypting.
[code]


I'm just curious, did you ever pass the first grade?
User is offlineProfile CardPM
+Quote Post

j_wolfe

RE: Simple Encyption

10 Jan, 2009 - 10:12 AM
Post #10

New D.I.C Head
*

Joined: 9 Jan, 2009
Posts: 6


My Contributions
I'm sorry that I couldn't take "XOR the input" and extrapolate "XOR the values of the input text and the password". When you said "input", I thought you meant the text to be encrypted. Perhaps if you had said 'inputs', but I'm not sure that would have been proper english.

Maybe things were different when you were in first grade, but I don't remember talking about bitwise operations or encryption techniques.

This post has been edited by j_wolfe: 10 Jan, 2009 - 10:16 AM
User is offlineProfile CardPM
+Quote Post

Hyper

RE: Simple Encyption

10 Jan, 2009 - 10:51 AM
Post #11

Banned
*****

Joined: 15 Oct, 2008
Posts: 2,129



Thanked: 97 times
Dream Kudos: 425
My Contributions
OK. I'll dumb myself down that far then. Here you go idiot:
ENCRYPTION
#include <iostream>
using namespace std;

int main() {

char INPUT[15] = {'\0'};

printf("Enter 14 keys: ");
cin.getline(INPUT, 14, '\n');

for (int x = 0; x < strlen(INPUT); x++) { INPUT[x] ^= 15; }

printf("Encrypted message: %s", INPUT);
_flushall();

cin.get();
return 0;
}


Fucking welcome.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/8/09 08:14AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month