Android encryption



hello everyone, my name is oum saokosal. today i'm gonna teach about android sharedpreferences. i will use it for remember username and passwordin the log-in page. so, first you have to know how to read datafrom php mysql. for that i use one of my libraries, i call"generic_asynctask_v2" and then you use shared


Android encryption, preferences to store data and lastly we haveto encrypt data to md5. so i use a library of mine as well. i call it "md5 simply". so all of these in github.com/kosalgeek.


so, this is my youtube channel. please subscribe it. if you want to watch another video just goand browse it here. there are two languages english and khmerwhich is my native language. this is the github. you go to repositories and you will find generic_asynctaskhere and md5 simply and you go here just click download and grab the genasync.1.2.tarand md5 simply also download and grab md5 simply. because i downloaded it already so i willgrab the code so let's add the library first


and zip it, open it and copy, you go to androidhere. let's create a project, use the blank one. so to add a library you click over here andchoose project and you will find add and you go to libs and right-click here paste, andthen right-click again on the jar file and you say add aslibrary here, ok. so you need another library. this one just for md5 so to connect to thewebsite you need another library called generic_asynctask_v2 so copy again and right-click...paste it here,ok. right-click again and choose add aslibrary.


so you can go back to android, you have tomake sure that the library is already added, go to gradle & the second one & you will seethese two lines, it means you are good to go. so to connect to the website you have to askfor a permission android manifest here and you say uses-permission android internet,ok. so then you can use the internet and connectto the website. so go to the main activity here ok, this isthe...you know the basic code here you just edit in here... so in the layout you wantto have like log-in for e-mail and password. there are two file for you.


the activity main here is for the button thefloating button and the content main is a place you want to add the you know like thetextbox anything here. so delete it, ok. zoom in a little bit and you need an e-mail,grab e-mail like this, ok. and a password and you need a checkbox becauseyou want to remember your password here being dragged like this. and the last one is a button for the log-inso rename it i call it etemail username or password, e-mail it's up to you. and here i call remember me and cbremembercheck.


i want to check as a default . and login etonloginand then you want to remove this button you got to activity main and just simply deletelike this. but you have to delete the code in here aswell here. and if you don't want a menu, this is themenu code, just delete it. ok. so run first to test it. here you can give a username or maybe e-maillike this and a password maybe 123 and you remember this and log-in and you go to anotherpage and after that you log-in again the log-in page will be skipped because you rememberedthe password.


ok, this is the scenario. so first you have to know how to get the datafrom the webpage so i add the library here as you can see before the generic_asynctask_v2if you open here you will see postresponseasynctask, this is the main class and you will querythat back from asyncresponse class. so here postresponseasynctask we call it tasknew postresponseasynctask. the first one is the context so this and youwant to put the data we can use the third constructor. if you don't have anything to parse just usethe first one... so this is the class new you press ctrl+space and you will see asyncresponselike this.


and over here you can get the data insidehere, ok. say and then you want to point to a url. so now i want to use the local one so firstwe will have to have you know like a wampp or xampp for me i use xampp and i have torun it and port is 80. if you use another port we have to specifyit here. because you want to use the local one usuallysay like this http://localhost/. usually this is the method you want to connectto the local. but the local-host here is inside the genymotionor emulator as well. so if you call local-host it will not connectto your machine local-host.


task.execute("http://localhost/"); soat this point you have to give the alias ip address it's 10.0.2.2 if you are using theandroid 2 emulator but if you use genymotion like me you can use 3 instead. and if you are using another port beside 80you have to tell let's say you call 8080 like this than you would say http://10.0.3.2:8080and if you have another page it's for me customer so i have to give to the correct location. so at this point you have to have a php page. so now you go to htdocs folder[?8:33] andfor [8:34] there is one problem as well. usually you cannot easily connect to the 10.0.3like this because it has some permission like


a config file and then you have to changesomething. if you don't change it you cannot connect. you can go to the google and say wamp permissiondenied like this there are a lot of people that have the same problem and to do thatyou see c:\wamp\alias\phpmyadmin.conf you have to change from this to this. the thing is here ok. you can change something like that or youcan google for more but if you are using xampp like me, you are good to go. you don't have any problem.


now go to htdocs i have a php here under thefolder customer so maybe just test the local first so this is the current root. you should go to your browser and say local-host. ok this is the local-host xampp like thiscopy...like this. and remember there's a lot of problems. at first you have to figure out be seeingthe log...so let's see it. now simply ok. but let's say you don't have any internetor maybe your web server is down...stop all like this.


