Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i encrypt particular column data in Android SQLite Data base.Using SQLCipher utility

I have a SQLite DB in android, and I want to encrypt only few columns in that DB. I founded that its available with whole DB to encrypt, but I need to encrypt only few columns data only.

like image 629
Hari Enaganti Avatar asked May 16 '26 10:05

Hari Enaganti


2 Answers

You can encrypt and decrypt your data by using this class:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import android.util.Base64;

public class SimpleCrypto {

    private String charsetName = "UTF8";
    private String algorithm = "DES";
    private int base64Mode = Base64.DEFAULT;

    public String getCharsetName() {
        return charsetName;
    }

    public void setCharsetName(String charsetName) {
        this.charsetName = charsetName;
    }

    public String getAlgorithm() {
        return algorithm;
    }

    public void setAlgorithm(String algorithm) {
        this.algorithm = algorithm;
    }

    public int getBase64Mode() {
        return base64Mode;
    }

    public void setBase64Mode(int base64Mode) {
        this.base64Mode = base64Mode;
    }

    public String encrypt(String key, String data) {
        if (key == null || data == null)
            return null;
        try {
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(charsetName));
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
            SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
            byte[] dataBytes = data.getBytes(charsetName);
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return Base64.encodeToString(cipher.doFinal(dataBytes), base64Mode);
        } catch (Exception e) {
            return null;
        }
    }

    public String decrypt(String key, String data) {
        if (key == null || data == null)
            return null;
        try {
            byte[] dataBytes = Base64.decode(data, base64Mode);
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(charsetName));
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
            SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] dataBytesDecrypted = (cipher.doFinal(dataBytes));
            return new String(dataBytesDecrypted);
        } catch (Exception e) {
            return null;
        }
    }
}

You can encrypt your data like this:

private SimpleCrypto simpleCrypto = new SimpleCrypto();

String MasterPassword = "qssfFs32fFwada";

String encryptedText = simpleCrypto.encrypt(MasterPassword, "Hello World");
Log.i(encryptedText, encryptedText);

And then you can decrypt them like this:

String decryptedText = decrypt(MasterPassword, encryptedText);
Log.i(decryptedText, decryptedText);
like image 63
Vasileios Pallas Avatar answered May 18 '26 00:05

Vasileios Pallas


You can not do this with SQLCipher. SQLCipher encrypts the entire contents of a database, both schema and data. If you are still interested in using SQLCipher within your Android application, we have a tutorial for integration here.

like image 25
Nick Parker Avatar answered May 17 '26 22:05

Nick Parker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!