· 9 years ago · Dec 22, 2016, 03:22 PM
1java - javax.crypto.BadPaddingException: Given final block not properly padded
2
3import javax.crypto.BadPaddingException;
4import javax.crypto.IllegalBlockSizeException;
5import javax.crypto.NoSuchPaddingException;
6import java.io.File;
7import java.io.IOException;
8import java.security.InvalidAlgorithmParameterException;
9import java.security.InvalidKeyException;
10import java.security.NoSuchAlgorithmException;
11
12/**
13 * Created by Ashish Pancholi on 22-12-2016.
14 */
15public class EncryDecryPtion implements Securable{
16 public static void main(String[] argu){
17 EncryDecryPtion encryDecryPtion = new EncryDecryPtion();
18 File file = new File("shouldbeoriginal.jpg");
19 try {
20 encryDecryPtion.encryptFile(file,"Pa$$w0rd");
21 } catch (IOException e) {
22 e.printStackTrace();
23 } catch (NoSuchAlgorithmException e) {
24 e.printStackTrace();
25 } catch (IllegalBlockSizeException e) {
26 e.printStackTrace();
27 } catch (InvalidKeyException e) {
28 e.printStackTrace();
29 } catch (BadPaddingException e) {
30 e.printStackTrace();
31 } catch (NoSuchPaddingException e) {
32 e.printStackTrace();
33 }
34
35 File file_ = new File("shouldbeoriginal.jpg");
36 try {
37 encryDecryPtion.decryptFile(file_,"Pa$$w0rd");
38 } catch (IOException e) {
39 e.printStackTrace();
40 } catch (NoSuchAlgorithmException e) {
41 e.printStackTrace();
42 } catch (IllegalBlockSizeException e) {
43 e.printStackTrace();
44 } catch (InvalidKeyException e) {
45 e.printStackTrace();
46 } catch (BadPaddingException e) {
47 e.printStackTrace();
48 } catch (NoSuchPaddingException e) {
49 e.printStackTrace();
50 }
51 }
52}
53
54
55
56
57import org.apache.commons.io.FileUtils;
58import org.apache.commons.io.IOUtils;
59import javax.crypto.BadPaddingException;
60import javax.crypto.IllegalBlockSizeException;
61import javax.crypto.NoSuchPaddingException;
62import java.io.*;
63import java.nio.file.Files;
64import java.nio.file.Path;
65import java.nio.file.Paths;
66import java.security.InvalidKeyException;
67import java.security.NoSuchAlgorithmException;
68
69/**
70 * Encrypt and decrypt file with AES algorithm
71 * Created by Ashish Pancholi on 20-12-2016.
72 */
73public interface Securable {
74
75 /**
76 * Read and write the file in chunk.
77 * Encrypts the chunks with AES algorithm.
78 * It creates a new a file which having encrypted data,
79 * deletes old original file and
80 * rename a new file with the old file
81 * @param file which is to be encrypted and password.
82 */
83
84 default File encryptFile(File file, String password) throws IOException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
85 AESEncryptionDecryption aesEncryptionDecryption = new AESEncryptionDecryption(password);
86 String encryptedFilePath = file.getAbsolutePath() + ".ENCRYPTED";
87 File encryptedFile = new File(encryptedFilePath);
88 encryptedFile.createNewFile();
89 try
90 (FileInputStream in = new FileInputStream(file)) {
91
92 try
93 (OutputStream out = new FileOutputStream(encryptedFile)) {
94 byte[] chunk = new byte[1024];
95 int chunkLen = 0;
96 while ((chunkLen = in.read(chunk)) != -1) {
97 byte[] encryptedChunk = aesEncryptionDecryption.encrypt(chunk);
98 out.write(encryptedChunk);
99 }
100 }
101 }
102 Path path_originalFile = Paths.get(file.getAbsolutePath());
103 Path path_encryptedFile = Paths.get(encryptedFile.getAbsolutePath());
104 try {
105 Files.delete(path_originalFile);
106 }catch (IOException ex){
107 try {
108 FileUtils.forceDelete(file);
109 }catch (IOException ex1){
110 //ignore
111 }
112 }
113 Path path = Files.move(path_encryptedFile, path_originalFile);
114 return path.toFile();
115 }
116
117 default File encryptWholeFile(File file, String password) throws IOException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
118 AESEncryptionDecryption aesEncryptionDecryption = new AESEncryptionDecryption(password);
119 String encryptedFilePath = file.getAbsolutePath() + ".ENCRYPTED";
120 File encryptedFile = new File(encryptedFilePath);
121 encryptedFile.createNewFile();
122 try(FileInputStream in = new FileInputStream(file)) {
123 byte[] bytes = IOUtils.toByteArray(in);
124 byte[] encryptedChunk = aesEncryptionDecryption.encrypt(bytes);
125 FileUtils.writeByteArrayToFile(encryptedFile, encryptedChunk);
126 }
127 Path path_originalFile = Paths.get(file.getAbsolutePath());
128 Path path_encryptedFile = Paths.get(encryptedFile.getAbsolutePath());
129 try {
130 Files.delete(path_originalFile);
131 }catch (IOException ex){
132 try {
133 FileUtils.forceDelete(file);
134 }catch (IOException ex1){
135 //ignore
136 }
137 }
138 Path path = Files.move(path_encryptedFile, path_originalFile);
139 return path.toFile();
140 }
141
142 /**
143 * Read and write the file in chunk.
144 * Encrypts the chunks with AES algorithm.
145 * It creates a new a file which having encrypted data,
146 * deletes old original file and
147 * rename a new file with the old file
148 * @param inputStream of file which is to be encrypted and a password.
149 */
150 default InputStream encryptFile(InputStream inputStream, String password) throws IOException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
151 InputStream in;
152 try {
153 AESEncryptionDecryption aesEncryptionDecryption = new AESEncryptionDecryption(password);
154 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
155 byte[] chunk = new byte[1024];
156 int chunkLen = 0;
157 while ((chunkLen = inputStream.read(chunk)) != -1) {
158 byte[] encryptedChunk = aesEncryptionDecryption.encrypt(chunk);
159 baos.write(encryptedChunk);
160 }
161 baos.flush();
162 in = new ByteArrayInputStream(baos.toByteArray());
163 }
164 }finally {
165 inputStream.close();
166 }
167 return in;
168 }
169
170 /**
171 * Read and write the file in chunk.
172 * Encrypts the chunks with AES algorithm.
173 * It creates a new a file which having encrypted data,
174 * deletes old original file and
175 * rename a new file with the old file
176 * @param inputStream of file which is to be encrypted and a password.
177 */
178 default File encryptFile(InputStream inputStream, String password, String targetFileName) throws IOException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
179 File encryptedFile = new File(targetFileName);
180 try {
181 AESEncryptionDecryption aesEncryptionDecryption = new AESEncryptionDecryption(password);
182 encryptedFile.getParentFile().mkdirs();
183 encryptedFile.createNewFile();
184 try (OutputStream baos = new FileOutputStream(encryptedFile)) {
185 byte[] chunk = new byte[1024];
186 int chunkLen = 0;
187 while ((chunkLen = inputStream.read(chunk)) != -1) {
188 byte[] encryptedChunk = aesEncryptionDecryption.encrypt(chunk);
189 baos.write(encryptedChunk);
190 }
191 }
192 }finally {
193 inputStream.close();
194 }
195 return encryptedFile;
196
197 }
198
199 /**
200 * Read and write the file in chunk.
201 * Decrypts the chunks with AES algorithm.
202 * It creates a new a file which having decrypted data,
203 * deletes old original encrypted file and
204 * rename a new file with the old file
205 * @param file which is to be decrypted and password.
206 */
207
208 default void decryptFile(File file, String password) throws IOException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
209 AESEncryptionDecryption aesEncryptionDecryption = new AESEncryptionDecryption(password);
210 String decryptedFilePath = file.getAbsolutePath() + ".DECRYPTED";
211 File decryptedFile = new File(decryptedFilePath);
212 decryptedFile.createNewFile();
213 try
214 (FileInputStream in = new FileInputStream(file)) {
215
216 try
217 (OutputStream out = new FileOutputStream(decryptedFile)) {
218 byte[] chunk = new byte[1024];
219 int chunkLen = 0;
220 while ((chunkLen = in.read(chunk)) != -1) {
221 byte[] encryptedChunk = aesEncryptionDecryption.decrypt(chunk);
222 out.write(encryptedChunk);
223 }
224 }
225 }
226 Path path_originalFile = Paths.get(file.getAbsolutePath());
227 Path path_decryptedFile = Paths.get(decryptedFile.getAbsolutePath());
228 try {
229 Files.delete(path_originalFile);
230 }catch (IOException ex){
231 try {
232 FileUtils.forceDelete(file);
233 }catch (IOException ex1){
234 //ignore
235 }
236 }
237 Files.move(path_decryptedFile, path_originalFile);
238 }
239
240 default File decryptWholeFile(File file, String password) throws IOException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
241 AESEncryptionDecryption aesEncryptionDecryption = new AESEncryptionDecryption(password);
242 String decryptedFilePath = file.getAbsolutePath() + ".DECRYPTED";
243 File decryptedFile = new File(decryptedFilePath);
244 decryptedFile.createNewFile();
245 try(FileInputStream in = new FileInputStream(file)) {
246 byte[] bytes = IOUtils.toByteArray(in);
247 byte[] encryptedChunk = aesEncryptionDecryption.decrypt(bytes);
248 FileUtils.writeByteArrayToFile(decryptedFile, encryptedChunk);
249 }
250 Path path_originalFile = Paths.get(file.getAbsolutePath());
251 Path path_decryptedFile = Paths.get(decryptedFile.getAbsolutePath());
252 try {
253 Files.delete(path_originalFile);
254 }catch (IOException ex){
255 try {
256 FileUtils.forceDelete(file);
257 }catch (IOException ex1){
258 //ignore
259 }
260 }
261 Path path = Files.move(path_decryptedFile, path_originalFile);
262 return path.toFile();
263 }
264
265}
266
267
268import org.apache.commons.io.FileUtils;
269import java.io.File;
270import java.io.UnsupportedEncodingException;
271import java.nio.file.Files;
272import java.nio.file.Path;
273import java.nio.file.Paths;
274import java.security.InvalidKeyException;
275import java.security.MessageDigest;
276import java.security.NoSuchAlgorithmException;
277import java.util.Arrays;
278import javax.crypto.BadPaddingException;
279import javax.crypto.Cipher;
280import javax.crypto.IllegalBlockSizeException;
281import javax.crypto.NoSuchPaddingException;
282import javax.crypto.spec.SecretKeySpec;
283
284
285/**
286 * Encrypt and decrypt file with AES algorithm
287 * Created by Ashish Pancholi on 20-12-2016.
288 */
289
290public class AESEncryptionDecryption {
291
292 private SecretKeySpec secretKey;
293 private byte[] key;
294
295 public AESEncryptionDecryption(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{
296 MessageDigest sha = null;
297 key = password.getBytes("UTF-8");
298 sha = MessageDigest.getInstance("SHA-1");
299 key = sha.digest(key);
300 key = Arrays.copyOf(key, 16); // use only first 128 bit
301 this.secretKey = new SecretKeySpec(key, "AES");
302 }
303
304 /**
305 * Encrypts the file with AES algorithm
306 * @param bytes of file which is to encrypted
307 * @return byte[] which is encrypted bytes
308 */
309 public byte[] encrypt(byte[] bytes) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
310 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
311 cipher.init(Cipher.ENCRYPT_MODE, this.secretKey);
312 byte[] encrytedBytes = cipher.doFinal(bytes);
313 return encrytedBytes;
314
315 }
316
317 /**
318 * Decrypts the file with AES algorithm
319 * @param encrytedBytes of file that to be decrypted
320 * @return byte[] which is original data.
321 */
322 public byte[] decrypt(byte[] encrytedBytes) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException
323 {
324 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
325 cipher.init(Cipher.DECRYPT_MODE, this.secretKey);
326 byte[] bytes = cipher.doFinal(encrytedBytes);
327 return bytes;
328 }
329}