Last active
January 1, 2016 17:39
-
-
Save yssharma/8178448 to your computer and use it in GitHub Desktop.
Java Encryption/ Decryption with AES Cipher. Compatible with cross platform C# Rijndael Cipher.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.FileInputStream; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import java.io.UnsupportedEncodingException; | |
| import java.security.Key; | |
| import java.security.NoSuchAlgorithmException; | |
| import java.security.spec.AlgorithmParameterSpec; | |
| import javax.crypto.Cipher; | |
| import javax.crypto.CipherInputStream; | |
| import javax.crypto.CipherOutputStream; | |
| import javax.crypto.spec.IvParameterSpec; | |
| import javax.crypto.spec.SecretKeySpec; | |
| public class AES { | |
| /** Cipher for encryption **/ | |
| Cipher ecipher; | |
| /** Cipher for decryption **/ | |
| Cipher dcipher; | |
| /** Buffer used to transport the bytes from one stream to another **/ | |
| byte[] buf = new byte[1024]; | |
| public AES(String strkey) throws UnsupportedEncodingException { | |
| byte[] keyBytes = new byte[16]; | |
| byte[] iv = new byte[16]; | |
| keyBytes = strkey.getBytes("UTF-8"); | |
| Key key = new SecretKeySpec(keyBytes, "AES"); | |
| iv = strkey.getBytes("UTF-8"); | |
| AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); | |
| try { | |
| ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
| dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
| /** CBC requires an initialization vector **/ | |
| ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); | |
| dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public void encrypt(InputStream in, OutputStream out) { | |
| try { | |
| out = new CipherOutputStream(out, ecipher); | |
| int numRead = 0; | |
| while ((numRead = in.read(buf)) >= 0) { | |
| out.write(buf, 0, numRead); | |
| } | |
| out.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public void decrypt(InputStream in, OutputStream out) { | |
| try { | |
| buf = new byte[1024]; | |
| in = new CipherInputStream(in, dcipher); | |
| int numRead = 0; | |
| while ((numRead = in.read(buf)) >= 0) { | |
| out.write(buf, 0, numRead); | |
| } | |
| out.close(); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static void main(String[] args) throws NoSuchAlgorithmException, | |
| FileNotFoundException, UnsupportedEncodingException { | |
| String ENCRYPT_IN = "C:/path/to/file/data.csv"; | |
| String ENCRYPT_OUT = "C:/path/to/file/data_encrypted.csv"; | |
| String DECRYPT_IN = "C:/path/to/file/data_encrypted.csv"; | |
| String DECRYPT_OUT = "C:/path/to/file/data_decrypted.csv"; | |
| String KEY = "SecretKey1234567"; // Has to be 16/24/32 char(byte) long | |
| AES test = new AES(KEY); | |
| test.encrypt(new FileInputStream(ENCRYPT_IN), new FileOutputStream(ENCRYPT_OUT)); | |
| System.out.println("Encrypted.."); | |
| test.decrypt(new FileInputStream(DECRYPT_IN), new FileOutputStream(DECRYPT_OUT)); | |
| System.out.println("Decrypted.."); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment