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.

159 lines
5.2 KiB

  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. KeyGenerator keyGen = KeyGenerator.getInstance("AES");
  32. keyGen.init(128);
  33. SecretKey key = keyGen.generateKey();
  34. Cipher cipher = Cipher.getInstance("AES");
  35. cipher.init(Cipher.ENCRYPT_MODE, key);
  36. FileInputStream fis = new FileInputStream(file);
  37. byte[] byteArray = new byte[(int)file.length()];
  38. int buffer = fis.read(byteArray);
  39. byte[] bArray = cipher.doFinal(byteArray);
  40. FileOutputStream fos = new FileOutputStream(file);
  41. fos.write(bArray);
  42. FileOutputStream encodedKey = new FileOutputStream("key.txt");
  43. byte[] keys = key.getEncoded();
  44. encodedKey.write(keys);
  45. fis.close();
  46. fos.close();
  47. encodedKey.close();
  48. }
  49. catch(FileNotFoundException fnfe)
  50. {
  51. System.out.println("File Not Found");
  52. }
  53. catch(IOException ioe)
  54. {
  55. System.out.println("Signals that an I/O exception of some sort has occurred.");
  56. }
  57. catch(IllegalBlockSizeException ibse)
  58. {
  59. 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");
  60. }
  61. catch(BadPaddingException bpe)
  62. {
  63. 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");
  64. }
  65. catch(NoSuchPaddingException nspe)
  66. {
  67. System.out.println("a particular padding mechanism is requested but is not available in the environment");
  68. }
  69. catch(NoSuchAlgorithmException nsae)
  70. {
  71. System.out.println("cryptographic algorithm is requested but is not available in the environment");
  72. }
  73. catch(InvalidKeyException ike)
  74. {
  75. System.out.println("the given key is inappropriate for initializing this cipher");
  76. }
  77. }//end of encrypt method
  78. public static void decrypt()
  79. {
  80. Scanner input = new Scanner(System.in);
  81. System.out.print("Enter the file name of whats to be decrypted: ");
  82. String fileName = input.nextLine();
  83. System.out.print("Enter the file name of the key: ");
  84. String keyName = input.nextLine();
  85. File encryptedFile = new File(fileName);
  86. File encryptedKey = new File(keyName);
  87. try
  88. {
  89. FileInputStream fisKey = new FileInputStream(encryptedKey);
  90. FileInputStream fisFile = new FileInputStream(encryptedFile);
  91. FileOutputStream fos = new FileOutputStream("decryptedFile.txt");
  92. byte[] fileArray = new byte[(int)encryptedFile.length()];
  93. int buffer2 = fisFile.read(fileArray);
  94. byte[] keyBytes = new byte[(int)encryptedKey.length()];
  95. int buffer = fisKey.read(keyBytes);
  96. SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
  97. Cipher cipher = Cipher.getInstance("AES");
  98. cipher.init(Cipher.DECRYPT_MODE, key);
  99. byte[] cipherBytes = cipher.doFinal(fileArray);
  100. fos.write(cipherBytes);
  101. fisKey.close();
  102. fos.close();
  103. fisFile.close();
  104. }
  105. catch(FileNotFoundException fnfe)
  106. {
  107. System.out.println("File Not Found");
  108. }
  109. catch(IOException ioe)
  110. {
  111. System.out.println("Signals that an I/O exception of some sort has occurred.");
  112. }
  113. catch(IllegalBlockSizeException ibse)
  114. {
  115. 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");
  116. }
  117. catch(NoSuchPaddingException nspe)
  118. {
  119. System.out.println("a particular padding mechanism is requested but is not available in the environment");
  120. }
  121. catch(NoSuchAlgorithmException nsae)
  122. {
  123. System.out.println("cryptographic algorithm is requested but is not available in the environment");
  124. }
  125. catch(InvalidKeyException ike)
  126. {
  127. System.out.println("the given key is inappropriate for initializing this cipher");
  128. }
  129. catch(NullPointerException npe)
  130. {
  131. System.out.println("specified algorithm is null");
  132. }
  133. catch(GeneralSecurityException gse)
  134. {
  135. System.out.println("bad or invalid padding/block");
  136. }
  137. }
  138. }