|
3 | 3 | import android.util.Base64; |
4 | 4 |
|
5 | 5 | import java.io.UnsupportedEncodingException; |
| 6 | +import java.nio.charset.StandardCharsets; |
6 | 7 | import java.security.InvalidAlgorithmParameterException; |
7 | 8 | import java.security.InvalidKeyException; |
8 | 9 | import java.security.NoSuchAlgorithmException; |
9 | 10 | import java.security.spec.InvalidKeySpecException; |
| 11 | +import java.security.SecureRandom; |
| 12 | +import java.nio.ByteBuffer; |
10 | 13 | import java.security.spec.InvalidParameterSpecException; |
11 | 14 |
|
12 | 15 | import javax.crypto.BadPaddingException; |
|
17 | 20 | import javax.crypto.SecretKeyFactory; |
18 | 21 | import javax.crypto.spec.PBEKeySpec; |
19 | 22 | import javax.crypto.spec.SecretKeySpec; |
| 23 | +import javax.crypto.spec.GCMParameterSpec; |
20 | 24 |
|
21 | 25 | public class Cryptography { |
22 | 26 | public static String encryptMsg(String message, SecretKey secret) |
23 | | - throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { |
24 | | - Cipher cipher = null; |
25 | | - cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); |
26 | | - cipher.init(Cipher.ENCRYPT_MODE, secret); |
27 | | - byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8")); |
28 | | - return Base64.encodeToString(cipherText, Base64.NO_WRAP); |
| 27 | + throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { |
| 28 | + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); |
| 29 | + byte[] iv = new byte[12]; // Ensure this is randomly generated for each encryption. |
| 30 | + new SecureRandom().nextBytes(iv); |
| 31 | + GCMParameterSpec spec = new GCMParameterSpec(128, iv); |
| 32 | + cipher.init(Cipher.ENCRYPT_MODE, secret, spec); |
| 33 | + byte[] cipherText = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8)); |
| 34 | + ByteBuffer byteBuffer = ByteBuffer.allocate(iv.length + cipherText.length); |
| 35 | + byteBuffer.put(iv); |
| 36 | + byteBuffer.put(cipherText); |
| 37 | + return Base64.encodeToString(byteBuffer.array(), Base64.NO_WRAP); |
29 | 38 | } |
30 | 39 |
|
31 | 40 | public static String decryptMsg(String cipherText, SecretKey secret) |
32 | | - throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException { |
33 | | - Cipher cipher = null; |
34 | | - cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); |
35 | | - cipher.init(Cipher.DECRYPT_MODE, secret); |
36 | | - byte[] decode = Base64.decode(cipherText, Base64.NO_WRAP); |
37 | | - String decryptString = new String(cipher.doFinal(decode), "UTF-8"); |
| 41 | + throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { |
| 42 | + ByteBuffer byteBuffer = ByteBuffer.wrap(Base64.decode(cipherText, Base64.NO_WRAP)); |
| 43 | + byte[] iv = new byte[12]; |
| 44 | + byteBuffer.get(iv); |
| 45 | + byte[] cipherBytes = new byte[byteBuffer.remaining()]; |
| 46 | + byteBuffer.get(cipherBytes); |
| 47 | + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); |
| 48 | + GCMParameterSpec spec = new GCMParameterSpec(128, iv); |
| 49 | + cipher.init(Cipher.DECRYPT_MODE, secret, spec); |
| 50 | + String decryptString = new String(cipher.doFinal(cipherBytes), StandardCharsets.UTF_8); |
38 | 51 | return decryptString; |
39 | 52 | } |
40 | 53 |
|
|
0 commit comments