test it again. ok, as you can see here it said: "failedto connect to 10.0.3.2 after 15ms" so you have to see this so make sure you havethe good internet and the web-server must be on so start again, and the port must beright. so now i can connect to the customer. and in my customer i have the index.php pagefor that so let's see the code first. we can open it in any text-editor. for now i use brackets. it's a open-source.


so this is the log-in page actually if yougo to local-host customer it is the index.php because i have already written the code soi will explain a little bit. for example the isset it means that you wanttest it is the txtusername or txtpassword will come in and on not? and come in fromthe $_post. $_post it means that the data you will notsee from the form here the $_post and then you grab that and assign to username and thenselect the data from a database. the database here is from connection and iwill post the source code in the description of the youtube so you can download the fullsource-code from there. so if you're using the different database,for example here i use customer and you just


change to your database and let's go to thedatabase here. this is the database customer. i have two tables. the first one is tbl_user for the log-in,this is what we are going to do with so kosal and kosal, abc and 1 2 3 and this one is astring. ok? this is the auto_increment so nothing specialhere. and if you want to compare it like exactlythe same you can use binary. so if the result is greater than one it meansthat you have the data in the database.


it means correct username and password thenyou echo success. then you grab the success and check in the website. the exit here it means that you exit, youdon't want the html below to appear. so this is log-in and kosal... kosal log-in...this is the word success...you grab the success and then you test it in your android. this is the url to the index page so i willlog this...so i will declare: final string tag = this.getclass().getname(); this will give a name here. we can just say mainactivity but this onewill grab the name and if the name change


then the text will change to the same name. ok?. and easy to do a log: log.d(tag, s); we run it. as you can see this is the result but it isin html. the html it is from here ok? and you can see the code when you just log-inright-click and view page source this is the code.


ok? it's not logged in yet. so you want to pass the data to the form. ok.so you remember the php here you have thetextusername and textpassword so you can put the data over there...so here we call: hashmap data = new hashmap(); and then . so because the hashmap use thekey and value here so you can put the key...so what is the key here the key is the variableof here in the $_post so you have to copy if you are not sure and then this one as well. so this is the data for that.


so let's say kosal ok data.put("txtusername", "kosal");data.put("txtpassword", "kosal"); the password is kosal. and you will see something different fromthe before but here we give the data as a second parameter here and new maybe enterthis. and log here because we put the tag as a nameof the class.let's say mainactivity ok.. see? the problem that you see is because that ididn't put the index.php at the end. remember this is not a redirect page so itdoesn't know if you put you know the customer like this it will redirect to the index.php.


it doesn't know so you have to give the exacturl to the php page. so now you get the word success here. so after you get the word success you cando something else. if s equal and you cannot put the equal signfor the string in java so success. sometime the word equal here can have someproblem as well because sometime the success has a space at the end so i suggest you touse contains like that. it contains success then you can go to thesubpage for example i have to create a sub-page here...right-click, you go to new activity,blank activity, sub-activity for this you see something like this just click the refreshbutton it should disappear.


ok, now you wanna go to the sub so you callintent in new intent from mainactivity this subactivity class and startactivity(in) thenit should go to the sub when it says successful. test it again...ok. now you can go to the sub because it is akosal and kosal the right password. but you want to input the data so you go tocontent_main.. the one that we have the interface here so remember the etemail, password, buttonand check remember here so you have to get it here first: button btnlogin;edittext etemail, etpassword; checkbox cbremember;


and maybe over here you call it by find youby id and cast to button and assign to the btnlogin.. btnlogin = (button)findviewbyid(r.id.btnlogin); ...and you can do the same for the other ok. so, now you can get the data so you want toget the data from the e-mail and password here so right here you wanna call maybe loginlike this... btnlogin.setonclicklistener(this); and then make mainactivityimplement onclicklistener. it will open onclick method for you so cutall code from here paste it here.


so now you wanna get the data from etmail.gettext().tostring()etpassword.gettext().tostring() ...data.put("txtusername", etemail.gettext().tostring()); data.put("txtpassword", etpassword.gettext().tostring()); give a wrong password e-mail first log-in..nothing. we can have message like log-in failed. try again here ok with the correct passwordok. now you can go to the sub-activity it's thecorrect one so we have completed a first one so we will go to sharedpreferences to savethe data and use it as like a remember me. so over here.. so maybe .. over here you havethe call sharedpreferences interface and maybe


you call pref here for short and you get asharedpreferences and the first one is the file name and maybe i call it "login.conf"and the mode the context must be mode_private you have to use mode_private. you don't want to expose your log-in [21:29]to another app it's very important. sharedpreferences pref = getsharedpreferences("login.conf",context.mode_private); and sharedpreferences.editor editor = pref.edit(); then you get the data from there. let me show you the example first ...edithere you can put string now it's a key in


