File Encryption and Decryption in Java And Android

package raja.JavaPages4all;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

public class AES
{
private static final String key = “1234567891234567”;
private static final String method = “AES”;
/**
* @param args
*/
public static void main(String[] args)
{
try
{
long stime=System.currentTimeMillis();
File inFile = new File(“D:/sample.wmv”);
File outFile = new File(“D:/sample_encrypted.wmv”);
File outFile_dec = new File(“D:/sample_decrypted.wmv”);
encrypt(new FileInputStream(inFile), new FileOutputStream(outFile));
long etime=System.currentTimeMillis();
System.out.println((etime-stime));
decrypt(new FileInputStream(outFile), new FileOutputStream(outFile_dec));
System.out.println(System.currentTimeMillis()-etime);
} catch (Exception e)
{
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}

public static SecretKeySpec keyGen(String s) throws NoSuchAlgorithmException
{
KeyGenerator kgen = KeyGenerator.getInstance(method);
kgen.init(128); // 192 and 256 bits may not be available
byte[] raw = s.getBytes();
SecretKeySpec result = new SecretKeySpec(raw, method);
//System.out.println(“result “+new String(result.getEncoded()));
// Instantiate the cipher
return result;
}

public static void encrypt(InputStream is, OutputStream os)
{
encryptOrDecrypt(Cipher.ENCRYPT_MODE, is, os);
}

public static void decrypt(InputStream is, OutputStream os)
{
encryptOrDecrypt(Cipher.DECRYPT_MODE, is, os);
}

public static void encryptOrDecrypt(int mode, InputStream is, OutputStream os)
{
try
{
Cipher cipher = Cipher.getInstance(method); // DES/ECB/PKCS5Padding for SunJCE
if (mode == Cipher.ENCRYPT_MODE)
{
cipher.init(Cipher.ENCRYPT_MODE, keyGen(key));
CipherInputStream cis = new CipherInputStream(is, cipher);
doCopy(cis, os);
} else if (mode == Cipher.DECRYPT_MODE)
{
cipher.init(Cipher.DECRYPT_MODE, keyGen(key));
CipherOutputStream cos = new CipherOutputStream(os, cipher);
doCopy(is, cos);
}
} catch (Exception e)
{
e.printStackTrace();
} catch (Throwable e)
{
e.printStackTrace();
}
}

public static void doCopy(InputStream is, OutputStream os) throws IOException
{
/* byte[] bytes = new byte[1024];
int numBytes;
while ((numBytes = is.read(bytes)) != -1) {
os.write(bytes, 0, numBytes);
}
os.flush();
os.close();
is.close();*/

byte[] buffer = new byte[8192]; // Or larger (but use powers of 2)
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1)
{
os.write(buffer, 0, bytesRead);
}
os.flush();
os.close();
is.close();
}

}

Advertisements

Tagged: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: