Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encrypt a text file using C?

Tags:

c

encryption

I want to have a program like this:

scanf("%s", name);
scanf("%s", id);
scanf("%d", &age);

Now I want to write name, id and age to a file, but the file should be encrypted, so that only my program can read back the data it wrote.

Do I need to use one of the encryption libraries mentioned here (they all are pretty heavy duty libraries, I just need to encrypt a text file), or is there any other simpler method?

If an encryption library is the solution, which one is best suited for a simple text file encryption?

like image 557
Lazer Avatar asked Aug 01 '10 13:08

Lazer


People also ask

Can a TXT be encrypted?

Windows Encrypting File SystemTo use EFS to encrypt your file, right-click the text file and select Properties. Now, select Advanced, then check the box to Encrypt contents to secure data. Press OK, then Apply.

What is encrypt in C?

The encrypt() function uses an array of 16 48-bit keys produced by the setkey() function to encode bytes specified by the block argument according to the Data Encryption Standard (DES) encryption algorithm or to decode argument bytes according to the DES decryption algorithm.

Can Notepad ++ encrypt files?

Using a plug-in for the popular Windows text editor, Notepad++, you can easily encrypt selected text and entire text files.


2 Answers

If you want security, it is a mistake to roll your own encryption library. Use a well established encryption library (even if it may seem bloated), and leave security and its proper implementation to the experts.

If you can use C++, I suggest Crypto++, and if you can't use C++, then I suggest you implement a C wrapper library around Crypto++. Another possibility is libcrypto, although it lacks support for AES.

I should warn you, though, that if the program and the text file are on the same machine, you will need to have the password supplied externally (e.g. by the user); passwords that are embedded in programs are easily extracted, and offer no security at all. If the program is inaccessible (e.g. it is located on a webserver, and someone with the text file won't have access to the executable), then it is ok.

like image 88
Michael Aaron Safyan Avatar answered Oct 27 '22 09:10

Michael Aaron Safyan


Better use a symmetric-key algorithm, as AES. You can find small sourcecodes here for instance.

If your applications are critical, then you should use the libraries you linked to.

like image 30
Wok Avatar answered Oct 27 '22 08:10

Wok