-
Notifications
You must be signed in to change notification settings - Fork 1
/
AES_Enc_Dec.java
62 lines (60 loc) · 2.39 KB
/
AES_Enc_Dec.java
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
package Encryption;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author theblackdevil
*/
public class AES_Enc_Dec {
private String plainText;
private String cipherText;
public void setPlainText(String plainText) {
this.plainText = plainText;
}
public String getPlainText() {
return this.plainText;
}
public String getCipherText() {
return this.cipherText;
}
public void setCipherText(String cipherText) {
this.cipherText = cipherText;
}
public void encryptAES() {
try {
byte[] plainTextBytes = plainText.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(Byte_Hex_Transform.hexStringToByteArray("YOUR_KEY_IN_HEX"), "AES");
Cipher encAEScipher = Cipher.getInstance("AES");
encAEScipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] cipherTextBytes = encAEScipher.doFinal(plainTextBytes);
this.cipherText=Byte_Hex_Transform.byteArrayToHexString(cipherTextBytes);
} catch (InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e){
System.out.println("don "+e);
}
}
public void decryptAES()
{
try {
byte [] cipherBytes =Byte_Hex_Transform.hexStringToByteArray(cipherText);
Key aesKey = new SecretKeySpec(Byte_Hex_Transform.hexStringToByteArray("YOUR_KEY_IN_HEX"), "AES");
Cipher encAEScipher = Cipher.getInstance("AES");
encAEScipher.init(Cipher.DECRYPT_MODE, aesKey);
byte[] plainTextBytes = encAEScipher.doFinal(cipherBytes);
System.out.println("The plainText is : "+new String(plainTextBytes));
} catch (InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
System.out.println("don: " + e);
}
}
}