|
| 1 | +import java.io.*; |
| 2 | +import java.security.spec.AlgorithmParameterSpec; |
| 3 | +import java.security.spec.KeySpec; |
| 4 | +import javax.crypto.spec.*; |
| 5 | +//import java.security.Provider; |
| 6 | + |
| 7 | + |
| 8 | +import java.security.Provider; |
| 9 | +import java.security.Security; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.Iterator; |
| 14 | + |
| 15 | +import javax.crypto.*; |
| 16 | + |
| 17 | +public class DesEncrypter { |
| 18 | + private Cipher ecipher; |
| 19 | + private Cipher dcipher; |
| 20 | + |
| 21 | + // Create an 8-byte initialization vector |
| 22 | + byte[] iv = new byte[]{ |
| 23 | + (byte)0x8E, (byte)0x12, (byte)0x39, (byte)0x9C, |
| 24 | + (byte)0x07, (byte)0x72, (byte)0x6F, (byte)0x5A |
| 25 | + }; |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + DesEncrypter(String passPhrase, String ALG) { |
| 33 | + // Prepare the parameter to the ciphers |
| 34 | + int iterationCount = 19; // Iteration count |
| 35 | + AlgorithmParameterSpec paramSpec = new PBEParameterSpec(iv, iterationCount); |
| 36 | + |
| 37 | + |
| 38 | + try { |
| 39 | + // Create the key |
| 40 | + KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), iv, iterationCount); |
| 41 | + SecretKey key = SecretKeyFactory.getInstance(ALG).generateSecret(keySpec); |
| 42 | + |
| 43 | + |
| 44 | + ecipher = Cipher.getInstance(key.getAlgorithm()); |
| 45 | + dcipher = Cipher.getInstance(key.getAlgorithm()); |
| 46 | + |
| 47 | + |
| 48 | + // Create the ciphers |
| 49 | + ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); |
| 50 | + dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); |
| 51 | + |
| 52 | + |
| 53 | + } |
| 54 | + catch (java.security.InvalidAlgorithmParameterException e) { System.err.println(e); } |
| 55 | + catch (java.security.spec.InvalidKeySpecException e) { System.err.println(e); } |
| 56 | + catch (javax.crypto.NoSuchPaddingException e) { System.err.println(e); } |
| 57 | + catch (java.security.NoSuchAlgorithmException e) { System.err.println(e); } |
| 58 | + catch (java.security.InvalidKeyException e) { System.err.println(e); } |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /* |
| 63 | + * Create a key like: SecretKey key = KeyGenerator.getInstance("DES").generateKey(); |
| 64 | + * and pass the key to this constructor. |
| 65 | + */ |
| 66 | + /* |
| 67 | + DesEncrypter(SecretKey key) { |
| 68 | +
|
| 69 | + AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); |
| 70 | + try { |
| 71 | + ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); |
| 72 | + dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); |
| 73 | +
|
| 74 | + // CBC requires an initialization vector |
| 75 | + ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); |
| 76 | + dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); |
| 77 | + } |
| 78 | + catch (java.security.InvalidAlgorithmParameterException e) { System.err.println(e); } |
| 79 | + catch (javax.crypto.NoSuchPaddingException e) { System.err.println(e); } |
| 80 | + catch (java.security.NoSuchAlgorithmException e) { System.err.println(e); } |
| 81 | + catch (java.security.InvalidKeyException e) { System.err.println(e); } |
| 82 | + } |
| 83 | + */ |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + public void encrypt(InputStream in, OutputStream out) { |
| 89 | + byte[] buf = new byte[1024]; |
| 90 | + try { |
| 91 | + // Bytes written to out will be encrypted |
| 92 | + out = new CipherOutputStream(out, ecipher); |
| 93 | + |
| 94 | + // Read in the cleartext bytes and write to out to encrypt |
| 95 | + int numRead = 0; |
| 96 | + while ((numRead = in.read(buf)) >= 0) { |
| 97 | + out.write(buf, 0, numRead); |
| 98 | + } |
| 99 | + out.close(); |
| 100 | + } |
| 101 | + catch (java.io.IOException e) { System.err.println("encrypt: " + e);} |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + public void decrypt(InputStream in, OutputStream out) { |
| 107 | + byte[] buf = new byte[1024]; |
| 108 | + try { |
| 109 | + // Bytes read from in will be decrypted |
| 110 | + in = new CipherInputStream(in, dcipher); |
| 111 | + |
| 112 | + // Read in the decrypted bytes and write the cleartext to out |
| 113 | + int numRead = 0; |
| 114 | + while ((numRead = in.read(buf)) >= 0) { |
| 115 | + out.write(buf, 0, numRead); |
| 116 | + } |
| 117 | + out.close(); |
| 118 | + } |
| 119 | + catch (java.io.IOException e) { System.err.println("decrypt: " + e);} |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + // |
| 124 | + // This method is implemented in Listing All Available Cryptographic Services |
| 125 | + // |
| 126 | + private static void printCryptoImpls(String serviceType) { |
| 127 | + String[] names; |
| 128 | + Set<String> result = new HashSet<String>(); |
| 129 | + |
| 130 | + // All providers |
| 131 | + Provider[] providers = Security.getProviders(); |
| 132 | + for (int i=0; i<providers.length; i++) { |
| 133 | + |
| 134 | + // Get services provided by each provider |
| 135 | + Set keys = providers[i].keySet(); |
| 136 | + |
| 137 | + |
| 138 | + /*for (Iterator it=keys.iterator(); it.hasNext(); ) { |
| 139 | + String key = (String)it.next(); |
| 140 | + System.out.println(key); |
| 141 | + }*/ |
| 142 | + |
| 143 | + for (Iterator it=keys.iterator(); it.hasNext(); ) { |
| 144 | + |
| 145 | + String key = (String)it.next(); |
| 146 | + |
| 147 | + key = key.split(" ")[0]; |
| 148 | + |
| 149 | + if (key.startsWith(serviceType + ".")) { |
| 150 | + result.add(key.substring(serviceType.length() + 1)); |
| 151 | + } |
| 152 | + else if (key.startsWith("Alg.Alias." + serviceType + ".")) { |
| 153 | + // This is an alias |
| 154 | + result.add(key.substring(serviceType.length() + 11)); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + names = (String[])result.toArray(new String[result.size()]); |
| 160 | + for(int i=0; i < names.length; i++) |
| 161 | + System.out.println("\t"+names[i]); |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + public static void main(String [] args) { |
| 167 | + |
| 168 | + try { |
| 169 | + /* |
| 170 | + System.out.println("Crypto Implementations:"); |
| 171 | + printCryptoImpls("Cipher"); |
| 172 | + System.out.println("Key Generators:"); |
| 173 | + printCryptoImpls("KeyGenerator"); |
| 174 | + */ |
| 175 | + |
| 176 | + /* |
| 177 | + * Pass it either "PBEWithMD5AndDES" or "PBEWithMD5AndTripleDES" for DES or 3DES respectively |
| 178 | + */ |
| 179 | + DesEncrypter DES = new DesEncrypter("smilepass", "PBEWithMD5AndDES" ); |
| 180 | + DES.encrypt(new FileInputStream("smile.png"), new FileOutputStream("smile_encripted")); |
| 181 | + DES.decrypt(new FileInputStream("smile_encripted"), new FileOutputStream("smileDecripted.png")); |
| 182 | + |
| 183 | + DesEncrypter TDES = new DesEncrypter("smilepass", "PBEWithMD5AndTripleDES" ); |
| 184 | + TDES.encrypt(new FileInputStream("smile.png"), new FileOutputStream("smile_3DESencripted")); |
| 185 | + TDES.decrypt(new FileInputStream("smile_3DESencripted"), new FileOutputStream("smile3DESDecripted")); |
| 186 | + } |
| 187 | + catch (Exception e) { System.err.println(e); } |
| 188 | + } |
| 189 | +} |
0 commit comments