44import android .content .pm .PackageInfo ;
55import android .content .pm .PackageManager ;
66import android .content .pm .Signature ;
7+ import android .util .Log ;
78
89import java .io .ByteArrayInputStream ;
910import java .io .InputStream ;
2627
2728public class AndroidStringObfuscator {
2829
29- private final String TAG = this .getClass ().getSimpleName ();
30+ private static final String CODIFICATION = "UTF-8" ;
31+ private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding" ;
32+ private static final char [] hexArray = {'0' ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'A' ,'B' ,'C' ,'D' ,'E' ,'F' };
33+ private static final String TAG = AndroidStringObfuscator .class .getSimpleName ();
34+ private static Context context ;
3035
31- public static String getCertificateSHA1Fingerprint (Context context ) {
36+ public static void init (Context c ) {
37+ context = c ;
38+ }
39+
40+ private static String getCertificateSHA1Fingerprint () {
3241 PackageManager pm = context .getPackageManager ();
3342 String packageName = context .getPackageName ();
3443 int flags = PackageManager .GET_SIGNATURES ;
@@ -77,40 +86,11 @@ private static String byte2HexFormatted(byte[] arr) {
7786 return str .toString ();
7887 }
7988
80- private static String convertToHex (byte [] data ) {
81- StringBuilder buf = new StringBuilder ();
82- for (byte b : data ) {
83- int halfbyte = (b >>> 4 ) & 0x0F ;
84- int two_halfs = 0 ;
85- do {
86- buf .append ((0 <= halfbyte ) && (halfbyte <= 9 ) ? (char ) ('0' + halfbyte ) : (char ) ('a' + (halfbyte - 10 )));
87- halfbyte = b & 0x0F ;
88- } while (two_halfs ++ < 1 );
89- }
90- return buf .toString ();
91- }
92-
93- private static String SHA1 (String text ){
94- MessageDigest md = null ;
95- try {
96- md = MessageDigest .getInstance ("SHA-1" );
97- md .update (text .getBytes ("iso-8859-1" ), 0 , text .length ());
98- } catch (NoSuchAlgorithmException e ) {
99- e .printStackTrace ();
100- return null ;
101- } catch (UnsupportedEncodingException e ) {
102- e .printStackTrace ();
103- return null ;
104- }
105- byte [] sha1hash = md .digest ();
106- return convertToHex (sha1hash );
107- }
108-
10989 private static SecretKey generateKey (String key ) throws NoSuchAlgorithmException {
11090 MessageDigest digest = MessageDigest .getInstance ("SHA-1" );
11191 byte [] passphrase = null ;
11292 try {
113- passphrase = digest .digest (key .getBytes ("UTF-8" ));
93+ passphrase = digest .digest (key .getBytes (CODIFICATION ));
11494 } catch (UnsupportedEncodingException e ) {
11595 e .printStackTrace ();
11696 }
@@ -120,8 +100,8 @@ private static SecretKey generateKey(String key) throws NoSuchAlgorithmException
120100 }
121101
122102 private static String encrypt (String message , String key ) throws Exception {
123- byte [] data = message .getBytes ("UTF-8" );
124- Cipher cipher = Cipher .getInstance ("AES/ECB/PKCS5Padding" );
103+ byte [] data = message .getBytes (CODIFICATION );
104+ Cipher cipher = Cipher .getInstance (TRANSFORMATION );
125105 cipher .init (Cipher .ENCRYPT_MODE , generateKey (key ));
126106 byte [] encryptData = cipher .doFinal (data );
127107
@@ -131,10 +111,10 @@ private static String encrypt(String message, String key) throws Exception {
131111 private static String decrypt (String v , String key ) throws Exception {
132112 byte [] tmp = hexStringToByteArray (v );
133113 SecretKeySpec spec = new SecretKeySpec (generateKey (key ).getEncoded (), "AES" );
134- Cipher cipher = Cipher .getInstance ("AES/ECB/PKCS5Padding" );
114+ Cipher cipher = Cipher .getInstance (TRANSFORMATION );
135115 cipher .init (Cipher .DECRYPT_MODE , spec );
136116
137- String result = new String (cipher .doFinal (tmp ), "UTF-8" );
117+ String result = new String (cipher .doFinal (tmp ), CODIFICATION );
138118 return result ;
139119 }
140120
@@ -149,7 +129,6 @@ private static byte[] hexStringToByteArray(String s) {
149129 return data ;
150130 }
151131
152- final private static char [] hexArray = {'0' ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'A' ,'B' ,'C' ,'D' ,'E' ,'F' };
153132 private static String byteArrayToHexString (byte [] bytes ) {
154133 char [] hexChars = new char [bytes .length *2 ];
155134 int v ;
@@ -163,8 +142,13 @@ private static String byteArrayToHexString(byte[] bytes) {
163142 return new String (hexChars );
164143 }
165144
166- public static String getString (Context context , int id ) {
167- String hash = getCertificateSHA1Fingerprint (context );
145+ public static String getString (int id ) {
146+ if (context == null ) {
147+ Log .e (TAG , "Library not initiated: AndroidStringObfuscator.init(Context)" );
148+ return null ;
149+ }
150+
151+ String hash = getCertificateSHA1Fingerprint ();
168152 try {
169153 return decrypt (context .getString (id ), hash );
170154 } catch (Exception e ) {
@@ -173,8 +157,13 @@ public static String getString(Context context, int id) {
173157 return context .getString (id ); // returns original value, maybe not encrypted
174158 }
175159
176- public static String simulateString (Context context , String text ) {
177- String hash = getCertificateSHA1Fingerprint (context );
160+ public static String simulateString (String text ) {
161+ if (context == null ) {
162+ Log .e (TAG , "Library not initiated: AndroidStringObfuscator.init(Context)" );
163+ return null ;
164+ }
165+
166+ String hash = getCertificateSHA1Fingerprint ();
178167 try {
179168 return encrypt (text , hash );
180169 } catch (Exception e ) {
0 commit comments