Last active
February 26, 2023 08:24
-
-
Save ankuryadav7/fa377c465a707513a371a2e68327df4e to your computer and use it in GitHub Desktop.
Updated CipherUtil class to avoid Unsafe cipher mode Error/Warning on playstore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Base64; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import java.nio.charset.StandardCharsets; | |
import java.security.MessageDigest; | |
import java.util.Arrays; | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
public class CipherUtil { | |
private static CipherUtil cipherUtil; | |
private static SecretKeySpec secretKey; | |
IvParameterSpec ivSpec = new IvParameterSpec(new byte[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}); | |
private static final String ENCODING_METHOD = "AES/GCM/NoPadding"; | |
private static final String ALGORITHM = "AES"; | |
@NonNull | |
public static CipherUtil getInstance() { | |
if (cipherUtil == null) { | |
cipherUtil = new CipherUtil(); | |
} | |
return cipherUtil; | |
} | |
public static void setKey(@NonNull String myKey) { | |
MessageDigest sha; | |
try { | |
byte[] key = myKey.getBytes(StandardCharsets.UTF_8); | |
sha = MessageDigest.getInstance("SHA-1"); | |
key = sha.digest(key); | |
key = Arrays.copyOf(key, 16); | |
secretKey = new SecretKeySpec(key, ALGORITHM); | |
} catch (Exception e) { | |
//Logger.w(e); | |
} | |
} | |
@Nullable | |
public String encrypt(@NonNull String strToEncrypt, @NonNull String secret) { | |
try { | |
setKey(secret); | |
Cipher cipher = Cipher.getInstance(ENCODING_METHOD); | |
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec); | |
return Base64.encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8)), 1); | |
} catch (Exception e) { | |
//Logger.e("Error while encrypt: " + e); | |
} | |
return null; | |
} | |
@Nullable | |
public String decrypt(@NonNull String strToDecrypt, @NonNull String secret) { | |
try { | |
setKey(secret); | |
Cipher cipher = Cipher.getInstance(ENCODING_METHOD); | |
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec); | |
return new String(cipher.doFinal(Base64.decode(strToDecrypt, 1))); | |
} catch (Exception e) { | |
//Logger.e("Error while decrypting: " + e); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This class has been designed to avoid above Error from playstore