Notice
Recent Posts
Recent Comments
Link
관리 메뉴

설.현.아빠

SharedPreference에 대한 암호화/복호화 본문

안드로이드/SharedPreference

SharedPreference에 대한 암호화/복호화

설.현.아빠 2013. 2. 26. 14:44

http://www.androidside.com/bbs/board.php?bo_table=B46&wr_id=44823&sca=유용한TIP&point=1

 

sharedPreference를 다들 많이 이용하시죠?

근데 만일 여기에 로그인정보같은 중요한 정보를 저장한다면 위험하겠죠?
루팅된 폰에선 금방 읽을수 있으니까요..
이걸 막을수 있는것이 바로 암호화입니다.

import java.util.Map;
import java.util.Set;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.util.Base64;

public class ObscuredSharedPreferences implements SharedPreferences {
protected static final String UTF8 = "utf-8";
private static final char[] SEKRIT = "ninelab.co.kr".toCharArray(); // INSERT
// A
// RANDOM
// PASSWORD
// HERE.
// Don't use anything you wouldn't want to
// get out there if someone decompiled
// your app.

protected SharedPreferences delegate;
protected Context context;

public ObscuredSharedPreferences(Context context, SharedPreferences delegate) {
this.delegate = delegate;
this.context = context;
}

public class Editor implements SharedPreferences.Editor {

protected SharedPreferences.Editor delegate;

public Editor() {
this.delegate = ObscuredSharedPreferences.this.delegate.edit();
}

@Override
public Editor putBoolean(String key, boolean value) {
delegate.putString(key, encrypt(Boolean.toString(value)));
return this;
}

@Override
public Editor putFloat(String key, float value) {
delegate.putString(key, encrypt(Float.toString(value)));
return this;
}

@Override
public Editor putInt(String key, int value) {
delegate.putString(key, encrypt(Integer.toString(value)));
return this;
}

@Override
public Editor putLong(String key, long value) {
delegate.putString(key, encrypt(Long.toString(value)));
return this;
}

@Override
public Editor putString(String key, String value) {
delegate.putString(key, encrypt(value));
return this;
}

@Override
public void apply() {
delegate.apply();
}

@Override
public Editor clear() {
delegate.clear();
return this;
}

@Override
public boolean commit() {
return delegate.commit();
}

@Override
public Editor remove(String s) {
delegate.remove(s);
return this;
}

@Override
public android.content.SharedPreferences.Editor putStringSet(
String arg0, Set<String> arg1) {
// TODO Auto-generated method stub
return null;
}
}

public Editor edit() {
return new Editor();
}

@Override
public Map<String, ?> getAll() {
throw new UnsupportedOperationException(); // left as an exercise to the
// reader
}

@Override
public boolean getBoolean(String key, boolean defValue) {
final String v = delegate.getString(key, null);
return v != null ? Boolean.parseBoolean(decrypt(v)) : defValue;
}

@Override
public float getFloat(String key, float defValue) {
final String v = delegate.getString(key, null);
return v != null ? Float.parseFloat(decrypt(v)) : defValue;
}

@Override
public int getInt(String key, int defValue) {
final String v = delegate.getString(key, null);
return v != null ? Integer.parseInt(decrypt(v)) : defValue;
}

@Override
public long getLong(String key, long defValue) {
final String v = delegate.getString(key, null);
return v != null ? Long.parseLong(decrypt(v)) : defValue;
}

@Override
public String getString(String key, String defValue) {
final String v = delegate.getString(key, null);
return v != null ? decrypt(v) : defValue;
}

@Override
public boolean contains(String s) {
return delegate.contains(s);
}

@Override
public void registerOnSharedPreferenceChangeListener(
OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
delegate.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
}

@Override
public void unregisterOnSharedPreferenceChangeListener(
OnSharedPreferenceChangeListener onSharedPreferenceChangeListener) {
delegate.unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
}

protected String encrypt(String value) {

try {
final byte[] bytes = value != null ? value.getBytes(UTF8)
: new byte[0];
SecretKeyFactory keyFactory = SecretKeyFactory
.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(
Cipher.ENCRYPT_MODE,
key,
new PBEParameterSpec(Settings.Secure.getString(
context.getContentResolver(),
Settings.System.ANDROID_ID).getBytes(UTF8), 20));
return new String(Base64.encode(pbeCipher.doFinal(bytes),
Base64.NO_WRAP), UTF8);

} catch (Exception e) {
throw new RuntimeException(e);
}

}

protected String decrypt(String value) {
try {
final byte[] bytes = value != null ? Base64.decode(value,
Base64.DEFAULT) : new byte[0];
SecretKeyFactory keyFactory = SecretKeyFactory
.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT));
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(
Cipher.DECRYPT_MODE,
key,
new PBEParameterSpec(Settings.Secure.getString(
context.getContentResolver(),
Settings.System.ANDROID_ID).getBytes(UTF8), 20));
return new String(pbeCipher.doFinal(bytes), UTF8);

} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public Set<String> getStringSet(String arg0, Set<String> arg1) {
// TODO Auto-generated method stub
return null;
}

}

이 클래스를 정의해주고요..
SharedPreferences mSPref = new ObscuredSharedPreferences(mContext,
mContext.getSharedPreferences(SETTING_TABLE_NAME, 0)); 로 이용하시면 됩니다.

Comments