Created
September 9, 2014 10:47
-
-
Save aliab/8cc7c4b9f8cbda263dc7 to your computer and use it in GitHub Desktop.
this is java class for encrypt/decrypt any Android resource, o bye[] file
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
package net.appersian.android.wod.common; | |
import java.security.SecureRandom; | |
import javax.crypto.Cipher; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.SecretKeySpec; | |
public class Encryptor { | |
public static byte[] generateKey(String password) throws Exception | |
{ | |
byte[] keyStart = password.getBytes("UTF-8"); | |
KeyGenerator kgen = KeyGenerator.getInstance("AES"); | |
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); | |
sr.setSeed(keyStart); | |
kgen.init(128, sr); | |
SecretKey skey = kgen.generateKey(); | |
return skey.getEncoded(); | |
} | |
public static byte[] encodeFile(byte[] key, byte[] fileData) throws Exception | |
{ | |
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(Cipher.ENCRYPT_MODE, skeySpec); | |
byte[] encrypted = cipher.doFinal(fileData); | |
return encrypted; | |
} | |
public static byte[] decodeFile(byte[] key, byte[] fileData) throws Exception | |
{ | |
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(Cipher.DECRYPT_MODE, skeySpec); | |
byte[] decrypted = cipher.doFinal(fileData); | |
return decrypted; | |
} | |
} |
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
package net.appersian.android.wod.common; | |
import java.io.BufferedInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import org.apache.http.util.ByteArrayBuffer; | |
import android.content.Context; | |
import android.content.res.Resources; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.drawable.BitmapDrawable; | |
public class EncryptorHelper { | |
public static byte[] encrypt(Context ctx,int resource,String pswd) throws Exception{ | |
InputStream is = ctx.getResources().openRawResource(resource); | |
BufferedInputStream bis = new BufferedInputStream(is); | |
ByteArrayBuffer baf = new ByteArrayBuffer(50); | |
int current = 0; | |
try { | |
while ((current = bis.read()) != -1) { | |
baf.append((byte) current); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
byte[] b = baf.toByteArray(); | |
byte[] filesBytes = null; | |
byte[] yourKey = null; | |
yourKey = Encryptor.generateKey(pswd); | |
filesBytes = Encryptor.encodeFile(yourKey, b); | |
return filesBytes; | |
} | |
public static byte[] decrypt(Context ctx,int resource,String pswd) throws Exception{ | |
InputStream is = ctx.getResources().openRawResource(resource); | |
BufferedInputStream bis = new BufferedInputStream(is); | |
ByteArrayBuffer baf = new ByteArrayBuffer(50); | |
int current = 0; | |
try { | |
while ((current = bis.read()) != -1) { | |
baf.append((byte) current); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
byte[] b = baf.toByteArray(); | |
byte[] filesBytes = null; | |
byte[] yourKey = null; | |
yourKey = Encryptor.generateKey(pswd); | |
filesBytes = Encryptor.decodeFile(yourKey, b); | |
return filesBytes; | |
} | |
public static Bitmap bitmapDecrypt(Context ctx,int resource,String pswd) throws Exception{ | |
byte[] b = decrypt(ctx, resource, pswd); | |
return getBitmap(b); | |
} | |
public static BitmapDrawable drawableDecrypt(Context ctx,int resource,String pswd,Resources res) throws Exception{ | |
return new BitmapDrawable(res, bitmapDecrypt(ctx,resource,pswd)); | |
} | |
public static Bitmap getBitmap(byte[] b){ | |
return BitmapFactory.decodeByteArray(b, 0, b.length); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's pretty old but what I remember is this resource int is the id from R file.