· 7 years ago · Aug 31, 2018, 05:28 PM
1public static void main(String[] args) throws Exception {
2
3 String textA = "";
4 String textB="";
5
6 byte[] thisismykey = "Hello How manyBytes are in@hts A".getBytes();
7 SecretKey secKey = new SecretKeySpec(thisismykey, "AES");
8
9
10 Cipher aesCipher = Cipher.getInstance("AES");
11
12 //turn your original text to byte
13 byte[] myoriginaltexttobyte = "Your Plain Text Here".getBytes();
14 //activate the encrypt method
15 aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
16 //encrypt the text and assign the encrypted text to a byte array
17 byte[] bytecipheredoforgtext = aesCipher.doFinal(myoriginaltexttobyte);
18 //change it to string with new string
19 textA = new String(bytecipheredoforgtext);
20 System.out.println(textA);
21
22 //get the bytes of encrypted text and assign it to a byte array
23 byte[] byteofencryptedtext = textA.getBytes();
24 //activate the decrypt mode of the cipher
25 aesCipher.init(Cipher.DECRYPT_MODE, secKey);
26 //decrypt the encrypted text and assign it to a byte array
27 byte[] byteofencryptedbacktonormaltext = aesCipher.doFinal(byteofencryptedtext);
28 //change it to string with new string
29 textB = new String(byteofencryptedbacktonormaltext);
30 System.out.println(textB);
31}
32
33public class CipherFinalB {
34
35//from https://stackoverflow.com/questions/20796042/aes-encryption-and-decryption-with-java/20796446#20796446
36private final byte[] thisismykey;
37private final SecretKey secKey;
38private final Cipher aesCipher;
39public CipherFinalB() throws NoSuchPaddingException, NoSuchAlgorithmException {
40 thisismykey = "HellodHowfmanyBytesgarehin@htseA".getBytes();
41 secKey = new SecretKeySpec(thisismykey, "AES");
42
43 aesCipher = Cipher.getInstance("AES");
44
45}public String encrypt (String originaltext) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
46 byte[] myoriginaltexttobyte =originaltext.getBytes();
47 aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
48 byte[] bytecipheredoforgtext = aesCipher.doFinal(myoriginaltexttobyte);
49 String textA = new String(bytecipheredoforgtext);
50 System.out.println(textA);
51 return new String(bytecipheredoforgtext);
52}
53
54public String decrypt (String encryptedtext) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
55 byte[] byteofencryptedtext = encryptedtext.getBytes();
56 aesCipher.init(Cipher.DECRYPT_MODE, secKey);
57 byte[] byteofencryptedbacktonormaltext = aesCipher.doFinal(byteofencryptedtext);
58 return new String(byteofencryptedbacktonormaltext);
59}
60
61}
62
63Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
64at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:936)
65 at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
66 at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
67 at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2191)
68 at main.CipherFinalB.decrypt(CipherFinalB.java:66)
69 at main.CipherTest.main(CipherTest.java:16)