├── README.md └── src └── encryption └── Encryption.java /README.md: -------------------------------------------------------------------------------- 1 | File-Encryption 2 | =============== 3 | 4 | File Encryption in Java with DES, DESede and other algorithms 5 | 6 | Encrypt file with Key File Generated with Random Key. 7 | -------------------------------------------------------------------------------- /src/encryption/Encryption.java: -------------------------------------------------------------------------------- 1 | package encryption; 2 | 3 | import java.io.*; 4 | import java.security.spec.AlgorithmParameterSpec; 5 | import java.security.spec.KeySpec; 6 | import java.util.Scanner; 7 | import javax.crypto.*; 8 | import javax.crypto.spec.*; 9 | 10 | /** 11 | * @author Jafaripur 12 | * email's mjafaripur@yahoo.Com 13 | * @version 1.0.0 14 | */ 15 | public class Encryption { 16 | 17 | /** 18 | * @param args the command line arguments 19 | */ 20 | 21 | public static void main(String[] args) throws Exception { 22 | Scanner sc = new Scanner(System.in); 23 | int input; 24 | String fileName; 25 | String keyFileName; 26 | boolean running = true; 27 | while (running) 28 | { 29 | System.out.println("1 - Write New Key"); 30 | System.out.println("2 - Encrypt"); 31 | System.out.println("3 - Decrypt"); 32 | System.out.println("4 - Exit"); 33 | System.out.println("Select Option Number : "); 34 | input = sc.nextInt(); 35 | switch (input) 36 | { 37 | case 1: 38 | { 39 | System.out.println("Enter Key File Name : "); 40 | keyFileName = sc.next(); 41 | writeKey(56,keyFileName,"DES"); 42 | break; 43 | } 44 | case 2: 45 | { 46 | System.out.println("Enter Key File Name : "); 47 | keyFileName = sc.next(); 48 | System.out.println("Enter File Name To Encrypt : "); 49 | fileName = sc.next(); 50 | encryptFile(fileName , keyFileName); 51 | break; 52 | } 53 | case 3: 54 | { 55 | System.out.println("Enter Key File Name : "); 56 | keyFileName = sc.next(); 57 | System.out.println("Enter File Name To Decrypt : "); 58 | fileName = sc.next(); 59 | decryptFile(fileName, keyFileName); 60 | break; 61 | } 62 | case 4: 63 | running = false; 64 | } 65 | System.out.println("*******************************************"); 66 | } 67 | } 68 | private static void encryptFile(String fileName, String keyName) 69 | { 70 | String original = fileName ; 71 | String encrypted = "encrypted_" + fileName ; 72 | Cipher encrypt; 73 | byte[] initialization_vector = { 22, 33, 11, 44, 55, 99, 66, 77 }; 74 | try 75 | { 76 | SecretKey secret_key = readKey(keyName, "DES"); 77 | AlgorithmParameterSpec alogrithm_specs = new IvParameterSpec(initialization_vector); 78 | encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding"); 79 | encrypt.init(Cipher.ENCRYPT_MODE, secret_key, alogrithm_specs); 80 | encrypt(new FileInputStream(original), new FileOutputStream(encrypted),encrypt); 81 | System.out.println("End of Encryption procedure!"); 82 | } 83 | catch(Exception e) 84 | { 85 | System.out.println(e.toString()); 86 | } 87 | } 88 | private static void decryptFile(String fileName, String keyName) 89 | { 90 | String original = "decrypted_" + fileName; 91 | String encrypted = fileName ; 92 | Cipher decrypt; 93 | byte[] initialization_vector = { 22, 33, 11, 44, 55, 99, 66, 77 }; 94 | try 95 | { 96 | SecretKey secret_key = readKey(keyName, "DES"); 97 | AlgorithmParameterSpec alogrithm_specs = new IvParameterSpec(initialization_vector); 98 | 99 | decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding"); 100 | decrypt.init(Cipher.DECRYPT_MODE, secret_key, alogrithm_specs); 101 | decrypt(new FileInputStream(encrypted), new FileOutputStream(original),decrypt); 102 | System.out.println("End of Decryption procedure!"); 103 | } 104 | catch(Exception e) 105 | { 106 | System.out.println(e.toString()); 107 | } 108 | } 109 | 110 | private static void encrypt(InputStream input, OutputStream output,Cipher encrypt) throws IOException { 111 | output = new CipherOutputStream(output, encrypt); 112 | writeBytes(input, output); 113 | } 114 | 115 | private static void decrypt(InputStream input, OutputStream output,Cipher decrypt) throws IOException { 116 | 117 | input = new CipherInputStream(input, decrypt); 118 | writeBytes(input, output); 119 | } 120 | private static void writeBytes(InputStream input, OutputStream output) throws IOException { 121 | byte[] writeBuffer = new byte[1024]; 122 | int readBytes = 0; 123 | while ((readBytes = input.read(writeBuffer)) >= 0) { 124 | output.write(writeBuffer, 0, readBytes); 125 | } 126 | output.close(); 127 | input.close(); 128 | } 129 | private static void writeKey(int keySize, String output,String algorithm) throws Exception { 130 | KeyGenerator kg = KeyGenerator.getInstance(algorithm); 131 | kg.init(keySize); 132 | System.out.println(); 133 | System.out.println("KeyGenerator Object Info: "); 134 | System.out.println("Algorithm = " + kg.getAlgorithm()); 135 | System.out.println("Provider = " + kg.getProvider()); 136 | System.out.println("Key Size = " + keySize); 137 | System.out.println("toString = " + kg.toString()); 138 | 139 | SecretKey ky = kg.generateKey(); 140 | byte[] kb; 141 | try (FileOutputStream fos = new FileOutputStream(output)) { 142 | kb = ky.getEncoded(); 143 | fos.write(kb); 144 | } 145 | System.out.println(); 146 | System.out.println("SecretKey Object Info: "); 147 | System.out.println("Algorithm = " + ky.getAlgorithm()); 148 | System.out.println("Saved File = " + output); 149 | System.out.println("Size = " + kb.length); 150 | System.out.println("Format = " + ky.getFormat()); 151 | System.out.println("toString = " + ky.toString()); 152 | } 153 | private static SecretKey readKey(String input, String algorithm)throws Exception { 154 | FileInputStream fis = new FileInputStream(input); 155 | int kl = fis.available(); 156 | byte[] kb = new byte[kl]; 157 | fis.read(kb); 158 | fis.close(); 159 | KeySpec ks = null; 160 | SecretKey ky = null; 161 | SecretKeyFactory kf = null; 162 | if (algorithm.equalsIgnoreCase("DES")) { 163 | ks = new DESKeySpec(kb); 164 | kf = SecretKeyFactory.getInstance("DES"); 165 | ky = kf.generateSecret(ks); 166 | } else if (algorithm.equalsIgnoreCase("DESede")) { 167 | ks = new DESedeKeySpec(kb); 168 | kf = SecretKeyFactory.getInstance("DESede"); 169 | ky = kf.generateSecret(ks); 170 | } else { 171 | ks = new SecretKeySpec(kb, algorithm); 172 | ky = new SecretKeySpec(kb, algorithm); 173 | } 174 | /* 175 | System.out.println(); 176 | System.out.println("KeySpec Object Info: "); 177 | System.out.println("Saved File = " + fl); 178 | System.out.println("Length = " + kb.length); 179 | System.out.println("toString = " + ks.toString()); 180 | 181 | System.out.println(); 182 | System.out.println("SecretKey Object Info: "); 183 | System.out.println("Algorithm = " + ky.getAlgorithm()); 184 | System.out.println("toString = " + ky.toString()); 185 | */ 186 | return ky; 187 | } 188 | } 189 | --------------------------------------------------------------------------------