value similar to this one...so you give thekey...what is the key here i say username and value is the etemail we just grab it likethis and the password similar etpassword. after you put all the string you have to callapply to save it editor.apply(); ...let's say it save any data and you wantto grab it back you have to call pref.getstring() ok then you have to give a key a correct oneso username and a second one is a default value so if you save something it means thatthe data username was not saved then it will give the default one now so now i give a defaultas a blank one and password so let's log it log.d(tag, pref.getstring("username",""));


log.d(tag, pref.getstring("password","")); because the etemail and password did not applyyet so it's like a blank one... so maybe i have to put it inside of onclick here so . maybei put it over here but now we got a problem the pref here is not belong (doesn't belong)to this method so i have to declare it outside so i cut and this one as well delete it likethis ok s i put it inside onclick so now even the right or wrong password it will save thedata because just for testing. let's say 1 2 3, 1 2 3 now the data is 1 23, 1 2 3. ok, now let's say the [24:39] you write ithere is the right thing so let's comment it here it means that you get a data from thefile should get the data 1 2 3, 1 2 3 from


the log-in here even though we don't put astring now nothing but click here see is 1 2 3, 1 2 3. now you got the idea of how to use the sharedpreferences, it's not that hard. maybe after you log-in success you want tosave it. and maybe you just delete this one. but remember when we log-in only you checkthis if you don't check this it will ask you to enter username and password again so thecheckbox here... you have to have an event so you set


cbremember.setoncheckboxchangedlistener(this); ...makemainimplement it so we will createanother method for that. this is the checked, ok? if it is checked then it's checked, if itis not then it's not. so easy right? it's check. it's a boolean. so you want to get the data so now i haveto declare a boolean outside and i call it checkflag.. so the flag here is like trueor false. so first i wanna get the data of the checkflagfrom the cbremember is checked , ok?


so ... checkflag = cbremember.ischecked(); ...it will return the data the default datathat we put in here. if we put it like unchecked so the defaultwill be unchecked. now is checked then it's really true. ok, so if the checkflag is true so now wehave to get the data from here as well checkflag = ischecked; ...we change and then we will change the flagkey as well. now test it first.


i don't want to confuse you. so maybe here, log.d(tag, "checkflag: " + checkflag); and because it's a boolean it will be likethis... and here as well. let's see the log here when i uncheck see? now the checkflag is false. first one is true because of the here ok? so let's say i uncheck it and i run it again,as you can see now the checkflag is false as is the first one. if i check it then it will be true.


this is the checkflag. it's very important because we will use thecheckflag over here and we will say... if checkflag is true but for a boolean you don't have totake with the double equalities, you just like just ...if checkflag is true then youwant to save. and if not you don't save. so just all you have to do. let's say 1 2 3 and like this and we say checkok? now it's false right? it's not gonna saveanything and if you say kosal and password kosal like this it will save, ok?


now you wanna test if it's saved or not. now you go to sub here.. you want to use thesame class because you want to choose the same cache so, what you want is just thisline. you don't have to use the editor. editor is for writing. and over here pref getsharedpreferences defaultname must be the same. it's better to copy and paste here ... pref = getsharedpreferences("login.conf",context.mode_private); ...now log again so copy the log-in then youcan...log.d(tag, and now pref.getstring so


...maybe username like this and the defaultone we leave blank and the password as well a b c, 1 2 3, and you don't remember. now it is the last data. it didn't save. ok it is correct. so... here let's run it again. let's say i check "remember me", abc, 123, checked, login now abc, 123 so it is the correct one. so when you have the data saved, over here you want to test that if the data is saved then you wanna check it. if it is the correct password then youdon't have to you know enter the username and password again. so around here you haveto test it if let's say we get the data or sorry maybe not this one.. if pref.getstringand username, ok? maybe like this but maybe


