Skip to content

Instantly share code, notes, and snippets.

@ankurs
Created December 11, 2009 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankurs/254402 to your computer and use it in GitHub Desktop.
Save ankurs/254402 to your computer and use it in GitHub Desktop.
/* Initial implementation for a folder lock
* Author: Ankur Shrivastava
* License: GPLv3
*/
import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
class Lock
{
public static void main (String args[])
{
// functions here
}
public static void unzip(String filename)
{
/* takes filename as agrument and unzips the file
*/
try
{
FileInputStream fis = new FileInputStream(filename); // read the file
ZipInputStream zis = new ZipInputStream(fis); // open file in zip stream
byte buf[] = new byte[1024]; // buffer to hold temperory data
ZipEntry entry; // to hold file entries
entry = zis.getNextEntry(); // get file entery
while (entry != null) // loop till we have entries
{
System.out.println("Name-> " + entry.getName());
System.out.println("CSize-> " + entry.getCompressedSize());
System.out.println("Size-> " + entry.getSize());
FileOutputStream fos = new FileOutputStream("New-" +entry.getName()); // open file for entry
int len;
while ((len = zis.read(buf,0,buf.length)) != -1) // loop till end of data for the file
{
fos.write(buf,0,len); // write data in uncompressed file
}
fos.close(); // close file
entry = zis.getNextEntry(); // get next file
System.out.println();
}
}
catch (Exception e) // catch all exceptions #TODO proper clean up
{
System.out.println("Error " + e);
}
}
public static void zip(String files[],String filename)
{
/* Takes array of files to be ziped
* zipes and stores them in the file -> filename
*/
try
{
FileOutputStream fos = new FileOutputStream(filename); // open file to be written
ZipOutputStream zos = new ZipOutputStream(fos); // open the zip stream
byte buf[] = new byte[1024]; // data buffer
ZipEntry entry; // entry
for (int i =0; i< files.length;i++) // go through files one by one
{
entry = new ZipEntry(files[i]);// init entry
zos.putNextEntry(entry); // add entery to zip file
FileInputStream fis = new FileInputStream(files[i]); // read teh file
int len;
while ((len = fis.read(buf,0,buf.length)) != -1) // read till end
{
zos.write(buf,0,len); // write file entry
}
fis.close();
zos.closeEntry();
}
zos.close();
}
catch (Exception e) // catch all exceptions #TODO proper cleanup
{
System.out.println("Error " + e);
}
}
public static byte[] getKey(String password)
{
/*
* Takes a string as input and returns its MD5 in byte[]
*/
try
{
MessageDigest md5 = MessageDigest.getInstance("MD5"); // get the MD5 instance
return md5.digest(password.getBytes()); // get md5 of key ( byte[] )
}
catch(Exception e)
{
System.out.println("Error " + e);
}
return new byte[1];
}
public static void encrypt(String filename, String password)
{
/*
* Takes filename and password as argument and creates a encrypted file by name filename.enc
*/
try
{
FileInputStream fis = new FileInputStream(filename); // file to be encrypted
FileOutputStream fos = new FileOutputStream(filename+".enc"); // encrtpted file
byte[] keyBytes = getKey(password); // get md5 hash of the password
SecretKeySpec key = new SecretKeySpec(keyBytes,"AES"); // secret key for aes
Cipher cipher = Cipher.getInstance("AES"); // get cipher
cipher.init(Cipher.ENCRYPT_MODE,key); // init cipher
CipherInputStream cis = new CipherInputStream(fis,cipher); // get chiper input stream, to read teh file
byte buf[] = new byte[1024];
int len;
while( (len = cis.read(buf,0,buf.length)) != -1) // read till end
{
fos.write(buf,0,len); // write out to encrypted file
}
cis.close();
fos.close();
fis.close();
}
catch (Exception e) // catch all exceptions #TODO proper cleanup
{
System.out.println("Error " + e);
}
}
public static void decrypt(String filename, String password)
{
/*
* Takes filename and password as arguments and decrypts file "filename.enc" stores it as filename
*/
try
{
FileInputStream fis = new FileInputStream(filename+".enc"); // file to be decrypted
FileOutputStream fos = new FileOutputStream(filename); // decrtpted file
byte[] keyBytes = getKey(password); // get md5 hash of the password
SecretKeySpec key = new SecretKeySpec(keyBytes,"AES"); // secret key for aes
Cipher cipher = Cipher.getInstance("AES"); // get cipher
cipher.init(Cipher.DECRYPT_MODE,key); // init cipher
CipherInputStream cis = new CipherInputStream(fis,cipher); // get chiper input stream, to read teh file
byte buf[] = new byte[1024];
int len;
while( (len = cis.read(buf,0,buf.length)) != -1) // read till end
{
fos.write(buf,0,len); // write out to encrypted file
}
cis.close();
fos.close();
fis.close();
}
catch (Exception e) // catch all exceptions #TODO proper cleanup
{
System.out.println("Error " + e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment