-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEncryption_AES.java
More file actions
151 lines (133 loc) · 6.2 KB
/
Encryption_AES.java
File metadata and controls
151 lines (133 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package Security;
import java.util.Random;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
class Encryption_AES {
/* Private variable declaration */
protected static final String SECRET_KEY = "jab_tak_hain_jaan_janey_jahan";
protected static final String SALTVALUE = "mein_nachungi";
/* Encryption Method */
public static String encrypt(String strToEncrypt)
{
try
{
/* Declare a byte array. */
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IvParameterSpec ivspec = new IvParameterSpec(iv);
/* Create factory for secret keys. */
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
/* PBEKeySpec class implements KeySpec interface. */
KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALTVALUE.getBytes(), 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
/* Retruns encrypted value. */
return Base64.getEncoder()
.encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8)));
}
catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e)
{
System.out.println("Error occured during encryption: " + e.toString());
}
return null;
}
public static String Data_splitting(String originalval)
{
String encryptedval = encrypt(originalval);
int min = (encryptedval.length() / 2) - 10;
int max = (encryptedval.length() / 2) + 10;
Random rand = new Random();
int splitIndex = rand.nextInt((max - min) + 1) + min;
System.out.println("Random number between " + min + " and " + max + ": " + splitIndex);
String firstHalf = encryptedval.substring(0, splitIndex);
String secondHalf = encryptedval.substring(splitIndex);
System.out.println("First half: " + firstHalf);
System.out.println("Second half: " + secondHalf);
String firsthalfencryptedval = encrypt(firstHalf);
System.out.println("First Half Encrypted value: " + firsthalfencryptedval);
String secondhalfencryptedval = encrypt(secondHalf);
System.out.println("Second Half Encrypted value: " + secondhalfencryptedval);
return (firsthalfencryptedval + secondhalfencryptedval);
}
}
class Decryption extends Encryption_AES{
public static String decrypt(String strToDecrypt)
{
try
{
/* Declare a byte array. */
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
IvParameterSpec ivspec = new IvParameterSpec(iv);
/* Create factory for secret keys. */
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
/* PBEKeySpec class implements KeySpec interface. */
KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALTVALUE.getBytes(), 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
/* Returns decrypted value. */
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
}
catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e)
{
System.out.println("Error occured during decryption: " + e.toString());
}
return null;
}
public static void data_merger(String firsthalfencryptedval,String secondhalfencryptedval){
String firsthalfdecryptedval = decrypt(firsthalfencryptedval);
System.out.println("First Half Decrypted value: " + firsthalfdecryptedval);
String secondhalfdecryptedval = decrypt(secondhalfencryptedval);
System.out.println("Second Half Decrypted value: " + secondhalfdecryptedval);
String fsHalf = firsthalfdecryptedval + secondhalfdecryptedval;
System.out.println(fsHalf);
// Call the decrypt() method and store result of decryption.
String decryptedval = decrypt(fsHalf);
System.out.println("Decrypted value: " + decryptedval);
}
}
class Main1 extends Decryption{
public static void main(String[] args)
{
System.out.println("Enter 1 to store Data");
System.out.println("Enter 2 to get your Data");
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
switch(n) {
case 1:
{
System.out.println("Enter the text");
Scanner sc1 = new Scanner(System.in);
String originalval = sc1.nextLine();
System.out.println(originalval);
Decryption obj = new Decryption();
System.out.println(obj.Data_splitting(originalval));
break;
}
case 2:{
System.out.println("Database required to fetch");
break;
}
default:{
System.out.println("INVALID Entry");
}
}
}
}