File encryption and decryption with DES key in java programming
Friday, June 05, 2015
Encryption and decryption are fundamental requirements of every secure-aware application, therefore the Java platform provides strong support for encryption and decryption through its Java Cryptographic Extension (JCE) framework which implements the standard cryptographic algorithms such as AES, DES, DESede and RSA. Recently its need to me encrypt a data file for data secure and I have used this code as its logic on my github.
The CipherData class gives an example of using DES (Data Encryption Standard) to encrypt and decrypt files. DES requires a key (ie, password) that is at least 8 characters long. An exception will be thrown if the password does not meet the minimum size requirements. The CipherData class takes a plaintext file, "source.xml", and encrypts its contents, storing the ciphertext result in the "source_encrypted.xml" file. It then takes the "source_encrypted.xml" file and decrypts its ciphertext contents, storing the resulting plaintext in "source_decrypted.xml". When done, the contents of "source.xml" and "source_decrypted.xml" match. The encryption is handled by the use of a DES key and a DES cipher.
package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class CipherData {
public static void main(String[] args) {
try {
//////Encrypt source file (source.xml to encrypted_source.xml )/////
Scanner sc = new Scanner(System.in);
System.out.println("Enter encrypt key: ");
String enKey = sc.next(); // needs to be at least 8 characters for DES
FileInputStream fis = new FileInputStream("source.xml");
FileOutputStream fos = new FileOutputStream("encrypted_source.xml");
encrypt(enKey, fis, fos);
//////Decrypt source file (encrypted_source.xml to decrypted_source.xml )/////
System.out.println("Enter encrypt key: ");
String deKey = sc.next(); // needs to be at least 8 characters for DES
FileInputStream fis2 = new FileInputStream("encrypted_source.xml");
FileOutputStream fos2 = new FileOutputStream("decrypted_source.xml");
decrypt(deKey, fis2, fos2);
} catch (Throwable e) {
}
}
public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
}
public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
}
public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey desKey = skf.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE
if (mode == Cipher.ENCRYPT_MODE) {
cipher.init(Cipher.ENCRYPT_MODE, desKey);
CipherInputStream cis = new CipherInputStream(is, cipher);
doCopy(cis, os);
} else if (mode == Cipher.DECRYPT_MODE) {
cipher.init(Cipher.DECRYPT_MODE, desKey);
CipherOutputStream cos = new CipherOutputStream(os, cipher);
doCopy(is, cos);
}
}
public static void doCopy(InputStream is, OutputStream os) throws IOException {
byte[] bytes = new byte[64];
int numBytes;
while ((numBytes = is.read(bytes)) != -1) {
os.write(bytes, 0, numBytes);
}
os.flush();
os.close();
is.close();
}
}
Labels: Java
Post a Comment