Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Testing and securing android studio applications

You're reading from  Testing and securing android studio applications

Product type Book
Published in Aug 2014
Publisher Packt
ISBN-13 9781783988808
Pages 162 pages
Edition 1st Edition
Languages

Using encryption to store data


Using all the methods discussed in the earlier sections, you can now encrypt any information in your application, as shown in the following code:

String myData = "My secret information";

SecretKeySpec sks = generateKey();
byte[] encoded = encrypt(myData, sks);
String decoded = decrypt(encoded, sks);

Log.d("MAIN - Encoded: ", 
Base64.encodeToString(encoded, Base64.DEFAULT));
Log.d("MAIN - Decoded: ", decoded);

The results generated in LogCat are shown in the following screenshot:

The previous example can be adapted to encrypt the content of a file on the internal storage of your application, as shown in the following code:

String myData = "My secret information in my internal file";
SecretKeySpec sks = generateKey();
byte[] encoded = encrypt(myData, sks);

FileOutputStream fos = 
openFileOutput("MyEncryptedFile.txt", Context.MODE_PRIVATE);
fos.write(encoded);
fos.close();

On executing the code in your main activity, the MyEncryptedFile.txt file will be created...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}