473,503 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

encryption and decryption of text files

180 New Member
Hi guys,
I have some text files. I need to encrypt the files and then decrypt it again. This is my first experience in encryption and decryption. I want some suggestions about how this can be achieved in best way. I need it for my project.

The encryption and decryption solution should be perfect such that nobody others should be able to break the key unless he/she know it( security measures)

thanks in advance
Feb 1 '07 #1
20 16583
hirak1984
316 Contributor
Your question is not clear.

If you want to know only how to encrypt a file,the easiest way is to AND/OR each character present with a special key.
Then only performing reverse operation on the chars with that key can decrypt the file.
But if you want to encrypt file to make it verrrrrrrrry secure,go through some documents regarding encryption,there you will find advanced keys,and algorithms.

Hi guys,
I have some text files. I need to encrypt the files and then decrypt it again. This is my first experience in encryption and decryption. I want some suggestions about how this can be achieved in best way. I need it for my project.

The encryption and decryption solution should be perfect such that nobody others should be able to break the key unless he/she know it( security measures)

thanks in advance
Feb 1 '07 #2
Ganon11
3,652 Recognized Expert Specialist
Out of curiosity, what type of data is in the file to be encrypted?
Feb 1 '07 #3
hirak1984
316 Contributor
Ya I should have asked that also,
my suggestion will work for only text or doc files.
Out of curiosity, what type of data is in the file to be encrypted?
Feb 1 '07 #4
vermarajeev
180 New Member
Hmmm! Interesting,
Thanks for your replies....

The file contains only human understable characters... And yes I want the encryption and decryption to be very secure such that nobody should be able to hack my files.


Thanks
Feb 1 '07 #5
hirak1984
316 Contributor
Just consuder the ascii values of the chars and do some mathematical operations.It will serve ur purpose



Hmmm! Interesting,
Thanks for your replies....

The file contains only human understable characters... And yes I want the encryption and decryption to be very secure such that nobody should be able to hack my files.


Thanks
Feb 1 '07 #6
Ganon11
3,652 Recognized Expert Specialist
The most basic encryption method I could think of would be to take each character and add a certain value to it - 37, or 60, or some random number. Make sure to cast the result back to a char value, so that the encrypted text will look like a bunch of garbage text.

During decryption, take each character and subtract the same value from it, resulting in the original character.

Unfortunately, this is a very simple encryption method, and anyone who is able to study your encrypted file long enough or catch a glimpse of the code will be able to figure it out.

Another possibility would be to pick a key word, such as "house". Get the first character of the file to be encrypted, and add to it the integer value of 'h' - the first letter in house. Write the new character to the coded file. Next, get the second character from the input file, add add to it the value of 'o' - the second letter in house. Repeat this process until the 'e' is house, then loop back to the h and repeat. This will be a slightly more hidden process, but just as easily reversable.
Feb 1 '07 #7
vermarajeev
180 New Member
The most basic encryption method I could think of would be to take each character and add a certain value to it - 37, or 60, or some random number. Make sure to cast the result back to a char value, so that the encrypted text will look like a bunch of garbage text.

During decryption, take each character and subtract the same value from it, resulting in the original character.

Unfortunately, this is a very simple encryption method, and anyone who is able to study your encrypted file long enough or catch a glimpse of the code will be able to figure it out.

