Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

please help me  RSS feed

 
vikram veera
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to encrypt a given file?please help me in coding.
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
there are serveral ways on encryption-some standard or you can have your own algorithm.Standard encryption follow elaborate algorithms for which you will have to refer to books or sites on data encryption. However for simple uses, you can create your own algorithms.For example you can develop your algorithm where the characters are encrypted based on their position in the file and also based on the password. Her is a simple code to show you how encrypt a file "testfile.xyz" to "testfile.enc":
<code>
void encrypt(String password)
{
int currentchar;
int pos=0,passwordpos=0;
FileInputStream fis=new FileInputStream("testfile.xyz");
FileOutputStream fous=new FileOutputstream("testfile.enc");
//read the file character by character. let the current
//character read be in variable 'currentchar'
while((currentchar=fis.read())!=-1)
{
//encrypt the character with a simple algorithm
currentchar = currentchar+pos+ password.charAt( passwordpos );
pos++;
passwordpos++;
passwordpos=passwordpos%password.length();
fos.write(c);
}
}
</code>
To decrypt simply change the line
currentchar=currentchar+pos+password.charAt(passwordpos);
to
currentchar=currentchar-pos-password.charAt(passwordpos);
The encrpyted file cannot be easily decrypted even if one knows the algorithm (even the programmer) since the algorithm is also a function of the password. So the password has to be known to decrpyrt it.You can develope more comlpex algorithms yourself
hope this helps
Tanveer Rameez

[This message has been edited by Rahul Mahindrakar (edited January 04, 2001).]
 
vikram veera
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tanveer Rameez
for sending the code,i will try that code in my home after i will reply you.Please tell me the book and site for data encryption.
so that i can refer.
Tanveer Rameez
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!