Browse Source

(hopefully) last version

master
Raphael Roberts 7 years ago
parent
commit
517597f67f
  1. 152
      AESEncryption.java

152
AESEncryption.java

@ -1,25 +1,74 @@
import java.lang.Object;
import javax.crypto.Cipher;
import java.security.GeneralSecurityException;
import javax.crypto.NoSuchPaddingException;
import java.security.NoSuchAlgorithmException;
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)
{
}
public void encrypt()
{
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 final Cipher getInstance(String transformation)
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
{
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey key = keyGen.generateKey();
SecretKey key = keyGen.generateKey();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
FileInputStream fis = new FileInputStream(file);
byte[] byteArray = new byte[(int)file.length()];
int buffer = fis.read(byteArray);
byte[] bArray = cipher.doFinal(byteArray);
FileOutputStream fos = new FileOutputStream(file);
fos.write(bArray);
FileOutputStream encodedKey = new FileOutputStream("key.txt");
byte[] keys = key.getEncoded();
encodedKey.write(keys);
fis.close();
fos.close();
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)
{
@ -29,6 +78,83 @@ public class AESEncryption
{
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");
}
}//end of encrypt method
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
{
FileInputStream fisKey = new FileInputStream(encryptedKey);
FileInputStream fisFile = new FileInputStream(encryptedFile);
FileOutputStream fos = new FileOutputStream("decryptedFile.txt");
byte[] fileArray = new byte[(int)encryptedFile.length()];
int buffer2 = fisFile.read(fileArray);
byte[] keyBytes = new byte[(int)encryptedKey.length()];
int buffer = fisKey.read(keyBytes);
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] cipherBytes = cipher.doFinal(fileArray);
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");
}
}
}
Loading…
Cancel
Save