Skip to content

Instantly share code, notes, and snippets.

String encryptedStr = readFromAsset(context, BuildConfig.ENC_FILE);
String decryptedData = getDecryptedString(responseStr);
public String readFromAsset(Context context, String fileName) {
String json = null;
try {
InputStream is = context.getAssets().open(fileName);
int size = is.available(); byte[] buffer = new byte[size];
is.read(buffer);
is.close();
dependencies {
...
implementation 'com.github.simbiose:Encryption:2.0.1'
...
}
afterEvaluate {
android.productFlavors.all {
flavor ->
tasks."generate${flavor.name.capitalize()}DebugResources".dependsOn "encrypt${flavor.name.capitalize()}DebugConfig"
tasks."generate${flavor.name.capitalize()}ReleaseResources".dependsOn "encrypt${flavor.name.capitalize()}ReleaseConfig"
}
}
def taskFlavorTypes = ["flavor1", "flavor2"] //flavors defined for apk
def taskVariant = ["debug", "release"] //variant defined for apk
// generates gradle task dynamically which can be mapped for each build flavor and variant combination defined in above variables
// for eg. tasks will be declared as "encryptFlavor1DebugConfig", "encryptFlavor1ReleaseConfig", "encryptFlavor2DebugConfig", "encryptFlavor2ReleaseConfig"
taskFlavorTypes.each { buildFlavor ->
taskVariant.each { buildVariant ->
task "encrypt${buildFlavor.capitalize()}${buildVariant.capitalize()}Config" {
outputs.upToDateWhen { false }
ext.encryptfile = { buildFlavor, buildVariant ->
def fileToBeEncryptedName = 'config.txt' //name of your file to be encrypted
def folder = 'confidential-file' // folder where your encrypted file is to be found def folderPath = "$project.rootDir/${folder}/${buildFlavor}/${buildVariant}/" //this will resolve file path according to build flavor and build variant String fileContents = new File("$folderPath$fileToBeEncryptedName").text // read contents of this file // encryption logic def IV = new byte[16]
def encryption = Encryption.getDefault(encryptKey, encryptSalt, IV) //use encryptKey and encryptSalt defined above def data = encryption == null ? "" : encryption.encryptOrNull(fileContents)
// create file in generated assets folder which can be packaged into the selected flavor/variant apk def file = new File("$buildDir/generated/assets/shaders/$buildFlavor/$buildVariant/$ext.outputConfigFileName")
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs()
}
android {
compileSdkVersion 27
.....
defaultConfig {
....
buildConfigField 'String', 'ENC_KEY', "\"$encryptKey\""
buildConfigField 'String', 'ENC_SALT', "\"$encryptSalt\""
buildConfigField 'String', 'ENC_FILE', "\"$outputConfigFileName\""
}
ext {
encryptKey = 'EncryptK3y'
encryptSalt = 'EncryptS@lt'
outputConfigFileName = 'super-secret-file'
}
buildscript {
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.github.simbiose:Encryption:2.0.1'
}
}
@dudego
dudego / data.json
Last active January 16, 2020 10:05
JsonData
{
"welcome": {
"header": {
"type": "image",
"url": "https://www.tajmahal.gov.in/images/banners/1.jpg"
},
"content": [
{
"icon": "",
"text": "\u003cb\u003eDear Gareth, Ankit, Lance, Alan, Dilip\u003c/b\u003e\u003cp\u003eWelcome to Publicis Sapient India. We are delighted to host you.\u003c/p\u003e\u003cp\u003eOur CEO, Nigel Vaz has a brief \u003ca href=\"https://lion.app.box.com/s/uf1jtmf4ezcntr60orqepif8f33meup0\"\u003emessage\u003c/a\u003efor you providing an introduction to our company and services.\u003c/p\u003e\u003cp\u003eAs part of the itinerary, you would be visiting the Publicis Sapient Bangalore and Noida office to meet the leadership team and US Bank India account members.\u003c/p\u003e\u003cp\u003eBangalore, also known as Bengaluru (Kannada) is the capital of the Indian State of Karnataka and has been given the epithet of Garden City. Located on the Deccan Plateau in the south-eastern part of Karnataka, Bangalore was once known as Pensioner’s Paradise