LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2007, 10:23 PM   #1
vedanu
LQ Newbie
 
Registered: Feb 2007
Location: Bangalore
Distribution: Fedora
Posts: 18

Rep: Reputation: 0
Encrypting a file


Hello All,

I have struck in a problem can any one give the solution....

I have a abc.txt file ,I am using AES algorithm to encrypt this file ...
This algorithm takes the input file and output file as arguments...

Ex if i give abc.txt as input ,the output is abc.txt.enc.
where abc.txt.enc is an encrypted file ..

But the problem is that when I give input file i.e., txt file I want the txt content to be replaced with encrypted content in the same file ...

I mean to say the input file and output file should be the same...

Similarlly while decrypting .....

VedaAnuhttp://linuxquestions.cachefly.net/images/questions/images/smilies/newbie.gif
 
Old 02-28-2007, 10:24 PM   #2
vedanu
LQ Newbie
 
Registered: Feb 2007
Location: Bangalore
Distribution: Fedora
Posts: 18

Original Poster
Rep: Reputation: 0
Encrypting a file

Hello All,

I have struck in a problem can any one give the solution....

I have a abc.txt file ,I am using AES algorithm to encrypt this file ...
This algorithm takes the input file and output file as arguments...

Ex if i give abc.txt as input ,the output is abc.txt.enc.
where abc.txt.enc is an encrypted file ..

But the problem is that when I give input file i.e., txt file I want the txt content to be replaced with encrypted content in the same file ...

I mean to say the input file and output file should be the same...

Similarlly while decrypting .....
 
Old 03-01-2007, 12:26 AM   #3
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
Code:
$ aes-encrypt abc.txt abc.text.1 && mv abc.text.1 abc.text
 
Old 03-01-2007, 05:31 AM   #4
pworcester
LQ Newbie
 
Registered: Apr 2006
Posts: 7

Rep: Reputation: 0
RE:Encrypting a file

Use Vim to encrypt the file. It will do exactly what you want.
 
Old 03-01-2007, 06:24 AM   #5
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33
You need to create a temporary file for the output (either direction) and then unlink the first and rename the temporary file at the end.

In principle, since (I think!) AES is a block cipher where the size of a decrypted block is the same as an encrypted block, you could read a block, en/decrypt it, "seek" backwards in the file by one block and overwrite the content before moving forward. Check the lseek or fseek man pages. But I wouldn't recommend this - if you fail midway, you will have corrupted the original, and you would also have to fiddle the length of the file somehow in the last block (see the truncate function).

HTH.
 
Old 03-01-2007, 12:17 PM   #6
gavinbeatty
Member
 
Registered: Nov 2003
Posts: 79

Rep: Reputation: Disabled
Use something like encfs (requires FUSE) if you never need to share the file in encrypted form.
 
Old 03-01-2007, 04:05 PM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Well, 1st you need to post your Q in only 1 thread.
 
Old 03-01-2007, 04:06 PM   #8
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
edit: see above (post merge)

Last edited by archtoad6; 03-04-2007 at 07:23 AM.
 
Old 03-01-2007, 04:07 PM   #9
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
Also be aware if what you're trying to do is eliminate the plaintext from the hard drive completely, it's tough to reliably accomplish that. Using a temporary file and then unlinking the old one leaves the plaintext somewhere on the disk. Journaling filesystems or even swap space can retain the plaintext. Ways around that: encrypt your swap partition, only put plaintext files on encrypted filesystems (either a partition created with cryptsetup or perhaps just a filesystem that lives in a regular file mounted on a cryptoloop) or in a ramdisk (like /dev/shm), or use ext2 and the shred utility.
 
Old 03-01-2007, 09:46 PM   #10
Jacko123
LQ Newbie
 
Registered: Mar 2007
Posts: 13

Rep: Reputation: 0
Quote:
Originally Posted by puffinman
Also be aware if what you're trying to do is eliminate the plaintext from the hard drive completely, it's tough to reliably accomplish that. Using a temporary file and then unlinking the old one leaves the plaintext somewhere on the disk. Journaling filesystems or even swap space can retain the plaintext. Ways around that: encrypt your swap partition, only put plaintext files on encrypted filesystems (either a partition created with cryptsetup or perhaps just a filesystem that lives in a regular file mounted on a cryptoloop) or in a ramdisk (like /dev/shm), or use ext2 and the shred utility.
I have one more idea. create 'test.txt' with plain text, 'test.txt.enc' with encoded text.. Delete 'test.txt' and rename 'test.txt.enc' back to 'test.txt'. This logic will work for both windows and linux systems.

But I have one question, will this effect the peformance since I first delete and then rename the file. Will my application be slower.

One more doubt will the application be secure if you follow the above logic. Will the hacker have any chance to get the plain text??

Hi puffinman, the solution you have told works for linux what if my application is portable one(windows and linux)???

"encrypt your swap partition, only put plaintext files on encrypted filesystems (either a partition created with cryptsetup or perhaps just a filesystem that lives in a regular file mounted on a cryptoloop) or in a ramdisk (like /dev/shm), or use ext2 and the shred utility."

Please explain this. I'm unclear about this explanation. If possible with some code example.

Thanks,
Jacko
 
Old 03-02-2007, 03:01 AM   #11
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33
Quote:
Originally Posted by Jacko123
But I have one question, will this effect the peformance since I first delete and then rename the file. Will my application be slower.
Strictly, yes. But in practice, deleting and renaming a file will happen in the blinking of an eye. It would only be an issue if you were trying to process (say) thousands of files per second in your application.

Quote:
Originally Posted by Jacko123
One more doubt will the application be secure if you follow the above logic. Will the hacker have any chance to get the plain text??
This was puffinman's point. As long as the plaintext is on the disk anywhere, it's hard to guarantee it's been deleted (in the sense of not being on the physical disk surface - obviously the file is deleted). The techniques he refers to are worth considering.
 
Old 03-03-2007, 01:49 AM   #12
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
these two threads have been merged. please follow the rules you agreed to next time and don't post twice.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
encrypting partitions shouup SUSE / openSUSE 3 12-28-2006 01:49 PM
Encrypting things RVR777 Linux - Security 2 06-01-2006 02:24 PM
Encrypting an entire CD binarybob0001 Linux - General 0 03-26-2006 04:30 PM
Encrypting Question Bd22 Linux - Security 1 07-11-2003 09:26 PM
encrypting im1crazyassmofo Linux - General 1 04-20-2003 09:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration