Symmetric Key File Encryption
In this encryption same key will be shared between two or more users and used for file encryption and decryption.
Using OpenSSL
Below openssl command can be used for encryptin file and decryption
Arguments
aes-256-cbc = AES 256 with Cipher-block chaining
-e Encryption
-d Decryption
-out Output result file
-K Symmetric key
-iv IV paramenter
Using Java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class SecretKeyEncryptionUtil {
/**
* Secret Key algorithm
*/
public static final String KEY_ALGORITHM = "AES";
/**
* Hash algorithm
*/
public static final String PASSWORD_HASH_ALGORITHM = "SHA-256";
/**
* AES Encryption algorithm with PKCS5 padding
*/
public static final String ENCRYPTION_ALGO = "AES/CBC/PKCS5Padding";
* Secret Key algorithm
*/
public static final String KEY_ALGORITHM = "AES";
/**
* Hash algorithm
*/
public static final String PASSWORD_HASH_ALGORITHM = "SHA-256";
/**
* AES Encryption algorithm with PKCS5 padding
*/
public static final String ENCRYPTION_ALGO = "AES/CBC/PKCS5Padding";
/**
* UTF-8 String encoding
*/
public static final String UTF8 = "UTF-8";
* UTF-8 String encoding
*/
public static final String UTF8 = "UTF-8";
/**
*
* IV Parameter
*
* @return
*/
*
* IV Parameter
*
* @return
*/
public static String IV_STRING = "d02144b4a865621f2efef8d26f7b157e";
public static byte[] getIV() {
byte[] iv = new byte[16];
byte[] temp = IV_STRING.getBytes();
for (int i = 0; i < 16; i++) {
byte[] temp = IV_STRING.getBytes();
for (int i = 0; i < 16; i++) {
iv[i] = temp[i];
}
return iv;
}
return iv;
}
/**
*
* Encrypt file using secret key
*
* @param originalFile
* File which needs to encrypt
* @param encryptedFilePath
* Destination file path to store encrypted data
* @param secretKey
* secret key to encrypt file
* @throws Exception
*/
*
* Encrypt file using secret key
*
* @param originalFile
* File which needs to encrypt
* @param encryptedFilePath
* Destination file path to store encrypted data
* @param secretKey
* secret key to encrypt file
* @throws Exception
*/
public static void encrypt(String originalFile, String encryptedFilePath,
SecretKeySpec secretKey) throws Exception {
SecretKeySpec secretKey) throws Exception {
FileOutputStream fos = null;
FileInputStream fis = null;
CipherOutputStream cout = null;
try {
FileInputStream fis = null;
CipherOutputStream cout = null;
try {
IvParameterSpec ivParameterSpec = new IvParameterSpec(
IV_STRING.getBytes("UTF-8"), 0, 16);
IV_STRING.getBytes("UTF-8"), 0, 16);
fos = new FileOutputStream(new File(encryptedFilePath));
fis = new FileInputStream(new File(originalFile));
Cipher encrypt = Cipher.getInstance(ENCRYPTION_ALGO);
encrypt.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
cout = new CipherOutputStream(fos, encrypt);
fis = new FileInputStream(new File(originalFile));
Cipher encrypt = Cipher.getInstance(ENCRYPTION_ALGO);
encrypt.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
cout = new CipherOutputStream(fos, encrypt);
byte[] buf = new byte[1024];
int read;
while ((read = fis.read(buf)) != -1)
cout.write(buf, 0, read);
int read;
while ((read = fis.read(buf)) != -1)
cout.write(buf, 0, read);
}
finally {
try {
if (fis != null) {
fis.close();
}
try {
if (fis != null) {
fis.close();
}
} catch (Exception ex) {
}
try {
if (cout != null) {
cout.flush();
}
try {
if (cout != null) {
cout.flush();
}
} catch (Exception ex) {
}
try {
if (cout != null) {
cout.close();
}
try {
if (cout != null) {
cout.close();
}
} catch (Exception ex) {
}
try {
if (fos != null) {
try {
if (fos != null) {
fos.close();
}
}
} catch (Exception ex) {
}
}
}
/**
* Decrypt file using secret key
*
* @param encryptedFile
* Encrypted file which needs to be decrypted
* @param decryptionFilePath
* Destination path to store decrypted file
* @param secretKey
* Secret key used for decryption
* @throws Exception
*/
public static void decrypt(String encryptedFile, String decryptionFilePath,
SecretKeySpec secretKey) throws Exception {
* Decrypt file using secret key
*
* @param encryptedFile
* Encrypted file which needs to be decrypted
* @param decryptionFilePath
* Destination path to store decrypted file
* @param secretKey
* Secret key used for decryption
* @throws Exception
*/
public static void decrypt(String encryptedFile, String decryptionFilePath,
SecretKeySpec secretKey) throws Exception {
FileOutputStream fos = null;
CipherInputStream cin = null;
FileInputStream fis = null;
try {
// byte[] iv = new byte[16];
// byte[] iv
// ={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
IvParameterSpec ivParameterSpec = new IvParameterSpec(
IV_STRING.getBytes("UTF-8"), 0, 16);
CipherInputStream cin = null;
FileInputStream fis = null;
try {
// byte[] iv = new byte[16];
// byte[] iv
// ={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
IvParameterSpec ivParameterSpec = new IvParameterSpec(
IV_STRING.getBytes("UTF-8"), 0, 16);
Cipher decrypt = Cipher.getInstance(ENCRYPTION_ALGO);
decrypt.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
decrypt.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
fis = new FileInputStream(new File(encryptedFile));
fos = new FileOutputStream(decryptionFilePath);
cin = new CipherInputStream(fis, decrypt);
fos = new FileOutputStream(decryptionFilePath);
cin = new CipherInputStream(fis, decrypt);
byte[] buf = new byte[1024];
int read = 0;
while ((read = cin.read(buf)) != -1)
fos.write(buf, 0, read);
int read = 0;
while ((read = cin.read(buf)) != -1)
fos.write(buf, 0, read);
} finally {
try {
if (fos != null) {
fos.close();
}
try {
if (fos != null) {
fos.close();
}
} catch (Exception ex) {
}
try {
if (fis != null) {
fis.close();
}
try {
if (fis != null) {
fis.close();
}
} catch (Exception ex) {
}
try {
if (cin != null) {
cin.close();
}
if (cin != null) {
cin.close();
}
} catch (Exception ex) {
}
}
}
public static byte[] decodeHex(final String encoded) {
byte[] decoded = new BigInteger(encoded, 16).toByteArray();
if (decoded[0] == 0) {
final byte[] tmp = new byte[decoded.length - 1];
System.arraycopy(decoded, 1, tmp, 0, tmp.length);
decoded = tmp;
}
return decoded;
}
byte[] decoded = new BigInteger(encoded, 16).toByteArray();
if (decoded[0] == 0) {
final byte[] tmp = new byte[decoded.length - 1];
System.arraycopy(decoded, 1, tmp, 0, tmp.length);
decoded = tmp;
}
return decoded;
}
public static void decrypt1(String encryptedFile,
String decryptionFilePath, String aesKey) throws Exception {
String decryptionFilePath, String aesKey) throws Exception {
FileOutputStream fos = null;
CipherInputStream cin = null;
FileInputStream fis = null;
CipherInputStream cin = null;
FileInputStream fis = null;
final byte[] secretKey = decodeHex(aesKey);
final byte[] initVector = decodeHex(IV_STRING);
try {
final byte[] initVector = decodeHex(IV_STRING);
try {
Cipher decrypt = Cipher.getInstance(ENCRYPTION_ALGO);
decrypt.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey,
"AES"),
new IvParameterSpec(initVector, 0, decrypt.getBlockSize()));
decrypt.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey,
"AES"),
new IvParameterSpec(initVector, 0, decrypt.getBlockSize()));
fis = new FileInputStream(new File(encryptedFile));
fos = new FileOutputStream(decryptionFilePath);
cin = new CipherInputStream(fis, decrypt);
fos = new FileOutputStream(decryptionFilePath);
cin = new CipherInputStream(fis, decrypt);
byte[] buf = new byte[1024];
int read = 0;
while ((read = cin.read(buf)) != -1)
fos.write(buf, 0, read);
int read = 0;
while ((read = cin.read(buf)) != -1)
fos.write(buf, 0, read);
} finally {
try {
if (fos != null) {
fos.close();
}
try {
if (fos != null) {
fos.close();
}
} catch (Exception ex) {
}
try {
if (fis != null) {
fis.close();
}
try {
if (fis != null) {
fis.close();
}
} catch (Exception ex) {
}
try {
if (cin != null) {
cin.close();
}
if (cin != null) {
cin.close();
}
} catch (Exception ex) {
}
}
}
public static SecretKeySpec loadSecretKey(String keyString)
throws Exception {
throws Exception {
MessageDigest digester = MessageDigest
.getInstance(PASSWORD_HASH_ALGORITHM);
digester.update(String.valueOf(keyString).getBytes(UTF8));
byte[] key = digester.digest();
SecretKeySpec spec = new SecretKeySpec(key, KEY_ALGORITHM);
return spec;
.getInstance(PASSWORD_HASH_ALGORITHM);
digester.update(String.valueOf(keyString).getBytes(UTF8));
byte[] key = digester.digest();
SecretKeySpec spec = new SecretKeySpec(key, KEY_ALGORITHM);
return spec;
}
public static void main(String[] args) throws Exception {
// TODO Read this from a file
String aesKey = "8fafa6e39e6bf3a3e11cc521696ab5a3552fb024e8fdca0ab277798e2e8d3308";
String aesKey = "8fafa6e39e6bf3a3e11cc521696ab5a3552fb024e8fdca0ab277798e2e8d3308";
// Create Secret Key
SecretKeySpec secretKey = loadSecretKey(aesKey);
// Encrypt Data using secret key
encrypt("c:/temp/201401060000221.dat",
"c:/temp/201401060000221_enc.dat", secretKey);
decrypt("c:/temp/201401060000221_enc.dat",
"c:/temp/201401060000221_dec.dat", secretKey);
"c:/temp/201401060000221_enc.dat", secretKey);
decrypt("c:/temp/201401060000221_enc.dat",
"c:/temp/201401060000221_dec.dat", secretKey);
}
}