This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!