Skip to content

Instantly share code, notes, and snippets.

@yssharma
Last active April 28, 2022 03:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yssharma/8216192 to your computer and use it in GitHub Desktop.
Save yssharma/8216192 to your computer and use it in GitHub Desktop.
Basic java code to Encrypt/Decrypt Files present on the HDFS. The code does not use Map-Reduce jobs for the process. The code is cross-platform compatible C# modules. The C# code would need little tweaking for cross-platform compatibility: aes.Padding = PaddingMode.PKCS7; aes.Mode = CipherMode.CBC; aes.KeySize = 128; aes.BlockSize = 128;
package com.test.aes;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class AESTest {
Cipher ecipher;
Cipher dcipher;
static String ENCRYPT_IN = "hdfs://localhost:9000/aestest/data.txt";
static String ENCRYPT_OUT = "hdfs://localhost:9000/aestest/data.enc";
static String DECRYPT_IN = "hdfs://localhost:9000/aestest/data.enc";
static String DECRYPT_OUT = "hdfs://localhost:9000/aestest/data.dec";
static String KEY = "ThisIsASecretKey";
static String ALGONAME = "AES";
public AESTest(Key key) {
/** 8-byte initialization vector **/
byte[] iv = byte[] iv = key.getBytes("UTF-8");
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
try
{
ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
/** CBC Cipher requires an initialization vector **/
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
}
catch (Exception e)
{
e.printStackTrace();
}
}
// Buffer used to transport the bytes from one stream to another
byte[] buf = new byte[1024];
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 {
in = new CipherInputStream(in, dcipher);
int numRead = 0;
while ((numRead = in.read(buf)) >= 0) {
out.write(buf, 0, numRead);
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws NoSuchAlgorithmException,
FileNotFoundException, IOException {
Key key = new SecretKeySpec(KEY.getBytes(), ALGONAME);
AESTest test = new AESTest(key);
Path ENC_IN_PATH = new Path(ENCRYPT_IN);
Path ENC_OUT_PATH = new Path(ENCRYPT_OUT);
Path DEC_IN_PATH = new Path(DECRYPT_IN);
Path DEC_OUT_PATH = new Path(DECRYPT_OUT);
FileSystem filesystem = FileSystem.get(new Configuration());
test.encrypt(filesystem.open(ENC_IN_PATH), filesystem.create(ENC_OUT_PATH));
System.out.println("Encrypted..");
test.decrypt(filesystem.open(DEC_IN_PATH), filesystem.create(DEC_OUT_PATH));
System.out.println("Decrypted..");
}
}
@aryasindhu
Copy link

aryasindhu commented Oct 26, 2016

key.getBytes("UTF-8"); is undefined. Please update the code snippet.
key.getEncoded(); can be used here as replacement.

@s-l-s-aditya
Copy link

Hey i am trying use this program to encrypt/decrypt files on hadoop could you help me with the installations and packages needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment