-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageDecryption.java
More file actions
47 lines (38 loc) · 1.71 KB
/
MessageDecryption.java
File metadata and controls
47 lines (38 loc) · 1.71 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
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
public class MessageDecryption {
private String message;
public MessageDecryption(String message, Key key,String type) {
try {
byte[] cipherText = new byte[message.length()];
char[] carr = message.toCharArray();
for (int i = 0; i < message.length(); i++) {
cipherText[i] = (byte) carr[i];
}
// Creates the Cipher object (specifying the algorithm, mode, and padding)
Cipher cipher = Cipher.getInstance(type+"/ECB/PKCS5Padding");
// decrypt the ciphertext using the same key
System.out.println("\nStart decryption");
final long startTime = System.nanoTime();
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] newPlainText = cipher.doFinal(cipherText);
final long duration = System.nanoTime() - startTime;
System.out.println("Finish decryption: ");
//System.out.println(new String(newPlainText, StandardCharsets.UTF_8));
this.message = new String(newPlainText, StandardCharsets.UTF_8);
System.out.println("It took " + duration + " nanosecond to decrypt the message " + this.message);
System.out.println("Message length is " + this.message.length());
} catch (Exception e) {
e.printStackTrace();
}
}
public String getMessage() {
return message;
}
}