Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
How to encrypt a file which can be read only through code of any program? I have a text file which i have use it such a way that users should not open and see inside that or change or view it. but it should be viewed or read only via code?


is there any way to do this?



when i compile the .cpp file is made as .obj file when this .obj file is opened by notepad its in some encrypted(not understandable)format.similarly how can i make .txt .obj form which when tried to view should be encrypted as .obj file.
Posted
Updated 21-Mar-12 0:14am
v7
Comments
Jochen Arndt 21-Mar-12 5:59am    
You have changed the original question in such a way that solution 1 makes no more sense. You should at least let the original question in place to let others follow the thread without being confused.

Your updated question makes no sense. OBJ files are well documented but are not intended to store some kind of encrypted plain text.
chaiein 21-Mar-12 6:16am    
now i updated with previous question. so is there any other way to make others not to look inside the .txt file or change?
Resmi Anna 21-Mar-12 6:36am    
It is possible with normal c++ file read/write operations. for example suppose the content of your text file is "This is my file". here you can replace
'T' with '0'
'h' with 'x'
'i' with '!
's' with '@'

so that your file may look like "0x!@ i@..."
you can use your own logic to do this. What i said is just an example
chaiein 21-Mar-12 7:11am    
Thank you all :)for the solutions. i solved as below solution 3

 
Share this answer
 
Comments
chaiein 20-Mar-12 2:44am    
Thank you:)
chaiein 20-Mar-12 2:52am    
Actually i meant encrypting which is english only but when viewed should be in the format which user should not be able to read or understand. but the code should be able to read all that like a txt
file.
enhzflep 20-Mar-12 3:10am    
Well, why not asciiHex(or something more compact) encode the encrypted data?

Just encrypt your text file to a memory buffer then output the hexValue of each character in the encrypted stream.

I.e
char str[] = {0,1,16,10,255,0};
char *str2 = asciiHex(str);

After above call, str2 should look like this: "00 01 10 0A FF"
Chandrasekharan P 20-Mar-12 4:57am    
I would anytime go for asciiHex.
enhzflep 20-Mar-12 5:01am    
Depends on what it's going to be used for.
AsciiHex representation takes up 100% more storage space.
Ascii85 representation takes up ~25% more space

You could LZW then ascii85 to get an input that was both
(a) smaller and
(b) unreadable
A very simple solution:
C++
void Encrypt(LPCTSTR lpszFile, const BYTE *pBuf, unsigned nSize)
{
    LPBYTE pEnc = new BYTE[nSize];
    memcpy(pEnc, pBuf, nSize);
    for (unsigned i = 0; i < nSize; i++)
        pEnc[i] ^= 0xAA;
    CFile file;
    file.Open(lpszFile, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
    file.Write(pEnc, nSize);
    file.Close();
    delete [] pEnc;
}
LPBYTE Decrypt(LPCTSTR lpszFile, unsigned& nSize)
{
    CFile file;
    file.Open(lpszFile, CFile::modeRead | CFile::typeBinary);
    nSize = file.GetLength();
    LPBYTE pBuf = new BYTE[nSize];
    file.Read(pBuf, nSize);
    file.Close();
    for (unsigned i = 0; i < nSize; i++)
        pBuf[i] ^= 0xAA;
    // Calling function must delete pBuf!
    return pBuf;
}
// UPDATE: Added these functions:
void EncryptFile(LPCTSTR lpszInFile, LPCTSTR lpszOutFile)
{
    CFile file;
    file.Open(lpszInFile, CFile::modeRead | CFile::typeBinary);
    nSize = file.GetLength();
    LPBYTE pBuf = new BYTE[nSize];
    file.Read(pBuf, nSize);
    file.Close();
    Encrypt(lpszOutFile, pBuf, nSize);
    delete [] pBuf;
}
void DecryptFile(LPCTSTR lpszInFile, LPCTSTR lpszOutFile)
{
    unsigned nSize = 0;
    LPBYTE pBuf = Decrypt(lpzsInFile, nSize);
    CFile file;
    file.Open(lpszOutFile, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
    file.Write(pBuf, nSize);
    file.Close();
    delete [] pBuf;
}

You may replace the loop using XOR for encryption and decryption by any other method.
 
Share this answer
 
v2
Comments
chaiein 21-Mar-12 6:37am    
please help in this when I call this above function Encrypt("./sample 1.txt",what next?
Jochen Arndt 21-Mar-12 6:46am    
See the updated solution.
chaiein 21-Mar-12 7:09am    
Jochen Arndt Thank you so much :) Its working :)
An object file is compiled, not encrypted.
On the other hand you need a way to actually encrypt your plain text file.
While Resmi Anna's proposed link is good, you may also consider using a freely available encryption tool ( Google[^] is you friend).
 
Share this answer
 
Comments
chaiein 21-Mar-12 6:24am    
but its little confusing for me.
When i have a text file with my required content encrypt once via code.then decrypt any number of times when ever using the text file? am i right?
CPallini 21-Mar-12 6:31am    
When you have the encrypted file then you have to decrypt it to get back the original content. Once you have the original content, well..., you have it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900