· 6 years ago · May 03, 2019, 10:44 PM
1import javax.crypto.Cipher;
2import javax.crypto.spec.IvParameterSpec;
3import javax.crypto.spec.SecretKeySpec;
4import java.util.Base64;
5import java.security.InvalidKeyException;
6import javax.crypto.NoSuchPaddingException;
7import java.security.NoSuchAlgorithmException;
8import javax.crypto.BadPaddingException;
9import javax.crypto.IllegalBlockSizeException;
10public class test
11{
12public byte[] main()
13 throws Exception {
14
15 String encryptionKeyString = "zxcvbnmiyjljgetu";
16 String originalMessage = "secret message";
17 byte[] encryptionKeyBytes = encryptionKeyString.getBytes();
18 byte[] messageBytes = originalMessage.getBytes();
19
20 byte[] encryptedMessageBytes = encryptMessage(messageBytes, encryptionKeyBytes);
21 return encryptedMessageBytes;
22 //cipher.init(Cipher.DECRYPT_MODE, secretKey);
23
24 //byte[] decryptedMessageBytes = cipher.doFinal(encryptedMessageBytes);
25 //assertThat(originalMessage).isEqualTo(new String(decryptedMessageBytes));
26
27}
28public byte[] encryptMessage(byte[] message, byte[] keyBytes)
29 throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException,
30 BadPaddingException, IllegalBlockSizeException {
31
32 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
33 SecretKeySpec Key = new SecretKeySpec(keyBytes, "AES");
34 cipher.init(Cipher.ENCRYPT_MODE, Key);
35 return cipher.doFinal(message);
36}
37}