You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
import java.lang.Object;import javax.crypto.*;import java.security.*;import java.io.*;import javax.crypto.spec.SecretKeySpec;import java.util.Scanner;public class AESEncryption{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter E for encyrption or D for decryption: "); String a = input.nextLine(); if(a.equals("E")) { encrypt(); } else { decrypt(); } } public static void encrypt() { Scanner input = new Scanner(System.in); System.out.print("Enter the file name: "); String fileName = input.nextLine(); File file = new File(fileName); try { // create secret key
KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey key = keyGen.generateKey();
// initialize aes encryption object
Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key);
// read in bytes
FileInputStream fis = new FileInputStream(file); byte[] byteArray = new byte[(int)file.length()]; int buffer = fis.read(byteArray);
// encrypt bytes
byte[] bArray = cipher.doFinal(byteArray);
// write bytes (in place)
FileOutputStream fos = new FileOutputStream(file); fos.write(bArray);
// write key to file
FileOutputStream encodedKey = new FileOutputStream("key.txt"); byte[] keys = key.getEncoded(); encodedKey.write(keys);
// close result file
fis.close(); fos.close();
// close key file
encodedKey.close(); } catch(FileNotFoundException fnfe) { System.out.println("File Not Found"); } catch(IOException ioe) { System.out.println("Signals that an I/O exception of some sort has occurred."); } catch(IllegalBlockSizeException ibse) { System.out.println(" no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size; or if this encryption algorithm is unable to process the input data provided"); } catch(BadPaddingException bpe) { System.out.println("if this cipher is in decryption mode, and (un)padding has been requested, but the decrypted data is not bounded by the appropriate padding bytes"); } catch(NoSuchPaddingException nspe) { System.out.println("a particular padding mechanism is requested but is not available in the environment"); } catch(NoSuchAlgorithmException nsae) { System.out.println("cryptographic algorithm is requested but is not available in the environment"); } catch(InvalidKeyException ike) { System.out.println("the given key is inappropriate for initializing this cipher"); } } public static void decrypt() Scanner input = new Scanner(System.in); System.out.print("Enter the file name of whats to be decrypted: "); String fileName = input.nextLine(); System.out.print("Enter the file name of the key: "); String keyName = input.nextLine(); File encryptedFile = new File(fileName); File encryptedKey = new File(keyName); try { // load all data
FileInputStream fisKey = new FileInputStream(encryptedKey); FileInputStream fisFile = new FileInputStream(encryptedFile); FileOutputStream fos = new FileOutputStream("decryptedFile.txt");
// initialize output byte array
byte[] fileArray = new byte[(int)encryptedFile.length()]; int buffer2 = fisFile.read(fileArray);
// initialize key byte array
byte[] keyBytes = new byte[(int)encryptedKey.length()]; int buffer = fisKey.read(keyBytes);
// construct secret key from key bytes
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
// initialize cipher and decrypt
Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] cipherBytes = cipher.doFinal(fileArray);
// write to file
fos.write(cipherBytes); fisKey.close(); fos.close(); fisFile.close(); } catch(FileNotFoundException fnfe) { System.out.println("File Not Found"); } catch(IOException ioe) { System.out.println("Signals that an I/O exception of some sort has occurred."); } catch(IllegalBlockSizeException ibse) { System.out.println(" no padding has been requested (only in encryption mode), and the total input length of the data processed by this cipher is not a multiple of block size; or if this encryption algorithm is unable to process the input data provided"); } catch(NoSuchPaddingException nspe) { System.out.println("a particular padding mechanism is requested but is not available in the environment"); } catch(NoSuchAlgorithmException nsae) { System.out.println("cryptographic algorithm is requested but is not available in the environment"); } catch(InvalidKeyException ike) { System.out.println("the given key is inappropriate for initializing this cipher"); } catch(NullPointerException npe) { System.out.println("specified algorithm is null"); } catch(GeneralSecurityException gse) { System.out.println("bad or invalid padding/block"); } }}
|