Skip to content

Commit 3309c02

Browse files
committed
iml files
2 parents 9f56525 + b5f563a commit 3309c02

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

androidstringobfuscator/src/main/java/com/efraespada/androidstringobfuscator/AndroidStringObfuscator.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
import java.io.UnsupportedEncodingException;
1212
import java.security.MessageDigest;
1313
import java.security.NoSuchAlgorithmException;
14+
import java.security.SecureRandom;
1415
import java.security.cert.CertificateEncodingException;
1516
import java.security.cert.CertificateException;
1617
import java.security.cert.CertificateFactory;
1718
import java.security.cert.X509Certificate;
1819
import java.util.Arrays;
20+
import java.util.Random;
1921

2022
import javax.crypto.Cipher;
2123
import javax.crypto.SecretKey;
24+
import javax.crypto.spec.IvParameterSpec;
2225
import javax.crypto.spec.SecretKeySpec;
2326

2427
/**
@@ -27,11 +30,13 @@
2730

2831
public class AndroidStringObfuscator {
2932

33+
private static final int LENGTH = 16;
3034
private static final String CODIFICATION = "UTF-8";
31-
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
35+
private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
3236
private static final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
3337
private static final String TAG = AndroidStringObfuscator.class.getSimpleName();
3438
private static Context context;
39+
private static byte[] iv = new byte[0];
3540

3641
public static void init(Context c) {
3742
context = c;
@@ -100,19 +105,21 @@ private static SecretKey generateKey(String key) throws NoSuchAlgorithmException
100105
}
101106

102107
private static String encrypt(String message, String key) throws Exception {
108+
buildIvParam(message, key);
103109
byte[] data = message.getBytes(CODIFICATION);
104110
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
105-
cipher.init(Cipher.ENCRYPT_MODE, generateKey(key));
111+
cipher.init(Cipher.ENCRYPT_MODE, generateKey(key), new IvParameterSpec(iv));
106112
byte[] encryptData = cipher.doFinal(data);
107113

108114
return byteArrayToHexString(encryptData);
109115
}
110116

111117
private static String decrypt(String message, String key) throws Exception {
118+
buildIvParam(message, key);
112119
byte[] tmp = hexStringToByteArray(message);
113120
SecretKeySpec spec = new SecretKeySpec(generateKey(key).getEncoded(), "AES");
114121
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
115-
cipher.init(Cipher.DECRYPT_MODE, spec);
122+
cipher.init(Cipher.DECRYPT_MODE, spec, new IvParameterSpec(iv));
116123

117124
String result = new String(cipher.doFinal(tmp), CODIFICATION);
118125
return result;
@@ -201,4 +208,23 @@ public static String decryptString(String value) {
201208
}
202209
return null;
203210
}
211+
212+
private static void buildIvParam(String message, String key) {
213+
if (iv.length == 0) {
214+
iv = new byte[LENGTH];
215+
int index = randomNumber(key.length());
216+
System.arraycopy(key.getBytes(), index, iv, 0, LENGTH);
217+
218+
try {
219+
return decrypt(message, hash);
220+
} catch (Exception e) {
221+
e.printStackTrace();
222+
}
223+
}
224+
}
225+
226+
private static int randomNumber(int length){
227+
int value = new Random().nextInt(length) + length - 2 - length;
228+
return value;
229+
}
204230
}

sample/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ dependencies {
3737
})
3838
compile 'com.android.support:appcompat-v7:25.2.0'
3939
testCompile 'junit:junit:4.12'
40-
// compile project(path: ':androidstringobfuscator')
41-
compile 'efraespada:androidstringobfuscator:0.4.1'
40+
compile project(path: ':androidstringobfuscator')
41+
//compile 'efraespada:androidstringobfuscator:0.4.1'
4242

4343
}
4444

0 commit comments

Comments
 (0)