Skip to content

Instantly share code, notes, and snippets.

@paulocoutinhox
Created August 6, 2017 03:47
Show Gist options
  • Select an option

  • Save paulocoutinhox/86c2416feada8b377c36b70d75826260 to your computer and use it in GitHub Desktop.

Select an option

Save paulocoutinhox/86c2416feada8b377c36b70d75826260 to your computer and use it in GitHub Desktop.
ArvoreSecurityUtil - Decryptor para Árvore
package com.ubook.reader.util;
import android.util.Base64;
import android.util.Log;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class ArvoreSecurityUtil {
private final String TAG = ArvoreSecurityUtil.this.getClass().getSimpleName();
Cipher ecipher;
Cipher dcipher;
public ArvoreSecurityUtil() {
try {
byte[] key = getKey();
byte[] iv = getIV();
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
IvParameterSpec ivSpec = new IvParameterSpec(iv);
ecipher = Cipher.getInstance("AES/CBC/NoPadding");
ecipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
dcipher = Cipher.getInstance("AES/CBC/NoPadding");
dcipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
} catch (Exception e) {
Log.e(TAG, "", e);
e.printStackTrace();
}
}
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return Base64.encodeToString(enc, Base64.DEFAULT);
} catch (Exception e) {
Log.e(TAG, "", e);
e.printStackTrace();
return null;
}
}
public String decrypt(String str) {
try {
// Decode base64 to get bytes
byte[] dec = Base64.decode(str, Base64.DEFAULT);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
} catch (Exception e) {
Log.e(TAG, "", e);
e.printStackTrace();
return null;
}
}
private byte[] getIV() {
return base64ToBinary("N9mT3Z5DzqghlBVBC0YsTg==\n");
}
private byte[] getKey() {
return base64ToBinary("6LMmbmbL4EKvi55WJFxZHW5FOH25/RGbuD3Vx8MEYGU=\n");
}
private byte[] base64ToBinary(String base64_arg) {
return Base64.decode(base64_arg, Base64.DEFAULT);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment