Skip to content

Commit 08cd532

Browse files
committed
Encryption and Decryption
1 parent 8e980e3 commit 08cd532

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

EncryptionDecryption/.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

EncryptionDecryption/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>EncryptionDecryption</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)