81 lines
3.3 KiB
Java
81 lines
3.3 KiB
Java
package com.ea.nimble;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import com.ea.nimble.Log;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.io.OutputStream;
|
|
import java.security.GeneralSecurityException;
|
|
import java.security.Provider;
|
|
import java.security.Security;
|
|
import java.util.Iterator;
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.CipherInputStream;
|
|
import javax.crypto.CipherOutputStream;
|
|
import javax.crypto.SecretKey;
|
|
import javax.crypto.SecretKeyFactory;
|
|
import javax.crypto.spec.PBEKeySpec;
|
|
import javax.crypto.spec.PBEParameterSpec;
|
|
|
|
|
|
class Encryptor {
|
|
private static int ENCRYPTION_KEY_LENGHT = 128;
|
|
private static int ENCRYPTION_KEY_ROUND = 997;
|
|
private Cipher m_encryptor = null;
|
|
private Cipher m_decryptor = null;
|
|
|
|
@SuppressLint({"NewApi"})
|
|
private void initialize() throws GeneralSecurityException {
|
|
for (Provider provider : Security.getProviders()) {
|
|
Log.Helper.LOGIS(null, "Cryptor Provider: " + provider.getName(), new Object[0]);
|
|
Iterator<Provider.Service> it = provider.getServices().iterator();
|
|
while (it.hasNext()) {
|
|
Log.Helper.LOGIS(null, "Cryptor Algorithm: " + it.next().getAlgorithm(), new Object[0]);
|
|
}
|
|
}
|
|
try {
|
|
String applicationBundleId = ApplicationEnvironment.getComponent().getApplicationBundleId();
|
|
byte[] bytes = "02:00:00:00:00:00".getBytes();
|
|
byte[] bArr = new byte[8];
|
|
int i = 0;
|
|
int i2 = 0;
|
|
while (i < bArr.length) {
|
|
int i3 = i2 + 1;
|
|
if (i3 % 3 == 0) {
|
|
i2 = i3;
|
|
}
|
|
bArr[i] = bytes[i2];
|
|
i++;
|
|
i2++;
|
|
}
|
|
SecretKey generateSecret = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(applicationBundleId.toCharArray(), bArr, ENCRYPTION_KEY_ROUND, ENCRYPTION_KEY_LENGHT));
|
|
PBEParameterSpec pBEParameterSpec = new PBEParameterSpec(bArr, ENCRYPTION_KEY_ROUND);
|
|
this.m_encryptor = Cipher.getInstance("PBEWithMD5AndDES");
|
|
this.m_encryptor.init(1, generateSecret, pBEParameterSpec);
|
|
this.m_decryptor = Cipher.getInstance("PBEWithMD5AndDES");
|
|
this.m_decryptor.init(2, generateSecret, pBEParameterSpec);
|
|
} catch (GeneralSecurityException e) {
|
|
Log.Helper.LOGFS(null, "Can't initialize Encryptor: " + e.toString(), new Object[0]);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public ObjectInputStream encryptInputStream(InputStream inputStream) throws IOException, GeneralSecurityException {
|
|
Log.Helper.LOGFUNC(this);
|
|
if (this.m_encryptor == null || this.m_decryptor == null) {
|
|
initialize();
|
|
}
|
|
return new ObjectInputStream(new CipherInputStream(inputStream, this.m_decryptor));
|
|
}
|
|
|
|
public ObjectOutputStream encryptOutputStream(OutputStream outputStream) throws IOException, GeneralSecurityException {
|
|
Log.Helper.LOGFUNC(this);
|
|
if (this.m_encryptor == null || this.m_decryptor == null) {
|
|
initialize();
|
|
}
|
|
return new ObjectOutputStream(new CipherOutputStream(outputStream, this.m_encryptor));
|
|
}
|
|
}
|