Code example for Cipher

Methods: getBlockSize, init

0
			this.mDecryptCipher = Cipher.getInstance(SecureSharedPreferences.CIPHER_TRANSFORMATION);
 
			final IvParameterSpec ivSpec = this.getIvParameterSpec();
			final SecretKeySpec secretKey = this.getSecretKeySpec(pSecureKey);
 
			this.mEncryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
			this.mDecryptCipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
		} catch (final GeneralSecurityException e) {
			throw new SecureSharedPreferencesException(e);
		} catch (final UnsupportedEncodingException e) {
			throw new SecureSharedPreferencesException(e);
		} 
	} 
 
	// =========================================================== 
	// Getter & Setter 
	// =========================================================== 
 
	// =========================================================== 
	// Methods for/from SuperClass/Interfaces 
	// =========================================================== 
 
	@Override 
	public Editor edit() { 
		return new Editor(); 
	} 
 
	@Override 
	public Map<String, ?> getAll() {
		throw new MethodNotYetImplementedException(); 
	} 
 
	@Override 
	public boolean getBoolean(final String pKey, final boolean pDefaultValue) {
		final String encryptedKey = this.encryptKey(pKey);
		final String value = this.mDelegate.getString(encryptedKey, null);
		if (value == null) {
			return pDefaultValue;
		} else { 
			final String decryptedValue = this.decryptValue(value);
			return Boolean.parseBoolean(decryptedValue);
		} 
	} 
 
	@Override 
	public int getInt(final String pKey, final int pDefaultValue) {
		final String encryptedKey = this.encryptKey(pKey);
		final String value = this.mDelegate.getString(encryptedKey, null);
		if (value == null) {
			return pDefaultValue;
		} else { 
			final String decryptedValue = this.decryptValue(value);
			return Integer.parseInt(decryptedValue);
		} 
	} 
 
	@Override 
	public long getLong(final String pKey, final long pDefaultValue) {
		final String encryptedKey = this.encryptKey(pKey);
		final String value = this.mDelegate.getString(encryptedKey, null);
		if (value == null) {
			return pDefaultValue;
		} else { 
			final String decryptedValue = this.decryptValue(value);
			return Long.parseLong(decryptedValue);
		} 
	} 
 
	@Override 
	public float getFloat(final String pKey, final float pDefaultValue) {
		final String encryptedKey = this.encryptKey(pKey);
		final String value = this.mDelegate.getString(encryptedKey, null);
		if (value == null) {
			return pDefaultValue;
		} else { 
			final String decryptedValue = this.decryptValue(value);
			return Float.parseFloat(decryptedValue);
		} 
	} 
 
	@Override 
	public String getString(final String pKey, final String pDefaultValue) {
		final String encryptedKey = this.encryptKey(pKey);
		final String value = this.mDelegate.getString(encryptedKey, null);
		if (value == null) {
			return pDefaultValue;
		} else { 
			final String decryptedValue = this.decryptValue(value);
			return decryptedValue;
		} 
	} 
 
	@Override 
	public boolean contains(final String pKey) {
		return this.mDelegate.contains(pKey);
	} 
 
	@Override 
	public void registerOnSharedPreferenceChangeListener(final OnSharedPreferenceChangeListener pOnSharedPreferenceChangeListener) {
		this.mDelegate.registerOnSharedPreferenceChangeListener(pOnSharedPreferenceChangeListener);
	} 
 
	@Override 
	public void unregisterOnSharedPreferenceChangeListener(final OnSharedPreferenceChangeListener pOnSharedPreferenceChangeListener) {
		this.mDelegate.unregisterOnSharedPreferenceChangeListener(pOnSharedPreferenceChangeListener);
	} 
 
	@Override 
	public Set<String> getStringSet(final String pKey, final Set<String> pStringSet) {
		throw new MethodNotYetImplementedException(); 
	} 
 
	// =========================================================== 
	// Methods 
	// =========================================================== 
 
	protected String encryptKey(final String pKey) {
		if (this.mEncryptKeys) {
			return this.encrypt(pKey);
		} else { 
			return pKey;
		} 
	} 
 
	protected String encryptValue(final String pValue) {
		if (this.mEncryptValues) {
			return this.encrypt(pValue);
		} else { 
			return pValue;
		} 
	} 
 
	protected String decryptKey(final String pKey) {
		if (this.mEncryptKeys) {
			return this.decrypt(pKey);
		} else { 
			return pKey;
		} 
	} 
 
	protected String decryptValue(final String pValue) {
		if (this.mEncryptValues) {
			return this.decrypt(pValue);
		} else { 
			return pValue;
		} 
	} 
 
	protected IvParameterSpec getIvParameterSpec() {
		final int blockSize = this.mEncryptCipher.getBlockSize();
		final byte[] iv = new byte[blockSize];
		System.arraycopy("abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".getBytes(), 0, iv, 0, blockSize);
		return new IvParameterSpec(iv);
	}