Another possibility would be to pick a key word, such as "house". Get the first character of the file to be encrypted, and add to it the integer value of 'h' - the first letter in house. Write the new character to the coded file. Next, get the second character from the input file, add add to it the value of 'o' - the second letter in house. Repeat this process until the 'e' is house, then loop back to the h and repeat. This will be a slightly more hidden process, but just as easily reversable.
Hi thanks for your reply....It sounds interesting. I have done a sample program.Please let me know if you meant the same

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. void encryptFile( ifstream& ifs, ofstream& ofs, string encryptKey )
  6. {
  7.     char line[256];    
  8.     while( !ifs.eof() )
  9.     {
  10.         ifs.getline( line, sizeof(line) );        
  11.  
  12.         string strLine = line;
  13.         unsigned int j=0;
  14.         for(unsigned int i=0; i<strLine.length(); ++i)
  15.         {    
  16.             strLine[i] += encryptKey[j++];            
  17.             ofs.put( strLine[i] );
  18.             if( j >= encryptKey.length() )
  19.                 j = 0;
  20.         }
  21.         cout<<"Encrypted data"<<endl;
  22.         cout<<strLine.data()<<endl;
  23.     }
  24. }
  25.  
  26. void decryptFile( ifstream& ifs, string encryptKey )
  27. {
  28.     char line[256];    
  29.     while( !ifs.eof() )
  30.     {
  31.         ifs.getline( line, sizeof(line) );
  32.         string strLine = line;
  33.         unsigned j=0;
  34.         for(unsigned i=0; i<strLine.length(); ++i)
  35.         {    
  36.             strLine[i] -= encryptKey[j++];            
  37.             if( j >= encryptKey.length() )
  38.                 j = 0;
  39.         }
  40.         cout<<"Decrypted data"<<endl;
  41.         cout<<strLine.data()<<endl;
  42.     }
  43. }
  44.  
  45. int main()
  46. {
  47.     ifstream ifs("test.txt");
  48.     if( ifs.fail() )
  49.         return 0;
  50.  
  51.     ofstream ofs("coded.txt");
  52.     if( ofs.fail() )
  53.         return 0;
  54.  
  55.     string encryptKey = "house";    
  56.     encryptFile( ifs, ofs, encryptKey );
  57.  
  58.     ofs.close();
  59.     ifs.close();
  60.  
  61.     ifstream ifstm("coded.txt");
  62.     if( !ifstm )
  63.         return 0;
  64.  
  65.     decryptFile( ifstm, encryptKey );    
  66.  
  67.     ifstm.close();
  68.  
  69.     return 0;
  70. }
Feb 2 '07 #8
nmadct
83 Recognized Expert New Member
It's not clear whether you just want results -- to keep your data safe -- or you want to actually implement encryption yourself. Encryption is tricky to do and, being a beginner, what you write will not be very secure until you've gotten to the point where you know what you're doing. If your goal is simply to add security into an app that you're writing, I recommend that you go download one of the various free encryption libraries that are available online and use the functions from that library to encrypt your data. If you want to learn about cryptography, more power to you, I recommend you go read a book on it first.
Feb 2 '07 #9
vermarajeev
180 New Member
It's not clear whether you just want results -- to keep your data safe -- or you want to actually implement encryption yourself. Encryption is tricky to do and, being a beginner, what you write will not be very secure until you've gotten to the point where you know what you're doing. If your goal is simply to add security into an app that you're writing, I recommend that you go download one of the various free encryption libraries that are available online and use the functions from that library to encrypt your data. If you want to learn about cryptography, more power to you, I recommend you go read a book on it first.

Yes i want to actually implement encryption myself.

"Encryption is tricky to do"
"I recommend you go read a book on it first"
--->Where can I find the best resources??????


"I recommend that you go download one of the various free encryption libraries that are available online and use the functions from that library to encrypt your data."
--->Please suggest some links such that I can get how it is implemented

Is there any problem with the solution that I have written?????

Please let me know about my queries.
Thanks
Feb 2 '07 #10
vermarajeev
180 New Member
Yes i want to actually implement encryption myself.

"Encryption is tricky to do"
"I recommend you go read a book on it first"
--->Where can I find the best resources??????


"I recommend that you go download one of the various free encryption libraries that are available online and use the functions from that library to encrypt your data."
--->Please suggest some links such that I can get how it is implemented

Is there any problem with the solution that I have written?????

Please let me know about my queries.
Thanks

I also want the file to be password protected.
The explanation is as follows:
Present scenario
--------------------------------
I have three models in my project 1) MODEL1 2) MODEL2 3) MODEL3

