How to Encrypt DataStore in Android

  Рет қаралды 21,010

Philipp Lackner

Philipp Lackner

Жыл бұрын

DataStore is a cool solution to save preferences. However, as of now there's no official way to encrypt the data we save there. In this video I'll show you how that works.
⭐ Get certificates for your future job
⭐ Save countless hours of time
⭐ 100% money back guarantee for 30 days
⭐ Become a professional Android developer now:
pl-coding.com/premium-courses...
💻 Let me be your mentor and become an industry-ready Android developer in 10 weeks:
pl-coding.com/drop-table-ment...
Subscribe to my FREE newsletter for regular Android, Kotlin & Architecture advice!
pl-coding.com/newsletter
Join this channel to get access to perks:
/ @philipplackner
Get the initial source code for this video here:
github.com/philipplackner/And...
Here's the video about encryption and decryption:
• FULL Guide to Encrypti...
Get the final source code for this video here:
github.com/philipplackner/And...
Join my Discord server:
/ discord
Regular programming advice on my Instagram page: / _philipplackner_
Checkout my GitHub: github.com/philipplackner
You like my free content? Here you can buy me a coffee:
www.buymeacoffee.com/philippl...

Пікірлер: 90
@leonardosibela
@leonardosibela Жыл бұрын
MAN!!!!! MAAAAAN!!!! MAAAAAAAAAAAN!!! Hey Philipp, I'm sorry the excitement, but I really am!!! You've been making so much awesome content that it is UNBELIEVABLE!!! YOU ARE ON FIRE, MA DUDE!!! lol I'm laughing, but it's totally true!! For me, you are a legend on Android!! And the reasons are plenty: * We all know how much you know about Android; * You are very, very updated on things related to the platform; * You have a really awesome way of teaching (and the reasons why I say this are also plenty); * You are extremely charismatic (which is icing on the cake); * Your videos are more and more professional (and there are plenty of reasons that I can elaborate on what makes me say that); * You are making shorts videos about a specific subject, long videos about architecture, playlists about long topics, shorts about tips... You are REALLY ON FIRE!! Philipp... Thank you so much for all your dedication. I'm really glad we all have such an incredible professional and amazing person such as you teaching us so much!! Have a really awesome life, my friend and keep rocking🤘🏻❤️
@PhilippLackner
@PhilippLackner Жыл бұрын
Yoo, thanks so much for the awesome message, nothing makes a creator smile more than this Really happy that my stuff helps 🙏🙏
@anthonyp4209
@anthonyp4209 Жыл бұрын
One of the best tips channels on KZfaq. I get something useful from every video
@dastanikus
@dastanikus Ай бұрын
This lesson is great. For those who has issues - read more in the net. This was a basic initial start tutorial to give you an idea, dont expect fully ready code for production. This is not tested for all cases in your mind, do some research and expand your knowledge based on this first lesson.
@ajailani4
@ajailani4 Жыл бұрын
Great! I just asked this kind of topic on your latest IG post. And we just got this new video
@lucasgomez4656
@lucasgomez4656 Жыл бұрын
Hey Phillip! Thanks again for another great video! As a suggestion, can you make a video for proguard optimization rules? I know network classes should be under the keep flag, but what else can we optimize to make the app safer?
@karthikeyan5916
@karthikeyan5916 Жыл бұрын
After saving the data if I kill the app and reopen the app and click on load button I am getting null data instead of fetching the old saved data. how to fix this issue bro
@zinmintun7062
@zinmintun7062 Жыл бұрын
I really like your videos. At the moment I would like to use ksp instead of kapt annotation processer.Do you think dagger-hilt support ksp in future?I got stuck in dagger-hilt ;") .The doc says using ksp is 2x faster than kapt that we can benefit if we use ksp fully.What do you think?I'll glad if you make video for ksp vs kapt too.
@ThomasFunke
@ThomasFunke 9 ай бұрын
Thank you for your video(s). I learn many from them. Unfortunately the AndroidCrypto App doesn't work. At runtime of the app, anything works, but after a restart of the app, there are deserialize errors. It is possible, that the keys, iv etc. are not se same after restart, and so the stream cannot be deserialized? Without cryption layer the datastore functionality is given.
@hichemhammadache8798
@hichemhammadache8798 9 ай бұрын
yes i'm experiencing the same issus , did you solve the problem ?
@arthurgomes9248
@arthurgomes9248 Жыл бұрын
Bought some courses on this channel, i must say, its quality content. Keep it up.
@PhilippLackner
@PhilippLackner Жыл бұрын
Thanks for the support!
@radu2173
@radu2173 Жыл бұрын
the hero we don't deserve
@PhilippLackner
@PhilippLackner Жыл бұрын
🙏🙏
@user-ds1ub3tv7x
@user-ds1ub3tv7x 11 ай бұрын
Philipp, It works well until you close the app :) Did you tried to close or load-out app from background and run it again? I'm getting serialization exception: Expected start of the object '{', but had 'EOF' instead :) I guess that on LOAD button click you are getting the first element of flow generated by DataStore without decryption :)
@hichemhammadache8798
@hichemhammadache8798 9 ай бұрын
yes i'm experiencing the same issue, did you solve the problem ?
@nicolascambuy1507
@nicolascambuy1507 8 ай бұрын
@@hichemhammadache8798 i have the same problem
@AhmedAli-qm1td
@AhmedAli-qm1td 3 ай бұрын
me too, did solve it?
@AhmedAli-qm1td
@AhmedAli-qm1td 3 ай бұрын
@@hichemhammadache8798 I tried to inject the crypto class as a singleton but that didn't work , i think the problem is in the key because the crypto class is created more than one time in the app, but how to solve that ? also when I logged the ivSize in decrypt function I found that it is not the same that generated in encrypt. if you found the solution tell me please
@ChristopherAdonis-zz3wb
@ChristopherAdonis-zz3wb Жыл бұрын
Hello, love your content!
@hemanthkumarupp
@hemanthkumarupp Жыл бұрын
Hey Phil, Can you create a video on how to improve webview performance. Thanks for the videos.
@PandaTop.
@PandaTop. Жыл бұрын
How to store, for example, List in the DataStore and then retrieve it? I tried using koltin serialization... How to properly store data class in DataStore?
@jayceeddeyro1406
@jayceeddeyro1406 Жыл бұрын
Question. what is the difference between "by dataStore" and "by preferencesDataStore"?
@absoz
@absoz Жыл бұрын
Hi Philipp, hope you are doing well I've been learning Kotlin for the last little bit and came across your channel where this video has been excellent However, I'm having hopefully a simple issue. I downloaded your final version of the code, after working through the previous video and at the 1:45 mark of this one, you show how saving the 2 values, closing the app, then opening it up again, it can reload the previously saved values Using the git master version of the project, not letting Android Studio make any updates to Gradle etc, and it seems to not work properly When I save the data, the .json file is created (if it doesnt already exist) with the correct date&time on the device It can be reloaded without a problem... but if I close the app and start it again, it won't load the old values. The date & time of the file doesnt change (I've even shut down Android Studio and opened it again) but it continues to have the original default values I can edit the 2 fields and save it again - the file updates, and can be reloaded, but if I shut down the app again - the old values still are not available to be used. Really weird Can you double check the version on GitHub is working for you ?? Hope this makes sense - really appreciate the info I'm learning from your channel
@nipunshah1373
@nipunshah1373 Жыл бұрын
Whilst Loading why its retrieved as first(), i.e isn't it meant to provide only a object which is stored in DataStore ? What is significance of Flow or Stream behind the same ?
@wolfik555
@wolfik555 Жыл бұрын
Thank you for the great video. Could you make a video about authorizing requests from Android on external Rest API?
@yasserakbbach7342
@yasserakbbach7342 Жыл бұрын
You're a life saver, btw you still mising RatingStar in compose 😉 Any news about the V2 compose series?
@mustafaammar551
@mustafaammar551 Жыл бұрын
Very cool Thank you
@ravisahu3301
@ravisahu3301 11 ай бұрын
hey phillip that video is just great but datastore isn't loading after the application dies. why it isn't persisting or may failed to load after encyption when reopening the application
@MonichGPT
@MonichGPT Жыл бұрын
Can you make similar for RoomDB?
@brgame8
@brgame8 Жыл бұрын
Hi philipp, Is cryptomanager needed specific rule for proguard? with minifyEnable true
@PhilippLackner
@PhilippLackner Жыл бұрын
Shouldn't be necessary
@DatHQ_Official
@DatHQ_Official Жыл бұрын
It will crash app after some step save and load. Open app again and click load it will crash
@AhmedAli-qm1td
@AhmedAli-qm1td 3 ай бұрын
me too, did solve it?
@mashoodpv9230
@mashoodpv9230 Жыл бұрын
Great content... 🥰🤗
@PhilippLackner
@PhilippLackner Жыл бұрын
Thank you!
@mashoodpv9230
@mashoodpv9230 Жыл бұрын
@@PhilippLackner ♥️
@oyacanl3997
@oyacanl3997 7 ай бұрын
Thank you, but this is not working after a close-reopen unfortunately, as a few other people mentioned
@miq0717
@miq0717 Жыл бұрын
Which datastore do you recommend to use? Proto DataStore or DataStore Preferences?
@lucasgomez4656
@lucasgomez4656 Жыл бұрын
He already told in another video that prefers Datastore Preferences because you have to do less code. In proto datastore you have to do a lot of pre-configuration before being able to use it.
@miq0717
@miq0717 Жыл бұрын
@@lucasgomez4656 Thanks
@utjok
@utjok 5 ай бұрын
Thanks man
@alsose5453
@alsose5453 Жыл бұрын
You cant open saved projects in demo...
@irsathkareem7513
@irsathkareem7513 Жыл бұрын
Can you please make video on "How to integrate Exoplayer with custom controls in jetpack compose"..... How to add custom controller UI written in jetpack compose to exoplayer, instead of XML???
@danielgomes9025
@danielgomes9025 Жыл бұрын
Nice video! But it doesn't work with large strings. I'm trying to encrypt/decrypt a large JWT token (more than 1000 charactes length) and get an InvalidInputException
@yoyodev
@yoyodev 10 ай бұрын
Same here, did you solve this?
@azmpdev
@azmpdev 10 ай бұрын
Update: Cleaned it up a bit . The edit by @chrisathanas i just added some extra parts for the over 1000 length fun encrypt(bytes: ByteArray, outputStream: OutputStream): ByteArray { // Include the Initialization Vector (IV) at the start of the data to encrypt val encryptedBytes = Base64.encodeToString(encryptCipher.doFinal(encryptCipher.iv + bytes), Base64.DEFAULT) //Encode the Encrypted data to base64 val iv = Base64.encodeToString(encryptCipher.iv, Base64.DEFAULT) //Encode the Iv to base64 outputStream.use { it.write(iv.length) // length of the IV it.write(iv.toByteArray()) // Iv base64 it.write(encryptedBytes.toByteArray()) // encrypted data that is base64 } return encryptedBytes.toByteArray() } fun decrypt(inputStream: InputStream): ByteArray { return inputStream.use { val ivSize = it.read() val iv = ByteArray(ivSize) it.read(iv) val encryptedBytes = it.readBytes() // This is better as you have got your iv read so what is left in the buffer should be data inputStream.close() // close the Stream to be safe. val ivDecoded = Base64.decode(iv, Base64.DEFAULT) // decode your IV base64 string val encryptedDecoded = Base64.decode(encryptedBytes, Base64.DEFAULT) //decode the data from base64 that is encrypted val decipheredByteArray = getDecryptCipherForIv(ivDecoded).doFinal(encryptedDecoded) //Decrypt the data // Strip the Initialization Vector (usually first 16 bytes) to get the actual unencrypted data decipheredByteArray.toList().subList(ivDecoded.size, decipheredByteArray.size).toByteArray() } }
@ankitverma3932
@ankitverma3932 Жыл бұрын
I would love to watch flutter tutorials from you...
@denisoluka
@denisoluka 7 ай бұрын
Awesome
@BashPSK
@BashPSK Жыл бұрын
Bro Please upload same thing of firebase database🤗🤗
@moeketsipelesa1012
@moeketsipelesa1012 Жыл бұрын
Nice video Can you please make a tutorial on how to create and draw pdf files with jetpack compose
@PhilippLackner
@PhilippLackner Жыл бұрын
That's not done via compose, that's done using a library usually. But PDFs is an interesting topic, thanks
@ankitverma3932
@ankitverma3932 Жыл бұрын
@@PhilippLackner a ebook reader sort of app
@shubhamshinde4042
@shubhamshinde4042 Жыл бұрын
Hey guys can anyone tell me how I can store JWY key in my android application?
@bryankeltonadams
@bryankeltonadams Жыл бұрын
Probably the same way this video recommends.
@chaitanyasalwan6711
@chaitanyasalwan6711 Жыл бұрын
Hi Philipp can you make a separate course on Android Security for offline as well as online system cause there are some tools(http canary) which can capture https data. Even ssl pining is useless there are tools which can disable them . Even signature verifications(ultima - a tool that kills every signature protection) are useless these days , using NDK is safe but there are no tutorials on web for the same
@ankitverma3932
@ankitverma3932 Жыл бұрын
what about assymettric encryption
@chaitanyasalwan6711
@chaitanyasalwan6711 Жыл бұрын
@@ankitverma3932 how?
@ankitverma3932
@ankitverma3932 Жыл бұрын
RSA encryption
@chaitanyasalwan6711
@chaitanyasalwan6711 Жыл бұрын
@@ankitverma3932 public ,private key one? ohk so i am not that much experienced in this type of enc can you give me a simple algo means b/w server and user I think so there's a way to decrypt those
@ankitverma3932
@ankitverma3932 Жыл бұрын
Yeah.. there are a bunch of medium articles about RSA encryption.
@luichigo15
@luichigo15 19 күн бұрын
It's not working, if you restart the app, it will crash
@amalmanoj5074
@amalmanoj5074 Жыл бұрын
Gestures , custom pointerInput anything 🌝
@vancedsyahrul7059
@vancedsyahrul7059 Жыл бұрын
cool
@codemine5626
@codemine5626 Жыл бұрын
While loading data after closing app, this exception occurs kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the object
@peterka4711
@peterka4711 Жыл бұрын
Hi, experiencing the exact same issue - have you managed to solve it?
@ChrisAthanas
@ChrisAthanas Жыл бұрын
I am seeing the same thing, not sure how to solve it. Looks like the first part of the data is mangled, but the second part is fine...
@ChrisAthanas
@ChrisAthanas Жыл бұрын
I keep getting my comments deleted and not sure why... Here goes my third attempt: This code change prevents the bug when you close the app and attempt to reopen it and click "load" to load the previously saved values. // in CryptoManager.kt, you have to add and strip the Initialization Vector (IV) as shown below: fun encrypt(bytes: ByteArray, outputStream: OutputStream): ByteArray { // Include the Initialization Vector (IV) at the start of the data to encrypt val encryptedBytes = encryptCipher.doFinal(encryptCipher.iv + bytes) outputStream.use { it.write(encryptCipher.iv.size) it.write(encryptCipher.iv) it.write(encryptedBytes.size) it.write(encryptedBytes) } return encryptedBytes } fun decrypt(inputStream: InputStream): ByteArray { return inputStream.use { val ivSize = it.read() val iv = ByteArray(ivSize) it.read(iv) val encryptedBytesSize = it.read() val encryptedBytes = ByteArray(encryptedBytesSize) it.read(encryptedBytes) val decipheredByteArray = getDecryptCipherForIv(iv) .doFinal(encryptedBytes) // Strip the Initialization Vector (usually first 16 bytes) to get the actual unencrypted data decipheredByteArray .toList() .subList(ivSize, decipheredByteArray.size) // skip the IV .toByteArray() } }
@jirigongol4207
@jirigongol4207 Жыл бұрын
@@ChrisAthanas Thank you so much, you saved my day🙏👏
@radu2173
@radu2173 Жыл бұрын
@@ChrisAthanas ty very much
@miq0717
@miq0717 Жыл бұрын
It produces following error when trying to save new data second time after saving once. What to do? ```java.lang.IllegalStateException: IV has already been used. Reusing IV in encryption mode violates security best practices.```
@ChrisAthanas
@ChrisAthanas Жыл бұрын
Check my comment above for solution
@quangthaita6084
@quangthaita6084 19 күн бұрын
decrypt logic has an issue so app can not load data after restarting app. I found an solution to fix it. a small change in decrypt function fun decrypt(inputStream: InputStream): ByteArray = inputStream.use { val ivSize = it.read() val iv = ByteArray(ivSize) it.read(iv, 0, ivSize) val encryptedBytesSize = it.read() val encryptedBytes = ByteArray(encryptedBytesSize) it.read(encryptedBytes, 0, encryptedBytesSize) getDecryptCipherForIv(iv).doFinal(encryptedBytes) }
@jirigongol4207
@jirigongol4207 Жыл бұрын
I used Phillip GitLab code...when save some values, relaunch app and try to load values, It throws me JSON exception: kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the object '{', but had 'EOF' instead JSON input: �A��W��J[^�r43t","password":"test"} Does anyone know whats the problem, How to fix it? It seems that at least password value was correctly decrypt...
@ChrisAthanas
@ChrisAthanas Жыл бұрын
Check my comment above for solution
@salhayek10
@salhayek10 Жыл бұрын
Thanks a lot! I learned a lot from your videos! It's just amazing! Likes everywhere! I forgot to write get in "encryptCipher get()"... and got this for 4 hours "java.lang.IllegalStateException: IV has already been used. Reusing IV in encryption mode violates security best practices." Maybe someone will help.
@dsonozaki
@dsonozaki 10 ай бұрын
thank you a lot, I have done the same mistake
Full Guide to Delegation in Kotlin  - Android Studio Tutorial
18:10
Philipp Lackner
Рет қаралды 66 М.
Дибала против вратаря Легенды
00:33
Mr. Oleynik
Рет қаралды 4,1 МЛН
Increíble final 😱
00:37
Juan De Dios Pantoja 2
Рет қаралды 111 МЛН
ROOM Database - #1 Create Database Schema | Android Studio Tutorial
14:59
"Stop Using Async Await in .NET to Save Threads" | Code Cop #018
14:05
5 Fatal Coroutine Mistakes Nobody Tells You About
18:15
Philipp Lackner
Рет қаралды 85 М.
DataStore: Best practices - MAD Skills
8:46
Android Developers
Рет қаралды 19 М.
Most overpowered way to build mobile apps?
8:33
Beyond Fireship
Рет қаралды 727 М.
Architecture: The Domain Layer - MAD Skills
8:48
Android Developers
Рет қаралды 55 М.
Дибала против вратаря Легенды
00:33
Mr. Oleynik
Рет қаралды 4,1 МЛН