| 1 | package model.security; | |
| 2 | ||
| 3 | import javax.crypto.Cipher; | |
| 4 | import javax.crypto.SecretKey; | |
| 5 | import javax.crypto.spec.GCMParameterSpec; | |
| 6 | import java.nio.charset.StandardCharsets; | |
| 7 | import java.security.GeneralSecurityException; | |
| 8 | import java.security.SecureRandom; | |
| 9 | import java.util.Base64; | |
| 10 | ||
| 11 | public class CryptoUtils { | |
| 12 | ||
| 13 | private static final SecureRandom random = new SecureRandom(); | |
| 14 | private static final int IV_LENGTH = 12; | |
| 15 | private static final int TAG_LENGTH = 128; | |
| 16 | ||
| 17 | private CryptoUtils() { | |
| 18 | // static class | |
| 19 | } | |
| 20 | ||
| 21 | public static String encrypt(SecretKey key, String plaintext) throws GeneralSecurityException { | |
| 22 | byte[] iv = new byte[IV_LENGTH]; | |
| 23 |
1
1. encrypt : removed call to java/security/SecureRandom::nextBytes → KILLED |
random.nextBytes(iv); |
| 24 | ||
| 25 | Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); | |
| 26 |
1
1. encrypt : removed call to javax/crypto/Cipher::init → KILLED |
cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(TAG_LENGTH, iv)); |
| 27 | ||
| 28 | byte[] ciphertext = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8)); | |
| 29 |
1
1. encrypt : Replaced integer addition with subtraction → KILLED |
byte[] result = new byte[iv.length + ciphertext.length]; |
| 30 | ||
| 31 |
1
1. encrypt : removed call to java/lang/System::arraycopy → KILLED |
System.arraycopy(iv, 0, result, 0, iv.length); |
| 32 |
1
1. encrypt : removed call to java/lang/System::arraycopy → KILLED |
System.arraycopy(ciphertext, 0, result, iv.length, ciphertext.length); |
| 33 | ||
| 34 |
1
1. encrypt : replaced return value with "" for model/security/CryptoUtils::encrypt → KILLED |
return Base64.getEncoder().encodeToString(result); |
| 35 | } | |
| 36 | ||
| 37 | public static String decrypt(SecretKey key, String encoded) throws GeneralSecurityException { | |
| 38 | byte[] input = Base64.getDecoder().decode(encoded); | |
| 39 | ||
| 40 | byte[] iv = new byte[IV_LENGTH]; | |
| 41 |
1
1. decrypt : removed call to java/lang/System::arraycopy → KILLED |
System.arraycopy(input, 0, iv, 0, IV_LENGTH); |
| 42 | ||
| 43 |
1
1. decrypt : Replaced integer subtraction with addition → KILLED |
byte[] ciphertext = new byte[input.length - IV_LENGTH]; |
| 44 |
1
1. decrypt : removed call to java/lang/System::arraycopy → KILLED |
System.arraycopy(input, IV_LENGTH, ciphertext, 0, ciphertext.length); |
| 45 | ||
| 46 | Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); | |
| 47 |
1
1. decrypt : removed call to javax/crypto/Cipher::init → KILLED |
cipher.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(TAG_LENGTH, iv)); |
| 48 | ||
| 49 | byte[] plain = cipher.doFinal(ciphertext); | |
| 50 |
1
1. decrypt : replaced return value with "" for model/security/CryptoUtils::decrypt → KILLED |
return new String(plain, StandardCharsets.UTF_8); |
| 51 | } | |
| 52 | } | |
| 53 | ||
Mutations | ||
| 23 |
1.1 |
|
| 26 |
1.1 |
|
| 29 |
1.1 |
|
| 31 |
1.1 |
|
| 32 |
1.1 |
|
| 34 |
1.1 |
|
| 41 |
1.1 |
|
| 43 |
1.1 |
|
| 44 |
1.1 |
|
| 47 |
1.1 |
|
| 50 |
1.1 |