Encryption in c++

Encrypt text file using an algorithm

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 9710 Views - Last Post: 13 January 2010 - 10:22 PM Rate Topic: -----

#1 hackr20345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-January 10

Encryption in c++

Posted 12 January 2010 - 03:01 PM

I am trying to encrypt a text file using an algorithm. In my code, the password input is read from a text file, as well as the user name. I am trying to encrypt these text files and prevent them from being read. Is there a free way to do this(because I tried using the Chilkat library, and that costs money after the 30 day trial period)? Part of my code is below.

The program reads the password from the file called pass.txt and then when the password is changed, it puts the new password back into "pass.txt" How can I encrypt this password in the text file?

 else if(tempass != inpass) {
		 cout << "Incorrect password! ";
		 Sleep(2000);
		 system("cls");
		 cout << "Enter current password(try two): "; // gives user another chance.
		 cin >> tempass; // asks for password
		 system("cls");
		 if(tempass == pass) { //if password is correct, allows user to change password.
			 cout << "Utility to change password.\n";
			 cout << "\n";
			 cout << "CHANGE PASS TO: ";
			 cin >> pass;
			 system("cls");
			 cout << "RETYPE NEW PASSWORD FOR VERIFICATION: ";
			 cin >> userretype; 
			 if(pass == userretype) {
				 ofstream Passfile("pass.txt", ios::out);
				 Passfile << pass;
				 Passfile.close();
				 cout << "Password successfully changed. Press a key to return to members area. ";
				 _getch();
				 Members();
			 }


This post has been edited by hackr20345: 12 January 2010 - 03:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Encryption in c++

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Encryption in c++

Posted 12 January 2010 - 03:15 PM

I would use a Cryptographic Hash Function on the password, and then hash the user entered password (with the same hash function of course) and compare it to the one in the file.
Was This Post Helpful? 0
  • +
  • -

#3 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Encryption in c++

Posted 12 January 2010 - 03:25 PM

Well there are various encryption ways, I think you should use google on this one, select an encryption way and if you have any problems understanding it, come back :)
Was This Post Helpful? 0
  • +
  • -

#4 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Encryption in c++

Posted 12 January 2010 - 03:55 PM

check out the RSA Algorithm.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Encryption in c++

Posted 12 January 2010 - 04:33 PM

http://www.openssl.org or http://cryptopp.com/
Was This Post Helpful? 0
  • +
  • -

#6 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 556
  • Joined: 24-July 08

Re: Encryption in c++

Posted 12 January 2010 - 08:38 PM

View PostJackOfAllTrades, on 12 Jan, 2010 - 03:33 PM, said:


You could just create your own encryption method. That is what i like to do, so i know exactly what is happening to my text or data. then, yes, I would also encrypt the user input using the same method. This way, your method doesn't need to be able to work backwards.

If you create your own method, the best way that i have found is to make it dynamic, base your method from the characters that is in the password it's self.

Good luck!
Was This Post Helpful? 0
  • +
  • -

#7 hackr20345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-January 10

Re: Encryption in c++

Posted 13 January 2010 - 05:23 AM

View PostUG Cyber, on 12 Jan, 2010 - 07:38 PM, said:

View PostJackOfAllTrades, on 12 Jan, 2010 - 03:33 PM, said:


You could just create your own encryption method. That is what i like to do, so i know exactly what is happening to my text or data. then, yes, I would also encrypt the user input using the same method. This way, your method doesn't need to be able to work backwards.

If you create your own method, the best way that i have found is to make it dynamic, base your method from the characters that is in the password it's self.

Good luck!


Quote

Well I am very new to c++ programming and programming in general. I know what encryption does, but I do not know how to work an encryption algorithm. Do I make a program that encrypts the text file or put the encryption/decryption in my actual program? And how do I make a hash?

This post has been edited by hackr20345: 13 January 2010 - 05:26 AM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Encryption in c++

Posted 13 January 2010 - 06:43 AM

erik.price has told you exactly what you need to do (as well as a link) and I have provided you with links to libraries that provide the hash functions you need. Assuming you want to keep this as simple as possible:

On initial save
get password from user
hash password with a hash function
save hashed password


On lookup
get password from user
lookup hashed password in file
hash user entered password and compare to one from file

Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Encryption in c++

Posted 13 January 2010 - 12:29 PM

Since it would seem that you are on windows you can also use the windows CryptoAPI -- I am not sure how much you want to trust your security to windows... but for generally purposes using the cryptoAPI is as good as anything else and saves you the trouble of having to write it yourself.
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Encryption in c++

Posted 13 January 2010 - 12:33 PM

Ah yes...Chilkat == Windows, not sure how I missed that.

I agree with Nick...just use the CryptoAPI.
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Encryption in c++

Posted 13 January 2010 - 12:45 PM

View PostDelta_Echo, on 12 Jan, 2010 - 05:55 PM, said:

check out the RSA Algorithm.



Isn't RSA still under copyright? As I undersood it, if you wish to make a product using the RSA algorithm you need to pay for a license...

Never mind, it was released into the public domain in 2000

Actually writing an RSA implementation is not terribly hard and is actually good experience. I wrote one for my number theory class (though it didn't use anything like 128bit keys... more like 8 bit keys but it was fun).

Though for security purposes you should probably use an established API/library rather than roll your own...
Was This Post Helpful? 0
  • +
  • -

#12 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Encryption in c++

Posted 13 January 2010 - 01:14 PM

Use your own algorithm that will be the best,as for example in your case its text so its consist of char which is nothing more then a byte so this byte contains a number for example in ASCII upper case char 'A' is 65 in decimal so if i add some value to it then the char's value will change i.e..(encryption) now after that i will (decrypt) it by subtracting the same value that i added before ,the constant value is called key in the process of encryption data.
A Example
#include "stdio.h"
int main(void)
{
	//Buffer for my password "ROY"
	char ch[4] = {'R','0','Y','\0'};
	int i = 0;

	//Print before encryption
	puts(ch);
	//encryption 
	for(i = 0;i<3;i++)
	{
		//Adding 2 its the key
		ch[i] = ch[i] + 2;
	}
	//Print after encryption
	puts(ch);

	//Decryption and print
	for(i = 0;i<3;i++)
	{
		//Sub... 2 its the key
		ch[i] = ch[i] - 2;
	}
	puts(ch);

	(void)getchar();//Pause on execution
	return 0;
}


And yes you can also try those algorithms pointed above by others any way,
its an example, make something more robust hard to break easily

Good Luck

This post has been edited by debjit625: 13 January 2010 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Encryption in c++

Posted 13 January 2010 - 01:39 PM

Quote

Use your own algorithm that will be the best


NOT if you care about security!!! It is HARD to develop a secure cryptographic system. So if you are interested in security (keeping secrets from anyone other than you kid sister) then you should use a well established algorithm with known crypto-analysis properties. (actually if you kid sister has a aptitude for mathematics than you should also try to use a more established algorithm).

i.e. you should have an idea of how hard it is to break... because if you make up your own then chances are it can be broken easily. For example the algorithm above is trivial to break.
Was This Post Helpful? 0
  • +
  • -

#14 hackr20345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-January 10

Re: Encryption in c++

Posted 13 January 2010 - 06:22 PM

View PostNickDMax, on 13 Jan, 2010 - 12:39 PM, said:

Quote

Use your own algorithm that will be the best


NOT if you care about security!!! It is HARD to develop a secure cryptographic system. So if you are interested in security (keeping secrets from anyone other than you kid sister) then you should use a well established algorithm with not crypto-analysis properties. (actually if you kid sister has a aptitude for mathematics than you should also try to use a more established algorithm).

i.e. you should have an idea of how hard it is to break... because if you make up your own then chances are it can be broken easily. For example the algorithm above is trivial to break.

Quote

Thanks everyone for the help. But how could a person crack your encryption if they do not know the equation or whatever you used to encrypt it? I mean, if the function is a two way one, then they could only use one function to decrypt right? that function being the reverse of the one used to encrypt it..

Was This Post Helpful? 0
  • +
  • -

#15 Jubb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 06-May 09

Re: Encryption in c++

Posted 13 January 2010 - 09:49 PM

Quote

Thanks everyone for the help. But how could a person crack your encryption if they do not know the equation or whatever you used to encrypt it? I mean, if the function is a two way one, then they could only use one function to decrypt right? that function being the reverse of the one used to encrypt it..


No known encryption scheme is uncrackable. Heck, just the other day they proved RSA-768 is insecure. Creating your own encryption algorithm is a fun exercise but can easily be cracked by people with experience so shouldn't be used for anything serious.

This post has been edited by Jubb: 13 January 2010 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2