Suppose
MODEL1 creates a text file called model1.txt with human understandable characters

This model1.txt is given to MODEL2 which performs operation using that model1.txt, then generates model2.txt( with different contents different from model1.txt)

model2.txt is given to MODEL3 to view the results.

To be done scenario
--------------------------------
MODEL1 creates a text file called model1.txt with human understandable characters which should be encrypted with password protected

This encrypted file model1.txt is given to MODEL2 who knows the password, enters the password known and then decrypts the information in that file(if the password is correct), which performs operation using that model1.txt, this MODEL2 then generates model2.txt( with different contents different from model1.txt) again with the same password protected same as MODEL1

model2.txt is given to MODEL3 who knows the password, enters the correct password then decrypts the file to view the results.

this is want I want
Thanks
Feb 2 '07 #11
Ganon11
3,652 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. void encryptFile( ifstream& ifs, ofstream& ofs, string encryptKey )
  6. {
  7.     char line[256];    
  8.     while( !ifs.eof() )
  9.     {
  10.         ifs.getline( line, sizeof(line) );        
  11.  
  12.         string strLine = line;
  13.         unsigned int j=0;
  14.         for(unsigned int i=0; i<strLine.length(); ++i)
  15.         {    
  16.             strLine[i] += encryptKey[j++];            
  17.             ofs.put( strLine[i] );
  18.             if( j >= encryptKey.length() )
  19.                 j = 0;
  20.         }
  21.         cout<<"Encrypted data"<<endl;
  22.         cout<<strLine.data()<<endl;
  23.     }
  24. }
  25.  
  26. void decryptFile( ifstream& ifs, string encryptKey )
  27. {
  28.     char line[256];    
  29.     while( !ifs.eof() )
  30.     {
  31.         ifs.getline( line, sizeof(line) );
  32.         string strLine = line;
  33.         unsigned j=0;
  34.         for(unsigned i=0; i<strLine.length(); ++i)
  35.         {    
  36.             strLine[i] -= encryptKey[j++];            
  37.             if( j >= encryptKey.length() )
  38.                 j = 0;
  39.         }
  40.         cout<<"Decrypted data"<<endl;
  41.         cout<<strLine.data()<<endl;
  42.     }
  43. }
  44.  
  45. int main()
  46. {
  47.     ifstream ifs("test.txt");
  48.     if( ifs.fail() )
  49.         return 0;
  50.  
  51.     ofstream ofs("coded.txt");
  52.     if( ofs.fail() )
  53.         return 0;
  54.  
  55.     string encryptKey = "house";    
  56.     encryptFile( ifs, ofs, encryptKey );
  57.  
  58.     ofs.close();
  59.     ifs.close();
  60.  
  61.     ifstream ifstm("coded.txt");
  62.     if( !ifstm )
  63.         return 0;
  64.  
  65.     decryptFile( ifstm, encryptKey );    
  66.  
  67.     ifstm.close();
  68.  
  69.     return 0;
  70. }
This code looks good. The only think I'll point out is that, when adding and subtracting encryptKey, there is a possibility of going out of character bounds. A single character may be ASCII value 240 when you add a letter to it with ASCII value 65, making 305 - a number out of bounds for a char. You should use % to keep the number between 0 and 255.

The same will happen when subtracting encryptKey from the encoded string. In the above example, when properly bounded, the char ASCII value would be 50. But when you subtract 65 from it, you will get -15, which is out of bounds. You will need to find a way to keep that between 0 and 255 (Instead of subtracting, try adding 256 - that char value).
Feb 2 '07 #12
nmadct
83 Recognized Expert New Member
Yes i want to actually implement encryption myself.

"Encryption is tricky to do"
"I recommend you go read a book on it first"
--->Where can I find the best resources??????


"I recommend that you go download one of the various free encryption libraries that are available online and use the functions from that library to encrypt your data."
--->Please suggest some links such that I can get how it is implemented

Is there any problem with the solution that I have written?????

