|
| 1 | +package com.cdac.encrypt.file; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.UnsupportedEncodingException; |
| 8 | +import java.security.InvalidKeyException; |
| 9 | +import java.security.NoSuchAlgorithmException; |
| 10 | + |
| 11 | +import javax.crypto.BadPaddingException; |
| 12 | +import javax.crypto.Cipher; |
| 13 | +import javax.crypto.IllegalBlockSizeException; |
| 14 | +import javax.crypto.NoSuchPaddingException; |
| 15 | +import javax.crypto.spec.SecretKeySpec; |
| 16 | + |
| 17 | +public class EncryptFile { |
| 18 | + private SecretKeySpec secretKey; |
| 19 | + private Cipher cipher; |
| 20 | + |
| 21 | + public EncryptFile(String secret, int length, String algorithm) |
| 22 | + throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException { |
| 23 | + byte[] key = new byte[length]; |
| 24 | + key = fixSecret(secret, length); |
| 25 | + this.secretKey = new SecretKeySpec(key, algorithm); |
| 26 | + this.cipher = Cipher.getInstance(algorithm); |
| 27 | + } |
| 28 | + |
| 29 | + private byte[] fixSecret(String s, int length) throws UnsupportedEncodingException { |
| 30 | + if (s.length() < length) { |
| 31 | + int missingLength = length - s.length(); |
| 32 | + for (int i = 0; i < missingLength; i++) { |
| 33 | + s += " "; |
| 34 | + } |
| 35 | + } |
| 36 | + return s.substring(0, length).getBytes("UTF-8"); |
| 37 | + } |
| 38 | + |
| 39 | + public void encryptFile(File f) |
| 40 | + throws InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException { |
| 41 | + System.out.println("Encrypting file: " + f.getName()); |
| 42 | + this.cipher.init(Cipher.ENCRYPT_MODE, this.secretKey); |
| 43 | + this.writeToFile(f); |
| 44 | + } |
| 45 | + |
| 46 | + public void decryptFile(File f) |
| 47 | + throws InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException { |
| 48 | + System.out.println("Decrypting file: " + f.getName()); |
| 49 | + this.cipher.init(Cipher.DECRYPT_MODE, this.secretKey); |
| 50 | + this.writeToFile(f); |
| 51 | + } |
| 52 | + |
| 53 | + public void writeToFile(File f) throws IOException, IllegalBlockSizeException, BadPaddingException { |
| 54 | + FileInputStream in = new FileInputStream(f); |
| 55 | + byte[] input = new byte[(int) f.length()]; |
| 56 | + in.read(input); |
| 57 | + |
| 58 | + FileOutputStream out = new FileOutputStream(f); |
| 59 | + byte[] output = this.cipher.doFinal(input); |
| 60 | + out.write(output); |
| 61 | + |
| 62 | + out.flush(); |
| 63 | + out.close(); |
| 64 | + in.close(); |
| 65 | + } |
| 66 | + |
| 67 | + public static void main(String[] args) { |
| 68 | + EncryptFile instance; |
| 69 | + try { |
| 70 | + instance = new EncryptFile("!@#$MySecr3tPassw0rd", 16, "AES"); |
| 71 | + File file = new File("E:\\plain_text.txt"); |
| 72 | + |
| 73 | + try { |
| 74 | + instance.encryptFile(file); |
| 75 | + } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException |
| 76 | + | IOException e) { |
| 77 | + System.err.println("Couldn't encrypt " + file.getName() + ": " + e.getMessage()); |
| 78 | + } |
| 79 | + |
| 80 | +/* try { |
| 81 | + object.decryptFile(file); |
| 82 | + } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException |
| 83 | + | IOException e) { |
| 84 | + System.err.println("Couldn't decrypt " + file.getName() + ": " + e.getMessage()); |
| 85 | + }*/ |
| 86 | + } catch (UnsupportedEncodingException ex) { |
| 87 | + System.err.println("Couldn't create key: " + ex.getMessage()); |
| 88 | + } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { |
| 89 | + System.err.println(e.getMessage()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | +} |
0 commit comments