· 6 years ago · Apr 28, 2019, 06:54 PM
1package com.jvl.security;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.OutputStream;
10import java.io.UnsupportedEncodingException;
11import java.nio.charset.Charset;
12import java.security.InvalidAlgorithmParameterException;
13import java.security.InvalidKeyException;
14import java.security.NoSuchAlgorithmException;
15import java.security.spec.AlgorithmParameterSpec;
16import java.security.spec.InvalidKeySpecException;
17import java.security.spec.KeySpec;
18import javax.crypto.BadPaddingException;
19import javax.crypto.Cipher;
20import javax.crypto.IllegalBlockSizeException;
21import javax.crypto.NoSuchPaddingException;
22import javax.crypto.SecretKey;
23import javax.crypto.SecretKeyFactory;
24import javax.crypto.spec.PBEKeySpec;
25import javax.crypto.spec.PBEParameterSpec;
26import org.apache.commons.codec.binary.Base64;
27
28public final class DesEncrypter
29{
30 Cipher ecipher;
31 Cipher dcipher;
32 byte[] salt = { -87, -101, -56, 50, 86, 53, -29, 3 };
33 int iterationCount = 19;
34
35 public DesEncrypter(String passPhrase)
36 {
37 try
38 {
39 KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), this.salt, this.iterationCount);
40 SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
41
42 this.ecipher = Cipher.getInstance(key.getAlgorithm());
43 this.dcipher = Cipher.getInstance(key.getAlgorithm());
44
45 AlgorithmParameterSpec paramSpec = new PBEParameterSpec(this.salt, this.iterationCount);
46
47 this.ecipher.init(1, key, paramSpec);
48 this.dcipher.init(2, key, paramSpec);
49 }
50 catch (InvalidAlgorithmParameterException e) {}catch (InvalidKeySpecException e) {}catch (NoSuchPaddingException e) {}catch (NoSuchAlgorithmException e) {}catch (InvalidKeyException e) {}
51 }
52
53 public void encryptToFile(String fileToEncrypt, String encryptedFile)
54 throws FileNotFoundException, IOException
55 {
56 byte[] buf = new byte['?'];
57 File inputFile = new File(fileToEncrypt);
58 InputStream in = new FileInputStream(inputFile);
59 in.read(buf);
60 String stringToEncrypt = new String(buf);
61 in.close();
62
63 String encyptedString = encrypt(stringToEncrypt.trim());
64
65 File outputFile = new File(encryptedFile);
66 OutputStream out = new FileOutputStream(outputFile);
67 out.write(encyptedString.getBytes());
68 out.close();
69 }
70
71 public String encrypt(String stringToEncrypt)
72 {
73 try
74 {
75 byte[] utf8 = stringToEncrypt.getBytes("UTF8");
76
77 byte[] enc = this.ecipher.doFinal(utf8);
78
79 return new String(Base64.encodeBase64(enc), Charset.forName("US-ASCII"));
80 }
81 catch (BadPaddingException e) {}catch (IllegalBlockSizeException e) {}catch (UnsupportedEncodingException e) {}catch (IOException e) {}
82 return null;
83 }
84
85 public String decryptFileToString(String encryptedFile)
86 throws FileNotFoundException, IOException
87 {
88 byte[] buf = new byte['?'];
89 File inputFile = new File(encryptedFile);
90 InputStream in = new FileInputStream(inputFile);
91 in.read(buf);
92 String str = new String(buf);
93 String decryptedText = decrypt(str.trim());
94 return decryptedText;
95 }
96
97 public void decryptFile(String encryptedFile, String decryptedFile)
98 throws FileNotFoundException, IOException
99 {
100 byte[] buf = new byte['?'];
101 File inputFile = new File(encryptedFile);
102 InputStream in = new FileInputStream(inputFile);
103 in.read(buf);
104 String str = new String(buf);
105 String decryptedText = decrypt(str.trim());
106
107 File outputFile = new File(decryptedFile);
108 OutputStream out = new FileOutputStream(outputFile);
109 out.write(decryptedText.getBytes());
110 out.close();
111 }
112
113 public String decrypt(String encryptedString)
114 {
115 try
116 {
117 byte[] dec = Base64.decodeBase64(encryptedString.getBytes(Charset.forName("ASCII")));
118
119 byte[] utf8 = this.dcipher.doFinal(dec);
120
121 return new String(utf8, "UTF8");
122 }
123 catch (BadPaddingException e) {}catch (IllegalBlockSizeException e) {}catch (UnsupportedEncodingException e) {}catch (IOException e) {}
124 return null;
125 }
126}