Please let me know about my queries.
Thanks
There's an online book about cryptography here:

http://www.cacr.math.uwaterloo.ca/hac/

Some resources with brief reviews:

http://www.youdzone.com/cryptobooks_Programming.html

It's good that you want to implement cryptography yourself, it's good as a programming exercise. However, I wanted to dispell the illusion that you're getting strong security from it. The kind of techniques being discussed here can easily be defeated by an attack that analyzes the frequency of letters and letter sequences in the text. I'm guessing such attacks would probably not even take very long on modern computers, ranging from seconds to minutes to crack your code. (This assumes that the data is text in a human language; arbitrary data would be hard to analyze in this way.)

If you want to use a freely available crypto library, have a look at this page:

http://www.homeport.org/~adam/crypto/

Another alternative is to use a stand-alone program like GnuPG to do the encryption, and simply invoke it from your program with the proper parameters.
Feb 2 '07 #13
vermarajeev
180 New Member
There's an online book about cryptography here:

http://www.cacr.math.uwaterloo.ca/hac/

Some resources with brief reviews:

http://www.youdzone.com/cryptobooks_Programming.html

It's good that you want to implement cryptography yourself, it's good as a programming exercise. However, I wanted to dispell the illusion that you're getting strong security from it. The kind of techniques being discussed here can easily be defeated by an attack that analyzes the frequency of letters and letter sequences in the text. I'm guessing such attacks would probably not even take very long on modern computers, ranging from seconds to minutes to crack your code. (This assumes that the data is text in a human language; arbitrary data would be hard to analyze in this way.)

If you want to use a freely available crypto library, have a look at this page:

http://www.homeport.org/~adam/crypto/

Another alternative is to use a stand-alone program like GnuPG to do the encryption, and simply invoke it from your program with the proper parameters.
I just tried using the crypto library but believe me I'm not getting anything...Can someone help me with this....

I have a text file, how can I encrypt it using crypto++ with password protected as discussed above....If you want any links please refer above threads...
Feb 7 '07 #14
hirak1984
316 Contributor
ABOUT PASSWORD PROTECTED FILES:

well say,there is a exe file "a.exe" which does some operation with a text file "b.txt".
Now if you double click on a.exe it will ask for password to open b.txt.
Writing code for this type of operation is easy.
But if we directly open b.txt using any text editor it will show content.
one cant password protect this file by writing any c code,because every code needs to be executed,which is irrelevant here.
what one can do is to hide b.txt in some other drive or encrypt it,or use some ready made software to password protect it.
if anyone can make the file b.txt password protected using c codes,please let me know.
Feb 12 '07 #15
nmadct
83 Recognized Expert New Member
I just tried using the crypto library but believe me I'm not getting anything...Can someone help me with this....

I have a text file, how can I encrypt it using crypto++ with password protected as discussed above....If you want any links please refer above threads...
I don't have time to write a program and test it, but I think you wanna do something like this:

Expand|Select|Wrap|Line Numbers
  1. int block_size = Twofish::BlockSize();
  2. char input[block_size];
  3. char output[block_size];
  4. // copy your data into input
  5. TwofishEncryption enc("mypassword");
  6. enc.ProcessBlock(input,output);
  7. // output is now filled with the encrypted version of input
  8. char original[block_size];
  9. Twofish::Decryption dec("mypassword");
  10. dec.ProcessBlock(output,original);
  11. // original now contains the same text as input
  12.  
Note that the encryption/decryption only works on one block of data at a time, in order to encrypt your whole file, you'll need to loop until it's done.
Feb 14 '07 #16
RedSon
5,000 Recognized Expert Expert
The easiest and most secure encryption method is to XOR your data with a random key. To decrypt it you must also have the same random key. All you do is XOR it again and your text appears. This is known as a one-time-pad or method.

It looks like some of the guys are telling you about some variations of a Cesar cipher like a polyalphabetic cipher or just a strait substitution cipher. But most of these methods are vulnerable to a statistical analysis attack. If you are serious about encryption and protecting your data, either use the XOR operation provided in c/c++ or use a crypto api. Your crypto api must be one that has been reviewed and throughly tested.

