Skip to content

Commit 9fc10e3

Browse files
committed
AES encryption and decryption
1 parent 626d51c commit 9fc10e3

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed

EncryptionDecryption/.classpath

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
33
<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"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
55
<classpathentry kind="output" path="bin"/>
66
</classpath>
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
eclipse.preferences.version=1
22
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
44
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5-
org.eclipse.jdt.core.compiler.compliance=1.7
5+
org.eclipse.jdt.core.compiler.compliance=1.8
66
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
77
org.eclipse.jdt.core.compiler.debug.localVariable=generate
88
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
99
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
1010
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11-
org.eclipse.jdt.core.compiler.source=1.7
11+
org.eclipse.jdt.core.compiler.source=1.8

EncryptionDecryption/bin/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/com/
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.cdac.encrypt.aes;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.util.Arrays;
7+
import javax.crypto.Cipher;
8+
import javax.crypto.spec.SecretKeySpec;
9+
import java.util.Base64;
10+
11+
/*
12+
* import java.util.Base64; this class is available from java 1.8
13+
* else you can use apache library for Base64
14+
*/
15+
public class AESEncryption {
16+
17+
public static void main(String[] args) {
18+
final String strToEncrypt = "My text to encrypt";
19+
final String strPssword = "f2fc2007-b24b-4ab5-b62f-8dba873d0341";
20+
AESEncryption.setKey(strPssword);
21+
AESEncryption.encrypt(strToEncrypt.trim());
22+
System.out.println("String to Encrypt: " + strToEncrypt);
23+
System.out.println("Encrypted: " + AESEncryption.getEncryptedString());
24+
final String strToDecrypt = AESEncryption.getEncryptedString();
25+
AESEncryption.decrypt(strToDecrypt.trim());
26+
System.out.println("String To Decrypt : " + strToDecrypt);
27+
System.out.println("Decrypted : " + AESEncryption.getDecryptedString());
28+
}
29+
30+
private static SecretKeySpec secretKey;
31+
private static byte[] key;
32+
private static String decryptedString;
33+
private static String encryptedString;
34+
35+
public static void setKey(String myKey) {
36+
MessageDigest sha = null;
37+
try {
38+
key = myKey.getBytes("UTF-8");
39+
System.out.println(key.length);
40+
sha = MessageDigest.getInstance("SHA-256");
41+
key = sha.digest(key);
42+
//key = Arrays.copyOf(key, 32);
43+
System.out.println(key.length);
44+
System.out.println(new String(key, "UTF-8"));
45+
secretKey = new SecretKeySpec(key, "AES");
46+
} catch (NoSuchAlgorithmException e) {
47+
e.printStackTrace();
48+
} catch (UnsupportedEncodingException e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
53+
public static String getDecryptedString() {
54+
return decryptedString;
55+
}
56+
57+
public static void setDecryptedString(String decString) {
58+
decryptedString = decString;
59+
}
60+
61+
public static String getEncryptedString() {
62+
return encryptedString;
63+
}
64+
65+
public static void setEncryptedString(String encString) {
66+
encryptedString = encString;
67+
}
68+
69+
public static String encrypt(String strToEncrypt) {
70+
try {
71+
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
72+
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
73+
byte[] buff = cipher.doFinal(strToEncrypt.getBytes("UTF-8"));
74+
setEncryptedString(Base64.getEncoder().encodeToString(buff));
75+
} catch (Exception e) {
76+
System.out.println("Error while encrypting: " + e.toString());
77+
}
78+
return null;
79+
}
80+
81+
public static String decrypt(String strToDecrypt) {
82+
try {
83+
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
84+
cipher.init(Cipher.DECRYPT_MODE, secretKey);
85+
setDecryptedString(new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))));
86+
} catch (Exception e) {
87+
System.out.println("Error while decrypting: " + e.toString());
88+
}
89+
return null;
90+
}
91+
}

0 commit comments

Comments
 (0)