Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet39/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   35   36   37   38   39   40   41   42   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Decryp/ng the file
Locate 
decrypt
 and add the following code:
private
 
fun
 
decrypt
(map: HashMap)
: ByteArray? 

  
var
 decrypted: ByteArray? = 
null
 
  
try
 { 
    
// 1
 
    
val
 salt = map[
"salt"

    
val
 iv = map[
"iv"

    
val
 encrypted = map[
"encrypted"

    
// 2 
 
    
val
 passwordChar = passwordString.toCharArray() 
    
val
 pbKeySpec = PBEKeySpec(passwordChar, salt, 
1324

256

    
val
 secretKeyFactory = 
SecretKeyFactory.getInstance(
"PBKDF2WithHmacSHA1"

    
val
 keyBytes = 
secretKeyFactory.generateSecret(pbKeySpec).encoded 
    
val
 keySpec = SecretKeySpec(keyBytes, 
"AES"

    
// 3 
 
    
val
 cipher = Cipher.getInstance(
"AES/CBC/PKCS7Padding"

    
val
 ivSpec = IvParameterSpec(iv) 
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec) 
    decrypted = cipher.doFinal(encrypted) 
  } 
catch
 (e: Exception) { 
    Log.e(
"SIMPLENOTE"

"decryption exception"
, e) 
  } 
  
// 4
 
  
return
 decrypted 
}
Saving Data on Android
Chapter 1: Using Files
raywenderlich.com
36


Here’s what’s happening:
1. Retrieve the 
salt

iv
 and 
encrypted
 fields from the 
HashMap
.
2. Regenerate the key from the password.
3. Decrypt the encrypted data.
4. Return the decrypted data.
Call the decrypt function after reading the file in 
getNote
.
val
 note = Note(fileName, 
""

if
 (isExternalStorageReadable()) { 
  
// 1
 
  ObjectInputStream(noteFileInputStream(note.fileName)).use 
{ stream -> 
    
// 2
 
    
val
 mapFromFile = stream.readObject() 
as
 HashMapByteArray> 
    
// 3
 
    
val
 decrypted = decrypt(mapFromFile) 
    
if
 (decrypted != 
null
) { 
      note.noteText = String(decrypted) 
    } 
  } 

return
 note
Here’s how it works:
1. Open an 
ObjectInputStream
 with 
use
 for reading data.
2. Read the data and store it into the 
HashMap
.
3. Decrypt the file with 
decrypt
; if it was successful, assign the decrypt text to 
note.noteText
.
Build and run the code now. Notice the weird, encrypted, obfuscated data is now 
decrypted and understandable again! :]
Saving Data on Android
Chapter 1: Using Files
raywenderlich.com
37


Understanding Parceliza/on and 
Serializa/on
In computer science, ** marshaling** or 
marshaling
 is the process of transforming 
an object into a format that is suitable for transmission or storage. Android apps 
often transfer data from one activity to another, where 
Parcelization
 and 
Serialization
 are the means of marshaling objects.
By default — and because your app utilized 
ObjectOutputStream
 to write the file — 
the encrypted data, iv and salt values were all 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   35   36   37   38   39   40   41   42   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish