· 7 years ago · Mar 14, 2018, 11:24 AM
1private final byte[] key;
2
3private static final String ALGORITHM = "AES";
4
5public AES_Algorithm(byte[] key)
6{
7 this.key = key;
8}
9
10/**
11 * Encrypts the given plain text
12 *
13 * @param plainText The plain text to encrypt
14 * @return
15 * @throws java.lang.Exception
16 */
17public byte[] encrypt(byte[] plainText) throws Exception
18{
19 SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
20 Cipher cipher = Cipher.getInstance(ALGORITHM);
21 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
22
23 return cipher.doFinal(plainText);
24}
25
26/**
27 * Decrypts the given byte array
28 *
29 * @param cipherText The data to decrypt
30 * @return
31 * @throws java.lang.Exception
32 */
33public byte[] decrypt(byte[] cipherText) throws Exception
34{
35 SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
36 Cipher cipher = Cipher.getInstance(ALGORITHM);
37 cipher.init(Cipher.DECRYPT_MODE, secretKey);
38
39 return cipher.doFinal(cipherText);
40}
41
42 public static String toBinary( byte[] bytes )
43{
44 StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE);
45 for( int i = 0; i < Byte.SIZE * bytes.length; i++ )
46 sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1');
47 return sb.toString();
48}
49
50public static void main(String[] args) throws Exception
51{
52 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
53
54 System.out.println("Enter your input text");
55 String input_text=br.readLine();
56
57 System.out.println("Enter your key");
58 String key_plaintext=br.readLine();
59
60
61 byte[] plainText = input_text.getBytes(StandardCharsets.UTF_8);
62 byte[] encryptionKey = key_plaintext.getBytes(StandardCharsets.UTF_8);
63
64 AES_Algorithm aes = new AES_Algorithm(encryptionKey);
65 byte[] cipherText = aes.encrypt(plainText);
66 System.out.println("your cipher in ascii = "+new String(cipherText));
67 System.out.println("encrypted string HEX: " + DatatypeConverter.printHexBinary(cipherText));
68 System.out.println("encrypted string Binary: " + toBinary(cipherText));
69
70 ////////////////////////////////
71
72 Scanner scan = new Scanner(System.in);
73 System.out.print("Enter your ciphertext in Binary :");
74 String ciphertext=scan.next();
75
76 //input type hex
77 //byte[] cipher_hex = DatatypeConverter.parseHexBinary(ciphertext);
78
79 //input type binary
80 byte[] cipher_binary = new BigInteger(ciphertext.trim(), 2).toByteArray();
81
82 String resulted = DatatypeConverter.printHexBinary(cipher_binary).trim(); //trim hilangkan 00
83
84 System.out.println("Your ciphertext in Hex : "+resulted.trim());
85
86 System.out.println("Enter your key :");
87 String key_ciphertext=br.readLine();
88
89 byte[] decription_key = key_ciphertext.getBytes(StandardCharsets.UTF_8);
90
91 AES_Algorithm aes_dec = new AES_Algorithm(decription_key);
92
93 byte[] decryptedCipherText = aes_dec.decrypt(cipher_binary);
94
95 System.out.println("Your Decrypted text = "+new String(decryptedCipherText));
96
97}