0

I am working on android application in which i want to encrypt my text which is stored using ormlite, so if any one from outside get the DB file, he will not be able to read it with out decrypting it. My major concern is to encrypt the data using base64 and when i want to read the data it should decrypt it. I have read some sample code from a link for base64 please suggest me the way for encrypting my text before saving it to the DB and on retrieving time it should decrypt it.

// Sending side
byte[] data = text.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);

// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
Robert
  • 39,162
  • 17
  • 99
  • 152
Usman Khan
  • 3,739
  • 6
  • 41
  • 89
  • 3
    There is a big difference between encryption and encoding. Please make sure your data isn't sensitive. Check this post for more encryption details http://stackoverflow.com/questions/1205135/how-to-encrypt-string-in-java – Stefan May 04 '15 at 08:07
  • Actually i am searching for any mechanism to encrypt and decrypt my text in ormlite data bases. I just need to make something which can encrypt my data in db and on receiving time it should decrypt it. – Usman Khan May 04 '15 at 08:09
  • 1
    Yes, and the differnce between encoding and encryption is that: sombody can ALWAYS decode your string, so it isn't secure. With encryption. another person will need an encryption key to decrypt it. – Stefan May 04 '15 at 08:14
  • @Stefan thanks for your reply. Can you please suggest me any link or sample code for encryption and decryption on text in android. – Usman Khan May 04 '15 at 08:24
  • Base64 quite a weak "encryption," merely for the non-savvy user to not be able to read the data. For the rest you are doing it right. – Joop Eggen May 04 '15 at 08:51

1 Answers1

1

You can use this code for encryption and decryption.

I am using 3DES encryption scheme here. I hope it will help you.

    public PasswordEncryption_TrippleDES() throws Exception {
        myEncryptionKey =  //your encryption key
        myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
        arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
        ks = new DESedeKeySpec(arrayBytes);
        skf = SecretKeyFactory.getInstance(myEncryptionScheme);
        cipher = Cipher.getInstance(myEncryptionScheme);
        key = skf.generateSecret(ks);
    }

    public String encrypt(String unencryptedString) {
        String encryptedString = null;
        try {
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
            byte[] encryptedText = cipher.doFinal(plainText);
            encryptedString = new String(Base64.encodeBase64(encryptedText));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedString;
    }

    public String decrypt(String encryptedString) {
        String decryptedText=null;
        try {
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] encryptedText = Base64.decodeBase64(encryptedString);
            byte[] plainText = cipher.doFinal(encryptedText);
            decryptedText= new String(plainText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return decryptedText;
    }
Stefan
  • 17,448
  • 11
  • 60
  • 79