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.

163 lines
5.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. import java.lang.Object;
  2. import javax.crypto.*;
  3. import java.security.*;
  4. import java.io.*;
  5. import javax.crypto.spec.SecretKeySpec;
  6. import java.util.Scanner;
  7. public class AESEncryption
  8. {
  9. public static void main(String[] args)
  10. {
  11. Scanner input = new Scanner(System.in);
  12. System.out.print("Enter E for encyrption or D for decryption: ");
  13. String a = input.nextLine();
  14. if(a.equals("E"))
  15. {
  16. encrypt();
  17. }
  18. else
  19. {
  20. decrypt();
  21. }
  22. }
  23. public static void encrypt()
  24. {
  25. Scanner input = new Scanner(System.in);
  26. System.out.print("Enter the file name: ");
  27. String fileName = input.nextLine();
  28. File file = new File(fileName);
  29. try
  30. {
  31. // create secret key
  32. KeyGenerator keyGen = KeyGenerator.getInstance("AES");
  33. keyGen.init(128);
  34. SecretKey key = keyGen.generateKey();
  35. // initialize aes encryption object
  36. Cipher cipher = Cipher.getInstance("AES");
  37. cipher.init(Cipher.ENCRYPT_MODE, key);
  38. // read in bytes
  39. FileInputStream fis = new FileInputStream(file);
  40. byte[] byteArray = new byte[(int)file.length()];
  41. int buffer = fis.read(byteArray);
  42. // encrypt bytes
  43. byte[] bArray = cipher.doFinal(byteArray);
  44. // write bytes (in place)
  45. FileOutputStream fos = new FileOutputStream(file);
  46. fos.write(bArray);
  47. // write key to file
  48. FileOutputStream encodedKey = new FileOutputStream("key.txt");
  49. byte[] keys = key.getEncoded();
  50. encodedKey.write(keys);
  51. // close result file
  52. fis.close();
  53. fos.close();
  54. // close key file
  55. encodedKey.close();
  56. }
  57. catch(FileNotFoundException fnfe)
  58. {
  59. System.out.println("File Not Found");
  60. }
  61. catch(IOException ioe)
  62. {
  63. System.out.println("Signals that an I/O exception of some sort has occurred.");
  64. }
  65. catch(IllegalBlockSizeException ibse)
  66. {
  67. 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");
  68. }
  69. catch(BadPaddingException bpe)
  70. {
  71. 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");
  72. }
  73. catch(NoSuchPaddingException nspe)
  74. {
  75. System.out.println("a particular padding mechanism is requested but is not available in the environment");
  76. }
  77. catch(NoSuchAlgorithmException nsae)
  78. {
  79. System.out.println("cryptographic algorithm is requested but is not available in the environment");
  80. }
  81. catch(InvalidKeyException ike)
  82. {
  83. System.out.println("the given key is inappropriate for initializing this cipher");
  84. }
  85. }
  86. public static void decrypt()
  87. Scanner input = new Scanner(System.in);
  88. System.out.print("Enter the file name of whats to be decrypted: ");
  89. String fileName = input.nextLine();
  90. System.out.print("Enter the file name of the key: ");
  91. String keyName = input.nextLine();
  92. File encryptedFile = new File(fileName);
  93. File encryptedKey = new File(keyName);
  94. try
  95. {
  96. // load all data
  97. FileInputStream fisKey = new FileInputStream(encryptedKey);
  98. FileInputStream fisFile = new FileInputStream(encryptedFile);
  99. FileOutputStream fos = new FileOutputStream("decryptedFile.txt");
  100. // initialize output byte array
  101. byte[] fileArray = new byte[(int)encryptedFile.length()];
  102. int buffer2 = fisFile.read(fileArray);
  103. // initialize key byte array
  104. byte[] keyBytes = new byte[(int)encryptedKey.length()];
  105. int buffer = fisKey.read(keyBytes);
  106. // construct secret key from key bytes
  107. SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
  108. // initialize cipher and decrypt
  109. Cipher cipher = Cipher.getInstance("AES");
  110. cipher.init(Cipher.DECRYPT_MODE, key);
  111. byte[] cipherBytes = cipher.doFinal(fileArray);
  112. // write to file
  113. fos.write(cipherBytes);
  114. fisKey.close();
  115. fos.close();
  116. fisFile.close();
  117. }
  118. catch(FileNotFoundException fnfe)
  119. {
  120. System.out.println("File Not Found");
  121. }
  122. catch(IOException ioe)
  123. {
  124. System.out.println("Signals that an I/O exception of some sort has occurred.");
  125. }
  126. catch(IllegalBlockSizeException ibse)
  127. {
  128. 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");
  129. }
  130. catch(NoSuchPaddingException nspe)
  131. {
  132. System.out.println("a particular padding mechanism is requested but is not available in the environment");
  133. }
  134. catch(NoSuchAlgorithmException nsae)
  135. {
  136. System.out.println("cryptographic algorithm is requested but is not available in the environment");
  137. }
  138. catch(InvalidKeyException ike)
  139. {
  140. System.out.println("the given key is inappropriate for initializing this cipher");
  141. }
  142. catch(NullPointerException npe)
  143. {
  144. System.out.println("specified algorithm is null");
  145. }
  146. catch(GeneralSecurityException gse)
  147. {
  148. System.out.println("bad or invalid padding/block");
  149. }
  150. }
  151. }