it's better to assign to string . maybe it'sbetter to do it outside like this and string password. ... so you have to check if it is a blankone then it's not correct, if it is not blank maybe there is some data. so you have to check if it is not blank. it's easy:if(!(username.equals("")) && password.equals("") it just means that if username and password are not blank, there must be something saved in here.


but why you have to check it like this? because the username and the password in the database might be changed somewhere else. for example like you are using computer and you change the password and the app should check it again, you know. so this is the best way to do that. so you have to check it here. and you have to call postasynctask again so it's almost the same. just copy here. and the hashmap code as well. and you wanna pass in the username and password that have been saved


in the sharedpreferences you give the data like this. but now you don't wanna save anything but you wanna check the data in sharedpreferences aganst the data in database if it is correct, it will return "success" if it is success, you will go into intent. now go here and click (run) as you can see, it will skip the login page and go to subactivity here.


because it checked the data over here, and send it here. so this is the correct one, you can do like... know.. maybe you can have a flash-screen youknow.. and then check the username and password and if not you will go the log-in, if thecorrect one maybe skip the log in and go to the sub one. so it is your way now i justgive the basic understanding. ok. now in the sub one you want to clear and you want toyou know log out. so in the sub menu maybe come down here, you can have a button to logout. but because now we have the button here it's a acitivysub here so i wanna click onthis button and i wanna log out. so first i wanna change the button let's call it maybeinputdelete.. if not the right one if you


wanna a good image, icon maybe go and downloadfrom the internet. but now just use this one. so let's call it fablogout and then insidethe sub-activity here fablogout floatingactionbutton fablogout = (flatingactionbutton)findviewbyid(r.id.fablogout); fablogout.setonclicklistener((view) ? {} so, over here i want to clear all the dataso maybe it's better to call it before the floating, ok. so, pref here you call editif you wanted to clear edit it's ok, if not just call edit like this and clear and commit.it will clear all the data in the login.conf. ok? it's a shortcut. if you want full codeit's like this sharedpreferences.editor editor;


.it's better to call it outside and then editor= pref.edit() and editor. clear, editor.comit. editor = pref.edit();editor.clear(); editor.commit(); and after you clear it you wanna go back tothe main one.. so intent in = new intent(subactivity.this, mainactivity.class);startacitivity(in); ok, so it should go back if you click thebutton. as you can see now it skipped log-in and go to sub but let's say log-out now, youcan see the log-in page again. let's run it again. the data should be cleared. not let'ssee the log even though you put the main here nothing. and the checkflag is false let'ssay correct password kosal and kosal and log


int now if you don't check it to test it.ok. go out do the start press it again see it's correct one and check, log in sub, notit's skipped to the sub-activity. ok? it's almost done except the md5. the reasonwhy we have to use the md5 here is that the log-in username and password is very important.we should you know encrypt.. and you can just do it by using my library md5 simply...soyou go there to the md5 simply and grab the md5 simply here. actually we copied alreadyso don't need it. so to it the md5 simply is very easy here when you put the passwordhere, the save here, we'll call md5 and then encrypt. that's all you need to do. ok. editor.putstring("password", md5.encrypt(etpassword.gettext().tostring());


here is encrypt. but let's test it beforewe do anything. what is the code here after encrypt? so kosal, kosal, check, log in. now this themd5 code, ok? so you have to go here and check now you check it but remember this is thecheck, ok? so let's see if you don't put the md5 over here just only when you save. itdoesn't know because the md5 code and the code that we enter from website is wrong.ok? so what you do here is you go to the php and you have to encrypt the password as wellas the md5. the username here is not md5'ed so we cannot just when you insert the datawe have to insert it with the md5. so we create the insert one.. this is the insert page butinsert for the product. just copy.. let's


call it we insert user so paste the code,that's right, and here input password and txtusername, txtpassword ok...like this. andhere you have to check username and txtpassword and delete the rest. ok. something like this..good. name password...user...ok. and the uid is auto_increment so we don't need it. hereit's password and must be a single quote for string. delete the rest. ok. should be good.so delete all of this. but when we insert it the password has to change to the md5 aswell so call it insertuser.php. now let's call it kosalgeek and password is kosalgeek,insert. now insert successfully. go to the database and check it out. pressed. now themd5 is inserted. ok. so when i compare the data from the database and here it must becorrect, ok? maybe one more thing over here...we


have to put that md5.encrypt() should be finenow. ok. ...geek, kosalgeek, log in, yes now i don't remember so it should not be saved.ok. it's good again. ok. it saved. log in again. ok. as you can see now it skipped tothe sub-activity, it means that the password is correct and we log out. should clear allthe data. ok. this is the way. so let me summarize it. so, first you have to have a php pagefor the insert user. and make sure that you have the md5 when you put the username tothe database. when you check the data here it's ok. don't put the md5 but when you checkthe data by enter from edit text here you have to check with the md5. and when the checkboxis ticked then you want to add the data and


Android encryption

you encrypt with the md5 as well.


ok. this is the end of the video. so if youlike it please don't forget to subscribe my channel and i will see you in the next goodvideo. thank you.


Android encryption Rating: 4.5 Diposkan Oleh: PaduWaras