You must also consider how your OS manages your application's data. If you are really serious about protecting your data you must be sure that no key information remains in unprotected areas of memory or on the stack.

One last thing about XOR, no matter what you do never, ever, ever use the same key twice and never use a key that is at all similar to any other previous key that you have used. This is critical! If you use a key that is the same or similar it will be a simple matter to decrypt your message.
Feb 14 '07 #17
hirak1984
316 Contributor
Hi RedSon,
basically there are hundred methods of encryption-decryption.
Where we were struggling was if we can make a file password protected in such a way that while double clicking over it in windows,it will ask for password for opening.
And the code should be in c/c++.
Can you please throw some light on the matter?
The easiest and most secure encryption method is to XOR your data with a random key. To decrypt it you must also have the same random key. All you do is XOR it again and your text appears. This is known as a one-time-pad or method.

It looks like some of the guys are telling you about some variations of a Cesar cipher like a polyalphabetic cipher or just a strait substitution cipher. But most of these methods are vulnerable to a statistical analysis attack. If you are serious about encryption and protecting your data, either use the XOR operation provided in c/c++ or use a crypto api. Your crypto api must be one that has been reviewed and throughly tested.

You must also consider how your OS manages your application's data. If you are really serious about protecting your data you must be sure that no key information remains in unprotected areas of memory or on the stack.

One last thing about XOR, no matter what you do never, ever, ever use the same key twice and never use a key that is at all similar to any other previous key that you have used. This is critical! If you use a key that is the same or similar it will be a simple matter to decrypt your message.
Feb 15 '07 #18
sicarie
4,677 Recognized Expert Moderator Specialist
vermarajeev-

I believe you will find this link interesting: it is MIT's Cryptography Course that they opened to the public. As well, they have an Advanced Crypto Class. I don't know what levels they start at (I haven't gotten to them yet) - so I included both.
Feb 15 '07 #19
vermarajeev
180 New Member
Hi guys,

I'm feel happy that there are many people to clarify doubts.

I'm just going through the documents provided as a link.

If any problems I'll let you know....

Kudous to you all guys
Feb 15 '07 #20
RedSon
5,000 Recognized Expert Expert
Hi RedSon,
basically there are hundred methods of encryption-decryption.
Where we were struggling was if we can make a file password protected in such a way that while double clicking over it in windows,it will ask for password for opening.
And the code should be in c/c++.
Can you please throw some light on the matter?

Let there be light... http://msdn2.microsoft.com/en-us/library/aa365005.aspx
Feb 15 '07 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3191
by: Anon | last post by:
I made this class to encrypt my DataSet before saving it to disk. So, first in the main program I write the DataSet to XML in a MemoryStream. I pass this stream to the E_File sub, which encrypts...
2
2402
by: tfoxusenet | last post by:
Hi, I need to encrypt/decrypt some data for my C# application that can also be read on a unix system, and need a quick, simple, cross-platform solution I can embed in my own code that doesn't...
8
2717
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
4
4778
by: Sylvie | last post by:
http://www.obviex.com/samples/Encryption.aspx According to this link, I am using Rijndael Encryption & Decryption Algorithms, But I want my encrypted strings just CAPS string and just...
0
2358
by: Dipanwita | last post by:
I have written a RSA encryption/decryption function in c using the formula a = b^c %n. For solving the equation I have used Squaring and multiplying method for modulo exponentiation . These...
3
3046
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt...
5
9482
by: Netwatcher | last post by:
well, i started messing around with dictionaries, yet, most of the pages i found about them always talk about getting only one word out of it and turning it vice versa, i've been playing with that...
9
4809
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
9
2423
by: Alan M Dunsmuir | last post by:
In my (PHP-5) application I have to write some records to a table in my database, which I don't want even my clients using the system to be able to read. This is not a problem in National...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.